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.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 Psr7Middlewares;
4
5
use Psr\Http\Message\RequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use RuntimeException;
8
use InvalidArgumentException;
9
10
class Middleware
11
{
12
    const KEY = 'Psr7Middlewares\\Middleware';
13
    const STORAGE_KEY = 'STORAGE_KEY';
14
15
    private static $streamFactory;
16
    private static $namespaces = ['Psr7Middlewares\\Middleware\\'];
17
18
    /**
19
     * Register a new namespace.
20
     *
21
     * @param string $namespace
22
     * @param bool   $prepend
23
     */
24
    public static function registerNamespace($namespace, $prepend = false)
25
    {
26
        if (false === $prepend) {
27
            self::$namespaces[] = $namespace;
28
        } else {
29
            array_unshift(self::$namespaces, $namespace);
30
        }
31
    }
32
33
    /**
34
     * Set the stream factory used by some middlewares.
35
     *
36
     * @param callable $streamFactory
37
     */
38
    public static function setStreamFactory(callable $streamFactory)
39
    {
40
        self::$streamFactory = $streamFactory;
41
    }
42
43
    /**
44
     * Set the stream factory used by some middlewares.
45
     *
46
     * @param callable|null
47
     */
48
    public static function getStreamFactory()
49
    {
50
        return self::$streamFactory;
51
    }
52
53
    /**
54
     * Create instances of the middlewares.
55
     *
56
     * @param string $name
57
     * @param array  $args
58
     */
59
    public static function __callStatic($name, $args)
60
    {
61
        foreach (self::$namespaces as $namespace) {
62
            $class = $namespace.ucfirst($name);
63
64
            if (class_exists($class)) {
65
                switch (count($args)) {
66
                    case 0:
67
                        return new $class();
68
69
                    case 1:
70
                        return new $class($args[0]);
71
72
                    default:
73
                        return (new \ReflectionClass($class))->newInstanceArgs($args);
74
                }
75
            }
76
        }
77
78
        throw new RuntimeException("The middleware {$name} does not exist");
79
    }
80
81
    /**
82
     * Create a middleware callable that acts as a "proxy" to a real middleware that must be returned by the given callback.
83
     *
84
     * @param callable|string $basePath The base path in which the middleware is created (optional)
85
     * @param callable        $factory  Takes no argument and MUST return a middleware callable or false
86
     *
87
     * @return callable
88
     */
89
    public static function create($basePath, callable $factory = null)
90
    {
91
        if ($factory === null) {
92
            $factory = $basePath;
93
            $basePath = '';
94
        }
95
96
        if (!is_callable($factory)) {
97
            throw new InvalidArgumentException('Invalid callable provided');
98
        }
99
100
        return function (RequestInterface $request, ResponseInterface $response, callable $next) use ($basePath, $factory) {
101
            $path = rtrim($request->getUri()->getPath(), '/');
102
            $basePath = rtrim($basePath, '/');
0 ignored issues
show
Consider using a different name than the imported variable $basePath, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
103
104
            if ($path === $basePath || strpos($path, "{$basePath}/") === 0) {
105
                $middleware = $factory($request, $response);
106
            } else {
107
                $middleware = false;
108
            }
109
110
            if ($middleware === false) {
111
                return $next($request, $response);
112
            }
113
114
            if (!is_callable($middleware)) {
115
                throw new RuntimeException(sprintf('Factory returned "%s" instead of a callable or FALSE.', gettype($middleware)));
116
            }
117
118
            return $middleware($request, $response, $next);
119
        };
120
    }
121
}
122