Issues (40)

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.

src/Services/AuthenticationService.php (2 issues)

Labels
Severity

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 AdvancedLearning\Oauth2Server\Services;
4
5
6
use AdvancedLearning\Oauth2Server\Exceptions\AuthenticationException;
7
use AdvancedLearning\Oauth2Server\Repositories\AccessTokenRepository;
8
use GuzzleHttp\Psr7\Response;
9
use League\OAuth2\Server\Exception\OAuthServerException;
10
use League\OAuth2\Server\ResourceServer;
11
use Robbie\Psr7\HttpRequestAdapter;
12
use Robbie\Psr7\HttpResponseAdapter;
13
use SilverStripe\Control\Director;
14
use SilverStripe\Control\HTTPRequest;
15
use SilverStripe\Core\Environment;
16
17
class AuthenticationService implements Authenticator
18
{
19
    protected $server;
20
21
    /**
22
     * AuthenticationService constructor.
23
     *
24
     * @param ResourceServer|null $server Optional resource server.
25
     */
26
    public function __construct(ResourceServer $server = null)
27
    {
28
        $this->server = $server ?: $this->createServer();
29
    }
30
31
    /**
32
     * Authenticate the request. Adds oauth fields as headers on the request.
33
     *
34
     * @param HTTPRequest $request The SilverStripe request object to be authenticated.
35
     *
36
     * @return HTTPRequest
37
     * @throws AuthenticationException
38
     */
39
    public function authenticate(HTTPRequest $request): HTTPRequest
40
    {
41
        $requestAdapter = new HttpRequestAdapter();
42
        $responseAdapter = new HttpResponseAdapter();
43
44
        // missing vars (cli)
45
        $this->addMissingServerVariables($requestAdapter);
46
47
        $server = $this->getServer();
48
        $psrRequest = $requestAdapter->toPsr7($request);
49
        $psrResponse = new Response();
50
51
        try {
52
            $psrRequest = $server->validateAuthenticatedRequest($psrRequest);
53
        } catch (OAuthServerException $exception) {
54
            // convert to authentication exception
55
            throw new AuthenticationException(
56
                $exception->getMessage(),
57
                $exception->getCode(),
58
                $responseAdapter->fromPsr7($exception->generateHttpResponse($psrResponse))
0 ignored issues
show
It seems like $responseAdapter->fromPs...Response($psrResponse)) targeting Robbie\Psr7\HttpResponseAdapter::fromPsr7() can also be of type object<SilverStripe\Control\HTTPRequest>; however, AdvancedLearning\Oauth2S...xception::__construct() does only seem to accept object<SilverStripe\Control\HTTPResponse>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
59
            );
60
        } catch (\Exception $exception) {
61
            // convert to authentication exception
62
            throw new AuthenticationException(
63
                $exception->getMessage(),
64
                $exception->getCode(),
65
                $responseAdapter->fromPsr7(
0 ignored issues
show
It seems like $responseAdapter->fromPs...Response($psrResponse)) targeting Robbie\Psr7\HttpResponseAdapter::fromPsr7() can also be of type object<SilverStripe\Control\HTTPRequest>; however, AdvancedLearning\Oauth2S...xception::__construct() does only seem to accept object<SilverStripe\Control\HTTPResponse>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
66
                    (new OAuthServerException($exception->getMessage(), 0, 'unknown_error', 500))
67
                        ->generateHttpResponse($psrResponse)
68
                )
69
            );
70
        }
71
        // add the request attributes as custom auth headers
72
        foreach ($psrRequest->getAttributes() as $attribute => $value) {
73
            $request->addHeader($attribute, $value);
74
        }
75
76
        return $request;
77
    }
78
79
    /**
80
     * Override the default ResourceServer.
81
     *
82
     * @param ResourceServer $v The new ResourceServer to use.
83
     *
84
     * @return $this
85
     */
86
    public function setServer(ResourceServer $v): Authenticator
87
    {
88
        $this->server = $v;
89
        return $this;
90
    }
91
92
    /**
93
     * Get the ResourceServer.
94
     *
95
     * @return ResourceServer
96
     */
97
    public function getServer(): ResourceServer
98
    {
99
        return $this->server;
100
    }
101
102
    /**
103
     * Create a default ResourceServer. Used if one isn't provided.
104
     *
105
     * @return ResourceServer
106
     */
107
    protected function createServer(): ResourceServer
108
    {
109
        // Init our repositories
110
        $accessTokenRepository = new AccessTokenRepository(); // instance of AccessTokenRepositoryInterface
111
112
        // Path to authorization server's public key
113
        $publicKeyPath = Environment::getEnv('OAUTH_PUBLIC_KEY_PATH');
114
115
        // Relative paths to the web root
116
        $publicKeyPath = str_replace('{BASE_DIR}', Director::baseFolder(), $publicKeyPath);
117
118
        // Setup the authorization server
119
        return new ResourceServer(
120
            $accessTokenRepository,
121
            $publicKeyPath
122
        );
123
    }
124
125
    /**
126
     * Cli is missing some $_SERVER variables.
127
     *
128
     * @param HttpRequestAdapter $adapter
129
     */
130
    protected function addMissingServerVariables(HttpRequestAdapter $adapter)
131
    {
132
        $vars = $adapter->getServerVars() ?: [];
133
        $defaults = [
134
            'SERVER_PORT' => 80,
135
            'HTTP_HOST' => Environment::getEnv('SS_BASE_URL')
136
        ];
137
138
        foreach ($defaults as $key => $value) {
139
            if (empty($vars[$key])) {
140
                $vars[$key] = $value;
141
            }
142
        }
143
144
        $adapter->setServerVars($vars);
145
    }
146
}
147