Issues (171)

Security Analysis    not enabled

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.

Unit/DependencyInjection/HttplugExtensionTest.php (6 issues)

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
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\Tests\Unit\DependencyInjection;
6
7
use Http\Adapter\Guzzle7\Client;
8
use Http\Client\HttpClient;
9
use Http\Client\Plugin\Vcr\Recorder\InMemoryRecorder;
10
use Http\HttplugBundle\Collector\PluginClientFactoryListener;
11
use Http\HttplugBundle\DependencyInjection\HttplugExtension;
12
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
13
use Symfony\Component\DependencyInjection\Reference;
14
use Symfony\Component\HttpKernel\Kernel;
15
16
/**
17
 * @author David Buchmann <[email protected]>
18
 * @author Tobias Nyholm <[email protected]>
19
 */
20
class HttplugExtensionTest extends AbstractExtensionTestCase
21
{
22
    protected function setUp(): void
23
    {
24
        parent::setUp();
25
26
        $this->setParameter('kernel.debug', true);
27
    }
28
29
    protected function getContainerExtensions(): array
30
    {
31
        return [
32
            new HttplugExtension(),
33
        ];
34
    }
35
36
    public function testConstants(): void
37
    {
38
        self::assertSame('httplug.client', HttplugExtension::HTTPLUG_CLIENT_TAG);
39
    }
40
41
    public function testConfigLoadDefault(): void
42
    {
43
        $this->load();
44
45 View Code Duplication
        foreach (['client', 'message_factory', 'uri_factory', 'stream_factory'] as $type) {
46
            $this->assertContainerBuilderHasAlias("httplug.$type", "httplug.$type.default");
47
        }
48
    }
49
50
    public function testConfigLoadClass(): void
51
    {
52
        if (!class_exists(Client::class)) {
53
            $this->markTestSkipped('Guzzle 7 adapter is not installed');
54
        }
55
56
        $this->load([
57
            'classes' => [
58
                'client' => Client::class,
59
            ],
60
        ]);
61
62
        $this->assertContainerBuilderHasService('httplug.client.default', Client::class);
63
    }
64
65
    public function testConfigLoadService(): void
66
    {
67
        $this->load([
68
            'main_alias' => [
69
                'client' => 'my_client_service',
70
                'message_factory' => 'my_message_factory_service',
71
                'uri_factory' => 'my_uri_factory_service',
72
                'stream_factory' => 'my_stream_factory_service',
73
            ],
74
        ]);
75
76 View Code Duplication
        foreach (['client', 'message_factory', 'uri_factory', 'stream_factory'] as $type) {
77
            $this->assertContainerBuilderHasAlias("httplug.$type", "my_{$type}_service");
78
        }
79
    }
80
81
    public function testClientPlugins(): void
82
    {
83
        $this->load([
84
            'clients' => [
85
                'acme' => [
86
                    'factory' => 'httplug.factory.curl',
87
                    'plugins' => [
88
                        [
89
                            'decoder' => [
90
                                'use_content_encoding' => false,
91
                            ],
92
                        ],
93
                        'httplug.plugin.redirect',
94
                        [
95
                            'add_host' => [
96
                                'host' => 'http://localhost:8000',
97
                            ],
98
                        ],
99
                        [
100
                            'content_type' => [
101
                                'skip_detection' => true,
102
                            ],
103
                        ],
104
                        [
105
                            'header_append' => [
106
                                'headers' => ['X-FOO' => 'bar'],
107
                            ],
108
                        ],
109
                        [
110
                            'header_defaults' => [
111
                                'headers' => ['X-FOO' => 'bar'],
112
                            ],
113
                        ],
114
                        [
115
                            'header_set' => [
116
                                'headers' => ['X-FOO' => 'bar'],
117
                            ],
118
                        ],
119
                        [
120
                            'header_remove' => [
121
                                'headers' => ['X-FOO'],
122
                            ],
123
                        ],
124
                        [
125
                            'query_defaults' => [
126
                                'parameters' => ['locale' => 'en'],
127
                            ],
128
                        ],
129
                        [
130
                            'authentication' => [
131
                                'my_basic' => [
132
                                    'type' => 'basic',
133
                                    'username' => 'foo',
134
                                    'password' => 'bar',
135
                                ],
136
                            ],
137
                        ],
138
                        [
139
                            'cache' => [
140
                                'cache_pool' => 'my_cache_pool',
141
                            ],
142
                        ],
143
                    ],
144
                ],
145
            ],
146
        ]);
147
148
        $plugins = [
149
            'httplug.client.acme.plugin.decoder',
150
            'httplug.plugin.redirect',
151
            'httplug.client.acme.plugin.add_host',
152
            'httplug.client.acme.plugin.content_type',
153
            'httplug.client.acme.plugin.header_append',
154
            'httplug.client.acme.plugin.header_defaults',
155
            'httplug.client.acme.plugin.header_set',
156
            'httplug.client.acme.plugin.header_remove',
157
            'httplug.client.acme.plugin.query_defaults',
158
            'httplug.client.acme.authentication.my_basic',
159
            'httplug.client.acme.plugin.cache',
160
        ];
161
        $pluginReferences = array_map(function ($id) {
162
            return new Reference($id);
163
        }, $plugins);
164
165
        $this->assertContainerBuilderHasService('httplug.client.acme');
166
        foreach ($plugins as $id) {
167
            $this->assertContainerBuilderHasService($id);
168
        }
169
        $this->assertContainerBuilderHasServiceDefinitionWithArgument('httplug.client.acme', 1, $pluginReferences);
170
        $this->assertContainerBuilderHasService('httplug.client.mock');
171
    }
172
173
    /**
174
     * @group legacy
175
     */
176 View Code Duplication
    public function testNoProfilingWhenToolbarIsDisabled(): void
177
    {
178
        $this->load(
179
            [
180
                'toolbar' => [
181
                    'enabled' => false,
182
                ],
183
                'clients' => [
184
                    'acme' => [
185
                        'factory' => 'httplug.factory.curl',
186
                        'plugins' => ['foo'],
187
                    ],
188
                ],
189
            ]
190
        );
191
192
        $this->verifyProfilingDisabled();
193
    }
194
195 View Code Duplication
    public function testNoProfilingWhenNotInDebugMode(): void
196
    {
197
        $this->setParameter('kernel.debug', false);
198
        $this->load(
199
            [
200
                'clients' => [
201
                    'acme' => [
202
                        'factory' => 'httplug.factory.curl',
203
                        'plugins' => ['foo'],
204
                    ],
205
                ],
206
            ]
207
        );
208
209
        $this->verifyProfilingDisabled();
210
    }
211
212
    /**
213
     * @group legacy
214
     */
215 View Code Duplication
    public function testProfilingWhenToolbarIsSpecificallyOn(): void
216
    {
217
        $this->setParameter('kernel.debug', false);
218
        $this->load(
219
            [
220
                'toolbar' => [
221
                    'enabled' => true,
222
                ],
223
                'clients' => [
224
                    'acme' => [
225
                        'factory' => 'httplug.factory.curl',
226
                        'plugins' => ['foo'],
227
                    ],
228
                ],
229
            ]
230
        );
231
232
        $this->assertContainerBuilderHasService(PluginClientFactoryListener::class);
233
    }
234
235
    public function testOverrideProfilingFormatter(): void
236
    {
237
        $this->load(
238
            [
239
                'profiling' => [
240
                    'formatter' => 'acme.formatter',
241
                ],
242
            ]
243
        );
244
245
        $def = $this->container->findDefinition('httplug.collector.formatter');
246
        $this->assertEquals('acme.formatter', (string) $def->getArgument(0));
247
    }
248
249 View Code Duplication
    public function testCachePluginConfigCacheKeyGeneratorReference(): void
250
    {
251
        $this->load([
252
            'plugins' => [
253
                'cache' => [
254
                    'cache_pool' => 'my_cache_pool',
255
                    'config' => [
256
                        'cache_key_generator' => 'header_cache_key_generator',
257
                    ],
258
                ],
259
            ],
260
        ]);
261
262
        $cachePlugin = $this->container->findDefinition('httplug.plugin.cache');
263
264
        $config = $cachePlugin->getArgument(2);
265
        $this->assertArrayHasKey('cache_key_generator', $config);
266
        $this->assertInstanceOf(Reference::class, $config['cache_key_generator']);
267
        $this->assertSame('header_cache_key_generator', (string) $config['cache_key_generator']);
268
    }
269
270 View Code Duplication
    public function testCachePluginConfigCacheListenersDefinition(): void
271
    {
272
        $this->load([
273
            'plugins' => [
274
                'cache' => [
275
                    'cache_pool' => 'my_cache_pool',
276
                    'config' => [
277
                        'cache_listeners' => [
278
                            'httplug.plugin.cache.listeners.add_header',
279
                        ],
280
                    ],
281
                ],
282
            ],
283
        ]);
284
285
        $cachePlugin = $this->container->findDefinition('httplug.plugin.cache');
286
287
        $config = $cachePlugin->getArgument(2);
288
        $this->assertArrayHasKey('cache_listeners', $config);
289
        $this->assertContainsOnlyInstancesOf(Reference::class, $config['cache_listeners']);
290
        $this->assertSame('httplug.plugin.cache.listeners.add_header', (string) $config['cache_listeners'][0]);
291
    }
292
293
    public function testContentTypePluginAllowedOptions(): void
294
    {
295
        $this->load([
296
            'clients' => [
297
                'acme' => [
298
                    'plugins' => [
299
                        [
300
                            'content_type' => [
301
                                'skip_detection' => true,
302
                                'size_limit' => 200000,
303
                            ],
304
                        ],
305
                    ],
306
                ],
307
            ],
308
        ]);
309
310
        $cachePlugin = $this->container->findDefinition('httplug.client.acme.plugin.content_type');
311
312
        $config = $cachePlugin->getArgument(0);
313
        $this->assertEquals([
314
            'skip_detection' => true,
315
            'size_limit' => 200000,
316
        ], $config);
317
    }
318
319
    public function testUsingServiceKeyForClients(): void
320
    {
321
        $this->load([
322
            'clients' => [
323
                'acme' => [
324
                    'service' => 'my_custom_client',
325
                ],
326
            ],
327
        ]);
328
329
        $client = $this->container->getAlias('httplug.client.acme.client');
330
        $this->assertEquals('my_custom_client', (string) $client);
331
        $this->assertFalse($client->isPublic());
332
    }
333
334
    private function verifyProfilingDisabled(): void
335
    {
336
        $def = $this->container->findDefinition('httplug.client');
337
        $this->assertTrue(is_subclass_of($def->getClass(), HttpClient::class));
338
        $arguments = $def->getArguments();
339
340
        if (isset($arguments[3])) {
341
            $this->assertEmpty(
342
                $arguments[3],
343
                'Parameter 3 to the PluginClient must not contain any debug_plugin information when profiling is disabled'
344
            );
345
        }
346
    }
347
348
    public function testClientShouldHaveDefaultVisibility(): void
349
    {
350
        $this->load([
351
            'clients' => [
352
                'acme' => [],
353
            ],
354
        ]);
355
356
        $this->assertContainerBuilderHasService('httplug.client.acme');
357
358
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
359
            // Symfony made services private by default starting from 3.4
360
            $this->assertTrue($this->container->getDefinition('httplug.client.acme')->isPrivate());
361
        } else {
362
            // Legacy Symfony
363
            $this->assertTrue($this->container->getDefinition('httplug.client.acme')->isPublic());
364
        }
365
    }
