Issues (26)

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.

tests/SafeRequests/IsSafeHttpMethodTest.php (5 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
declare(strict_types=1);
3
4
namespace TheCodingMachine\Middlewares\SafeRequests;
5
6
use PHPUnit\Framework\TestCase;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
/**
11
 * @covers \TheCodingMachine\Middlewares\SafeRequests\IsSafeHttpMethod
12
 */
13
final class IsSafeHttpMethodTest extends TestCase
14
{
15
    /**
16
     * @dataProvider httpMethodsProvider
17
     *
18
     * @param array  $safeMethods
19
     * @param string $httpMethod
20
     * @param bool   $expectedResult
21
     */
22 View Code Duplication
    public function testSafeMethods(array $safeMethods, string $httpMethod, bool $expectedResult)
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...
23
    {
24
        /* @var $request RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
25
        $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
26
27
        $request->expects(self::any())->method('getMethod')->willReturn($httpMethod);
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Psr\Http\Message\RequestInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
28
29
        self::assertSame($expectedResult, (new IsSafeHttpMethod(...$safeMethods))->__invoke($request));
0 ignored issues
show
$request is of type object<Psr\Http\Message\..._MockObject_MockObject>, but the function expects a object<Psr\Http\Message\ServerRequestInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30
    }
31
32
    public function httpMethodsProvider() : array
33
    {
34
        return [
35
            'empty' => [
36
                [],
37
                'GET',
38
                false,
39
            ],
40
            'GET only' => [
41
                ['GET'],
42
                'GET',
43
                true,
44
            ],
45
            'get only' => [
46
                ['get'],
47
                'GET',
48
                true,
49
            ],
50
            'GET only, matching lowercase get' => [
51
                ['GET'],
52
                'get',
53
                true,
54
            ],
55
            'GET only, non-matching method' => [
56
                ['GET'],
57
                'PUT',
58
                false,
59
            ],
60
            'GET, PUT only, matching method' => [
61
                ['GET', 'PUT'],
62
                'PUT',
63
                true,
64
            ],
65
        ];
66
    }
67
68
    /**
69
     * @dataProvider safeDefaultsMatchingProvider
70
     *
71
     * @param string $httpMethod
72
     * @param bool   $expectedResult
73
     */
74 View Code Duplication
    public function testSafeMethodsWithDefaults(string $httpMethod, bool $expectedResult)
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...
75
    {
76
        /* @var $request RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
77
        $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
78
79
        $request->expects(self::any())->method('getMethod')->willReturn($httpMethod);
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Psr\Http\Message\RequestInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
80
81
        self::assertSame($expectedResult, IsSafeHttpMethod::fromDefaultSafeMethods()->__invoke($request));
82
    }
83
84
    public function safeDefaultsMatchingProvider() : array
85
    {
86
        return [
87
            'empty' => [
88
                '',
89
                false,
90
            ],
91
            'GET' => [
92
                'GET',
93
                true,
94
            ],
95
            'get' => [
96
                'get',
97
                true,
98
            ],
99
            'HEAD' => [
100
                'HEAD',
101
                true,
102
            ],
103
            'head' => [
104
                'head',
105
                true,
106
            ],
107
            'OPTIONS' => [
108
                'OPTIONS',
109
                true,
110
            ],
111
            'options' => [
112
                'options',
113
                true,
114
            ],
115
            'DELETE' => [
116
                'DELETE',
117
                false,
118
            ],
119
            'delete' => [
120
                'delete',
121
                false,
122
            ],
123
            'POST' => [
124
                'POST',
125
                false,
126
            ],
127
            'post' => [
128
                'post',
129
                false,
130
            ],
131
            'PUT' => [
132
                'PUT',
133
                false,
134
            ],
135
            'put' => [
136
                'put',
137
                false,
138
            ],
139
            'UNKNOWN' => [
140
                'UNKNOWN',
141
                false,
142
            ],
143
            'unknown' => [
144
                'unknown',
145
                false,
146
            ],
147
        ];
148
    }
149
}
150