Issues (23)

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/Middleware/Https.php (2 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
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Middleware to redirect to https protocol.
11
 */
12
class Https
13
{
14
    use Utils\RedirectTrait;
15
16
    const HEADER = 'Strict-Transport-Security';
17
18
    /**
19
     * @var bool Add or remove https
20
     */
21
    private $addHttps;
22
23
    /**
24
     * @param int One year by default
25
     */
26
    private $maxAge = 31536000;
27
28
    /**
29
     * @param bool Whether include subdomains
30
     */
31
    private $includeSubdomains = false;
32
33
    /**
34
     * @param bool Whether check the headers
35
     */
36
    private $checkHttpsForward = false;
37
38
    /**
39
     * Set basic config.
40
     *
41
     * @param bool $addHttps
42
     */
43
    public function __construct($addHttps = true)
44
    {
45
        $this->addHttps = (bool) $addHttps;
46
        $this->redirect(301);
47
    }
48
49
    /**
50
     * Configure the max-age HSTS in seconds.
51
     *
52
     * @param int $maxAge
53
     *
54
     * @return self
55
     */
56
    public function maxAge($maxAge)
57
    {
58
        $this->maxAge = $maxAge;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Configure the includeSubDomains HSTS directive.
65
     *
66
     * @param bool $includeSubdomains
67
     *
68
     * @return self
69
     */
70
    public function includeSubdomains($includeSubdomains = true)
71
    {
72
        $this->includeSubdomains = $includeSubdomains;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Configure whether check the following headers before redirect:
79
     * X-Forwarded-Proto: https
80
     * X-Forwarded-Port: 443.
81
     *
82
     * @param bool $checkHttpsForward
83
     *
84
     * @return self
85
     */
86
    public function checkHttpsForward($checkHttpsForward = true)
87
    {
88
        $this->checkHttpsForward = $checkHttpsForward;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Execute the middleware.
95
     *
96
     * @param ServerRequestInterface $request
97
     * @param ResponseInterface      $response
98
     * @param callable               $next
99
     *
100
     * @return ResponseInterface
101
     */
102
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
103
    {
104
        $uri = $request->getUri();
105
106
        if ($this->addHttps) {
107 View Code Duplication
            if (strtolower($uri->getScheme()) !== 'https') {
0 ignored issues
show
This code seems to be duplicated across 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...
108
                $uri = $uri->withScheme('https')->withPort(443);
109
110
                if ($this->redirectStatus !== false && (!$this->checkHttpsForward || ($request->getHeaderLine('X-Forwarded-Proto') !== 'https' && $request->getHeaderLine('X-Forwarded-Port') !== '443'))) {
111
                    return $this->getRedirectResponse($request, $uri, $response);
112
                }
113
114
                $request = $request->withUri($uri);
115
            }
116
117
            if (!empty($this->maxAge)) {
118
                $response = $response->withHeader(self::HEADER, sprintf('max-age=%d%s', $this->maxAge, $this->includeSubdomains ? ';includeSubDomains' : ''));
119
            }
120 View Code Duplication
        } else {
0 ignored issues
show
This code seems to be duplicated across 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...
121
            if (strtolower($uri->getScheme()) !== 'http') {
122
                $uri = $uri->withScheme('http')->withPort(80);
123
124
                if ($this->redirectStatus !== false && (!$this->checkHttpsForward || ($request->getHeaderLine('X-Forwarded-Proto') !== 'http' && $request->getHeaderLine('X-Forwarded-Port') !== '80'))) {
125
                    return $this->getRedirectResponse($request, $uri, $response);
126
                }
127
128
                $request = $request->withUri($uri);
129
            }
130
        }
131
132
        $response = $next($request, $response);
133
134
        if (Utils\Helpers::isRedirect($response)) {
135
            if ($this->addHttps) {
136
                return $response->withHeader('Location', str_replace('http://', 'https://', $response->getHeaderLine('Location')));
137
            }
138
139
            return $response->withHeader('Location', str_replace('https://', 'http://', $response->getHeaderLine('Location')));
140
        }
141
142
        return $response;
143
    }
144
}
145