366
367 View Code Duplication
    public function testFlexibleClientShouldBePrivateByDefault(): void
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
368
    {
369
        $this->load([
370
            'clients' => [
371
                'acme' => [
372
                    'flexible_client' => true,
373
                ],
374
            ],
375
        ]);
376
377
        $this->assertContainerBuilderHasService('httplug.client.acme');
378
        $this->assertFalse($this->container->getDefinition('httplug.client.acme.flexible')->isPublic());
379
    }
380
381 View Code Duplication
    public function testHttpMethodsClientShouldBePrivateByDefault(): void
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
382
    {
383
        $this->load([
384
            'clients' => [
385
                'acme' => [
386
                    'http_methods_client' => true,
387
                ],
388
            ],
389
        ]);
390
391
        $this->assertContainerBuilderHasService('httplug.client.acme');
392
        $this->assertFalse($this->container->getDefinition('httplug.client.acme.http_methods')->isPublic());
393
    }
394
395 View Code Duplication
    public function testBatchClientShouldBePrivateByDefault(): void
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
396
    {
397
        $this->load([
398
            'clients' => [
399
                'acme' => [
400
                    'batch_client' => true,
401
                ],
402
            ],
403
        ]);
404
405
        $this->assertContainerBuilderHasService('httplug.client.acme');
406
        $this->assertFalse($this->container->getDefinition('httplug.client.acme.batch_client')->isPublic());
407
    }
