Issues (6)

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/MockOAuth2Server.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 CommerceGuys\Guzzle\Oauth2\Tests;
4
5
use GuzzleHttp\Ring\Client\MockHandler;
6
7
class MockOAuth2Server
8
{
9
    /** @var array */
10
    protected $options;
11
12
    public function __construct(array $options = [])
13
    {
14
        $defaults = [
15
            'tokenExpiresIn' => 3600,
16
            'tokenPath' => '/oauth2/token',
17
        ];
18
        $this->options = $options + $defaults;
19
    }
20
21
    /**
22
     * @return MockHandler
23
     */
24
    public function getHandler()
25
    {
26
        return new MockHandler(function (array $request) {
27
            return $this->getResult($request);
28
        });
29
    }
30
31
    /**
32
     * @param array $request
33
     *
34
     * @return array
35
     */
36
    protected function getResult(array $request)
37
    {
38
        if ($request['uri'] === $this->options['tokenPath']) {
39
            $response = $this->oauth2Token($request);
40
        } elseif (strpos($request['uri'], 'api/') !== false) {
41
            $response = $this->mockApiCall($request);
42
        }
43
        if (!isset($response)) {
44
            throw new \RuntimeException("Mock server cannot handle given request URI");
45
        }
46
47
        return $response;
48
    }
49
50
    /**
51
     * @param array $request
52
     *
53
     * @return array
54
     */
55
    protected function oauth2Token(array $request)
56
    {
57
        /** @var \GuzzleHttp\Post\PostBody $body */
58
        $body = $request['body'];
59
        $requestBody = $body->getFields();
60
        $grantType = $requestBody['grant_type'];
61
        switch ($grantType) {
62
            case 'password':
63
                return $this->grantTypePassword($requestBody);
64
65
            case 'client_credentials':
66
                return $this->grantTypeClientCredentials($request);
67
68
            case 'refresh_token':
69
                return $this->grantTypeRefreshToken($requestBody);
70
71
            case 'urn:ietf:params:oauth:grant-type:jwt-bearer':
72
                return $this->grantTypeJwtBearer($requestBody);
0 ignored issues
show
It seems like $requestBody defined by $body->getFields() on line 59 can also be of type string; however, CommerceGuys\Guzzle\Oaut...r::grantTypeJwtBearer() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
73
        }
74
        throw new \RuntimeException("Test grant type not implemented: $grantType");
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    protected function validTokenResponse()
81
    {
82
        $token = [
83
            'access_token' => 'testToken',
84
            'refresh_token' => 'testRefreshTokenFromServer',
85
            'token_type' => 'bearer',
86
        ];
87
88
        if (isset($this->options['tokenExpires'])) {
89
            $token['expires'] = $this->options['tokenExpires'];
90
        } elseif (isset($this->options['tokenExpiresIn'])) {
91
            $token['expires_in'] = $this->options['tokenExpiresIn'];
92
        }
93
94
        return [
95
            'status' => 200,
96
            'body' => json_encode($token),
97
        ];
98
    }
99
100
    /**
101
     * @param array $requestBody
102
     *
103
     * @return array
104
     *   The response as expected by the MockHandler.
105
     */
106
    protected function grantTypePassword(array $requestBody)
107
    {
108
        if ($requestBody['username'] != 'validUsername' || $requestBody['password'] != 'validPassword') {
109
            // @todo correct response headers
110
            return ['status' => 401];
111
        }
112
113
        return $this->validTokenResponse();
114
    }
115
116
    /**
117
     * @param array $request
118
     *
119
     * @return array
120
     *   The response as expected by the MockHandler.
121
     */
122
    protected function grantTypeClientCredentials(array $request)
123
    {
124
        if ($request['client']['auth'][1] != 'testSecret') {
125
            // @todo correct response headers
126
            return ['status' => 401];
127
        }
128
129
        return $this->validTokenResponse();
130
    }
131
132
    /**
133
     * @param array $requestBody
134
     *
135
     * @return array
136
     */
137
    protected function grantTypeRefreshToken(array $requestBody)
138
    {
139
        if ($requestBody['refresh_token'] != 'testRefreshToken') {
140
            return ['status' => 401];
141
        }
142
143
        return $this->validTokenResponse();
144
    }
145
146
    /**
147
     * @param array $requestBody
148
     *
149
     * @return array
150
     */
151
    protected function grantTypeJwtBearer(array $requestBody)
152
    {
153
        if (!array_key_exists('assertion', $requestBody)) {
154
            return ['status' => 401];
155
        }
156
157
        return $this->validTokenResponse();
158
    }
159
160
    /**
161
     * @param array $request
162
     *
163
     * @return array
164
     */
165
    protected function mockApiCall(array $request)
166
    {
167
        if (!isset($request['headers']['Authorization']) || $request['headers']['Authorization'][0] != 'Bearer testToken') {
168
            return ['status' => 401];
169
        }
170
171
        return ['status' => 200, 'body' => json_encode('Hello World!')];
172
    }
173
}
174