Issues (19)

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/Component/Http/Request.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
3
/*
4
 * This file is part of the "Kata 1" package.
5
 *
6
 * Copyright (c) Daniel González
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @author Daniel González <[email protected]>
12
 */
13
14
namespace Component\Http;
15
16
/**
17
 * Request.
18
 */
19
class Request
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $method;
25
26
    /**
27
     * @var array
28
     */
29
    protected $get;
30
31
    /**
32
     * @var array
33
     */
34
    protected $post;
35
36
    /**
37
     * @var array
38
     */
39
    protected $server;
40
41
    /**
42
     * @var array
43
     */
44
    protected $cookie;
45
46
    /**
47
     * @var array
48
     */
49
    protected $headers;
50
51
    /**
52
     * @var string
53
     */
54
    protected $uri = '/';
55
56
    /**
57
     * @param string $method
58
     * @param array  $get
59
     * @param array  $post
60
     * @param array  $server
61
     * @param array  $cookie
62
     * @param array  $headers
63
     */
64 28
    public function __construct(
65
        $method,
66
        array $get = [],
67
        array $post = [],
68
        array $server = [],
69
        array $cookie = [],
70
        array $headers = []
71
    ) {
72 28
        $this->method = $method;
73 28
        $this->get = $get;
74 28
        $this->post = $post;
75 28
        $this->server = $server;
76 28
        $this->cookie = $cookie;
77 28
        foreach ($headers as $key => $value) {
78 5
            $this->addHeader($key, $value);
79 28
        }
80
81 28
        $path = $this->get('server', 'REQUEST_URI');
82 28
        $parsed = parse_url($path);
83 28
        if (isset($parsed['path'])) {
84 28
            $this->uri = $parsed['path'];
85 28
        }
86 28
    }
87
88 1
    public static function createFromGlobals()
0 ignored issues
show
createFromGlobals uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
createFromGlobals uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
createFromGlobals uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
createFromGlobals uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
89
    {
90 1
        $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
91 1
        $headers = [];
92 1
        foreach ($_SERVER as $name => $value) {
93 1
            if (substr($name, 0, 5) == 'HTTP_') {
94
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
95
            }
96 1
        }
97
98 1
        return new static($method, $_GET, $_POST, $_SERVER, $_COOKIE, $headers);
99
    }
100
101
    /**
102
     * @return string
103
     */
104 19
    public function getMethod()
105
    {
106 19
        return $this->method;
107
    }
108
109
    /**
110
     * @return string
111
     */
112 17
    public function getUri()
113
    {
114 17
        return $this->uri;
115
    }
116
117
    /**
118
     * @param string $source
119
     * @param string $key
120
     * @param mixed  $default
121
     */
122 28
    public function get($source, $key, $default = false)
123
    {
124 28
        if (!in_array($source, ['get', 'post', 'server', 'cookie'])) {
125
            return;
126
        }
127 28
        $data = $this->$source;
128 28
        if (isset($data[$key])) {
129 23
            return $data[$key];
130
        }
131
132 10
        return $default;
133
    }
134
135
    /**
136
     * @param string $key
137
     *
138
     * @return string
139
     */
140 9 View Code Duplication
    public function getHeader($key)
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...
141
    {
142 9
        $key = $this->normalizeHeaderName($key);
143 9
        if (isset($this->headers[$key])) {
144 5
            return $this->headers[$key];
145
        }
146 7
    }
147
148
    /**
149
     * @param string $key
150
     * @param string $value
151
     */
152 5
    public function addHeader($key, $value)
153
    {
154 5
        $this->headers[$this->normalizeHeaderName($key)] = $value;
155 5
    }
156
157
    /**
158
     * @param string $key
159
     *
160
     * @return string
161
     */
162 9
    protected function normalizeHeaderName($key)
163
    {
164 9
        return ucwords(str_replace('_', '-', strtolower($key)), '-');
165
    }
166
}
167