408
409 View Code Duplication
    public function testClientCanBePublic(): void
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
410
    {
411
        $this->load([
412
            'clients' => [
413
                'acme' => [
414
                    'public' => true,
415
                ],
416
            ],
417
        ]);
418
419
        $this->assertContainerBuilderHasService('httplug.client.acme');
420
        $this->assertTrue($this->container->getDefinition('httplug.client.acme')->isPublic());
421
422
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
423
            // Symfony made services private by default starting from 3.4
424
            $this->assertFalse($this->container->getDefinition('httplug.client.acme')->isPrivate());
425
        }
426
    }
427
428 View Code Duplication
    public function testFlexibleClientCanBePublic(): void
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
429
    {
430
        $this->load([
431
            'clients' => [
432
                'acme' => [
433
                    'public' => true,
434
                    'flexible_client' => true,
435
                ],
436
            ],
437
        ]);
438
439
        $this->assertContainerBuilderHasService('httplug.client.acme');
440
        $this->assertTrue($this->container->getDefinition('httplug.client.acme.flexible')->isPublic());
441
442
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
443
            // Symfony made services private by default starting from 3.4
444
            $this->assertFalse($this->container->getDefinition('httplug.client.acme.flexible')->isPrivate());
445
        }
446
    }
447
448 View Code Duplication
    public function testHttpMethodsClientCanBePublic(): void
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
449
    {
450
        $this->load([
451
            'clients' => [
452
                'acme' => [
453
                    'public' => true,
454
                    'http_methods_client' => true,
455
                ],
456
            ],
457
        ]);
458
459
        $this->assertContainerBuilderHasService('httplug.client.acme');
460
        $this->assertTrue($this->container->getDefinition('httplug.client.acme.http_methods')->isPublic());
461
462
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
463
            // Symfony made services private by default starting from 3.4
464
            $this->assertFalse($this->container->getDefinition('httplug.client.acme.http_methods')->isPrivate());
465
        }
