Issues (91)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/JaneOpenApi.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Joli\Jane\OpenApi;
4
5
use Fitbug\SymfonySerializer\YamlEncoderDecoder\YamlDecode;
6
use Fitbug\SymfonySerializer\YamlEncoderDecoder\YamlEncode;
7
use Fitbug\SymfonySerializer\YamlEncoderDecoder\YamlEncoder;
8
use Joli\Jane\Encoder\RawEncoder;
9
use Joli\Jane\Generator\Context\Context;
10
use Joli\Jane\Generator\File;
11
use Joli\Jane\Generator\ModelGenerator;
12
use Joli\Jane\Generator\Naming;
13
use Joli\Jane\Generator\NormalizerGenerator;
14
use Joli\Jane\Guesser\ChainGuesser;
15
use Joli\Jane\OpenApi\Generator\ClientGenerator;
16
use Joli\Jane\OpenApi\Generator\GeneratorFactory;
17
use Joli\Jane\OpenApi\Guesser\OpenApiSchema\GuesserFactory;
18
use Joli\Jane\OpenApi\Model\OpenApi;
19
use Joli\Jane\OpenApi\Normalizer\NormalizerFactory;
20
use Joli\Jane\OpenApi\SchemaParser\SchemaParser;
21
use Joli\Jane\Registry;
22
use Joli\Jane\Schema;
23
use PhpCsFixer\Config;
24
use PhpCsFixer\ConfigInterface;
25
use PhpCsFixer\Console\ConfigurationResolver;
26
use PhpCsFixer\Differ\NullDiffer;
27
use PhpCsFixer\Error\ErrorsManager;
28
use PhpCsFixer\Finder;
29
use PhpCsFixer\Runner\Runner;
30
use PhpCsFixer\ToolInfo;
31
use PhpParser\PrettyPrinter\Standard as StandardPrettyPrinter;
32
use PhpParser\PrettyPrinterAbstract;
33
use Symfony\Component\Serializer\Encoder\JsonDecode;
34
use Symfony\Component\Serializer\Encoder\JsonEncode;
35
use Symfony\Component\Serializer\Encoder\JsonEncoder;
36
use Symfony\Component\Serializer\Serializer;
37
use PhpCsFixer\Cache\NullCacheManager;
38
use PhpCsFixer\Linter\Linter;
39
40
class JaneOpenApi
41
{
42
    const VERSION = '1.0.x';
43
44
    /**
45
     * @var SchemaParser
46
     */
47
    private $schemaParser;
48
    /**
49
     * @var Generator\ClientGenerator
50
     */
51
    private $clientGenerator;
52
53
    /**
54
     * @var \PhpParser\PrettyPrinterAbstract
55
     */
56
    private $prettyPrinter;
57
58
    /**
59
     * @var ModelGenerator
60
     */
61
    private $modelGenerator;
62
63
    /**
64
     * @var NormalizerGenerator
65
     */
66
    private $normalizerGenerator;
67
68
    /**
69
     * @var ChainGuesser
70
     */
71
    private $chainGuesser;
72
73
    /**
74
     * @var ConfigInterface
75
     */
76
    private $fixerConfig;
77
78
    /**
79
     * JaneOpenApi constructor.
80
     *
81
     * @param SchemaParser          $schemaParser
82
     * @param ChainGuesser          $chainGuesser
83
     * @param ModelGenerator        $modelGenerator
84
     * @param NormalizerGenerator   $normalizerGenerator
85
     * @param ClientGenerator       $clientGenerator
86
     * @param PrettyPrinterAbstract $prettyPrinter
87
     * @param ConfigInterface|null  $fixerConfig
88 13
     */
89
    public function __construct(
90
        SchemaParser $schemaParser,
91
        ChainGuesser $chainGuesser,
92
        ModelGenerator $modelGenerator,
93
        NormalizerGenerator $normalizerGenerator,
94
        ClientGenerator $clientGenerator,
95
        PrettyPrinterAbstract $prettyPrinter,
96
        ConfigInterface $fixerConfig = null
97 13
    ) {
98 13
        $this->schemaParser        = $schemaParser;
99 13
        $this->clientGenerator     = $clientGenerator;
100 13
        $this->prettyPrinter       = $prettyPrinter;
101 13
        $this->modelGenerator      = $modelGenerator;
102 13
        $this->normalizerGenerator = $normalizerGenerator;
103 13
        $this->chainGuesser        = $chainGuesser;
104 13
        $this->fixer               = $fixerConfig;
105
    }
106
107
    /**
108
     * Return a list of class guessed
109
     *
110
     * @param Registry $registry A registry
111
     * @param string $name
112
     *
113
     * @return Context
114 13
     */
115
    public function createContext(Registry $registry, $name)
0 ignored issues
show
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116 13
    {
117
        $schemas = array_values($registry->getSchemas());
118
119 13
        /** @var Schema $schema */
120 13
        foreach ($schemas as $schema) {
121 13
            $openApiSpec = $this->schemaParser->parseSchema($schema->getOrigin());
122 13
            $this->chainGuesser->guessClass($openApiSpec, $schema->getRootName(), $schema->getOrigin() . '#', $registry);
123
            $schema->setParsed($openApiSpec);
124
        }
125 13
126 13
        foreach ($registry->getSchemas() as $schema) {
127 10
            foreach ($schema->getClasses() as $class) {
128
                $properties = $this->chainGuesser->guessProperties($class->getObject(), $schema->getRootName(), $class->getReference(), $registry);
129 10
130 10
                foreach ($properties as $property) {
131
                    $property->setType($this->chainGuesser->guessType($property->getObject(), $property->getName(), $property->getReference(), $registry));
132
                }
133 13
134
                $class->setProperties($properties);
135
            }
136
        }
137 13
138
        return new Context($registry);
139
    }
140
141
    /**
142
     * Generate a list of files
143
     *
144
     * @param Registry $registry A registry
145
     *
146
     * @return File[]
147 13
     */
148
    public function generate(Registry $registry)
149
    {
150 13
        /** @var OpenApi $openApi */
151
        $context = $this->createContext($registry, 'Client');
152 13
153
        $files   = [];
154 13
155 13
        foreach ($registry->getSchemas() as $schema) {
156
            $context->setCurrentSchema($schema);
157 13
158 13
            $files = array_merge($files, $this->modelGenerator->generate($schema, $schema->getRootName(), $context));
159 13
            $files = array_merge($files, $this->normalizerGenerator->generate($schema, $schema->getRootName(), $context));
160
            $clients = $this->clientGenerator->generate($schema->getParsed(), $schema->getNamespace(), $context, $schema->getOrigin() . '#');
161 13
162 13
            foreach ($clients as $node) {
163
                $files[] = new File($schema->getDirectory() . DIRECTORY_SEPARATOR . 'Resource' . DIRECTORY_SEPARATOR . $node->stmts[2]->name . '.php', $node, '');
164
            }
165
        }
166 13
167
        return $files;
168
    }
169
170
    /**
171
     * Print files
172
     *
173
     * @param File[] $files
174
     * @param Registry $registry
175 13
     */
176
    public function printFiles($files, $registry)
177 13
    {
178 13
        foreach ($files as $file) {
179 13
            if (!file_exists(dirname($file->getFilename()))) {
180
                mkdir(dirname($file->getFilename()), 0755, true);
181
            }
182 13
183
            file_put_contents($file->getFilename(), $this->prettyPrinter->prettyPrintFile([$file->getNode()]));
184
        }
185 13
186 13
        foreach ($registry->getSchemas() as $schema) {
187
            $this->fix($schema->getDirectory());
188 13
        }
189
    }
190
191
    /**
192
     * Use php cs fixer to have a nice formatting of generated files
193
     *
194
     * @param string $directory
195
     *
196
     * @return array|void
197 13
     */
198
    protected function fix($directory)
199 13
    {
200
        if (!class_exists('PhpCsFixer\Config')) {
201
            return;
202
        }
203
204 13
        /** @var Config $fixerConfig */
205
        $fixerConfig = $this->fixerConfig;
206 13
207 13
        if (null === $fixerConfig) {
208 13
            $fixerConfig = Config::create()
209 13
                ->setRiskyAllowed(true)
210
                ->setRules(
211 13
                    array(
212
                        '@Symfony' => true,
213
                        'array_syntax' => array('syntax' => 'short'),
214
                        'simplified_null_return' => false,
215
                        'ordered_imports' => true,
216
                        'phpdoc_order' => true,
217
                        'binary_operator_spaces' => array('align_equals'=>true),
218
                        'concat_space' => false,
219
                        'yoda_style' => false,
220
                        'header_comment' => [
221
                            'header' => <<<EOH
222
This file has been auto generated by Jane,
223
224
Do no edit it directly.
225
EOH
226
                            ,
227
                        ]
228
                    )
229
                );
230 13
        }
231 13
        $resolverOptions = array('allow-risky' => true);
232
        $resolver = new ConfigurationResolver($fixerConfig, $resolverOptions, $directory, new ToolInfo());
233 13
234 13
        $finder = new Finder();
235 13
        $finder->in($directory);
236
        $fixerConfig->setFinder($finder);
237 13
238 13
        $runner = new Runner(
239 13
            $resolver->getConfig()->getFinder(),
240 13
            $resolver->getFixers(),
241 13
            new NullDiffer(),
242 13
            null,
243 13
            new ErrorsManager(),
244 13
            new Linter(),
245 13
            false,
246
            new NullCacheManager()
247
        );
248 13
249
        return $runner->fix();
250
    }
251 13
252
    public static function build(array $options = [])
253
    {
254 13
        $encoders        = [
255 13
            new JsonEncoder(
256 13
                new JsonEncode(),
257
                new JsonDecode(false)
258 13
            ),
259 13
            new YamlEncoder(
260 13
                new YamlEncode(),
261
                new YamlDecode(false, true, true, true)
262
            ),
263 13
        ];
264 13
        $normalizers     = NormalizerFactory::create();
265 13
        $serializer      = new Serializer($normalizers, $encoders);
266 13
        $schemaParser    = new SchemaParser($serializer);
267 13
        $clientGenerator = GeneratorFactory::build($serializer);
268 13
        $prettyPrinter   = new StandardPrettyPrinter();
269 13
        $naming          = new Naming();
270 13
        $modelGenerator  = new ModelGenerator($naming);
271
        $normGenerator   = new NormalizerGenerator($naming, isset($options['reference']) ? $options['reference'] : false);
272 13
273 13
        return new self(
274 13
            $schemaParser,
275 13
            GuesserFactory::create($serializer, $options),
276 13
            $modelGenerator,
277 13
            $normGenerator,
278 13
            $clientGenerator,
279
            $prettyPrinter
280
        );
281
    }
282
}
283