466
    }
467
468 View Code Duplication
    public function testBatchClientCanBePublic(): void
469
    {
470
        $this->load([
471
            'clients' => [
472
                'acme' => [
473
                    'public' => true,
474
                    'batch_client' => true,
475
                ],
476
            ],
477
        ]);
478
479
        $this->assertContainerBuilderHasService('httplug.client.acme');
480
        $this->assertTrue($this->container->getDefinition('httplug.client.acme.batch_client')->isPublic());
481
482
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
483
            // Symfony made services private by default starting from 3.4
484
            $this->assertFalse($this->container->getDefinition('httplug.client.acme.batch_client')->isPrivate());
485
        }
486
    }
487
488
    public function testClientIsTaggedWithHttplugClientTag(): void
489
    {
490
        $this->load([
491
            'clients' => [
492
                'acme' => null,
493
            ],
494
        ]);
495
496
        $serviceId = 'httplug.client.acme';
497
498
        $this->assertContainerBuilderHasService($serviceId);
499
500
        $this->assertTrue($this->container->getDefinition($serviceId)->hasTag(HttplugExtension::HTTPLUG_CLIENT_TAG), sprintf(
501
            'Failed asserting that client with service identifier "%s" has been tagged with "%s".',
502
            $serviceId,
503
            HttplugExtension::HTTPLUG_CLIENT_TAG
504
        ));
505
    }
506
507
    /**
508
     * @dataProvider provideVcrPluginConfig
509
     * @group vcr-plugin
510
     */
511
    public function testVcrPluginConfiguration(array $config, array $services, array $arguments = []): void
512
    {
513
        if (!class_exists(InMemoryRecorder::class)) {
514
            $this->markTestSkipped('VCR plugin is not installed.');
515
        }
516
517
        $prefix = 'httplug.client.acme.vcr';
518
        $this->load(['clients' => ['acme' => ['plugins' => [['vcr' => $config]]]]]);
519
        $this->assertContainerBuilderHasService('httplug.plugin.vcr.recorder.in_memory', InMemoryRecorder::class);
520
521
        foreach ($services as $service) {
522
            $this->assertContainerBuilderHasService($prefix.'.'.$service);
523
        }
524
525
        foreach ($arguments as $id => $args) {
526
            foreach ($args as $index => $value) {
527
                $this->assertContainerBuilderHasServiceDefinitionWithArgument($prefix.'.'.$id, $index, $value);
528
            }
529
        }
530
    }
531
532
    /**
533
     * @group vcr-plugin
534
     */
535
    public function testIsNotLoadedUnlessNeeded(): void
536
    {
537
        if (!class_exists(InMemoryRecorder::class)) {
538
            $this->markTestSkipped('VCR plugin is not installed.');
539
        }
540
541
        $this->load(['clients' => ['acme' => ['plugins' => []]]]);
542
        $this->assertContainerBuilderNotHasService('httplug.plugin.vcr.recorder.in_memory');
543
    }
544
545
    public function provideVcrPluginConfig()
546
    {
547
        $config = [
548
            'mode' => 'record',
549
            'recorder' => 'in_memory',
550
            'naming_strategy' => 'app.naming_strategy',
551
        ];
552
        yield [$config, ['record']];
553
554
        $config['mode'] = 'replay';
555
        yield [$config, ['replay']];
556
557
        $config['mode'] = 'replay_or_record';
558
        yield [$config, ['replay', 'record']];
559
560
        $config['recorder'] = 'filesystem';
561
        $config['fixtures_directory'] = __DIR__;
562
        unset($config['naming_strategy']);
563
564
        yield [$config, ['replay', 'record', 'recorder', 'naming_strategy'], ['replay' => [2 => false]]];
565
566
        $config['naming_strategy_options'] = [
567
            'hash_headers' => ['X-FOO'],
568
            'hash_body_methods' => ['PATCH'],
569
        ];
570
571
        yield [
572
            $config,
573
            ['replay', 'record', 'recorder', 'naming_strategy'],
574
            ['naming_strategy' => [$config['naming_strategy_options']]],
575
        ];
576
    }
577
}
578