Issues (5)

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/Policies/Policy.php (3 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 Spatie\Csp\Policies;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use ReflectionClass;
9
use Spatie\Csp\Directive;
10
use Spatie\Csp\Exceptions\InvalidDirective;
11
use Spatie\Csp\Exceptions\InvalidValueSet;
12
use Spatie\Csp\Keyword;
13
use Spatie\Csp\Value;
14
use Symfony\Component\HttpFoundation\Response;
15
16
abstract class Policy
17
{
18
    protected $directives = [];
19
20
    protected $reportOnly = false;
21
22
    abstract public function configure();
23
24
    /**
25
     * @param string $directive
26
     * @param string|array|bool $values
27
     *
28
     * @return \Spatie\Csp\Policies\Policy
29
     *
30
     * @throws \Spatie\Csp\Exceptions\InvalidDirective
31
     * @throws \Spatie\Csp\Exceptions\InvalidValueSet
32
     */
33
    public function addDirective(string $directive, $values): self
34
    {
35
        $this->guardAgainstInvalidDirectives($directive);
36
        $this->guardAgainstInvalidValues(Arr::wrap($values));
37
38
        if ($values === Value::NO_VALUE) {
39
            $this->directives[$directive][] = Value::NO_VALUE;
40
41
            return $this;
42
        }
43
44
        $values = array_filter(Arr::flatten(array_map(function ($value) {
0 ignored issues
show
array_map(function ($val...ort\Arr::wrap($values)) is of type array, but the function expects a object<Illuminate\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
            return explode(' ', $value);
46
        }, Arr::wrap($values))));
47
48
        if (in_array(Keyword::NONE, $values, true)) {
49
            $this->directives[$directive] = [$this->sanitizeValue(Keyword::NONE)];
50
51
            return $this;
52
        }
53
54
        $this->directives[$directive] = array_filter($this->directives[$directive] ?? [], function ($value) {
55
            return $value !== $this->sanitizeValue(Keyword::NONE);
56
        });
57
58
        foreach ($values as $value) {
59
            $sanitizedValue = $this->sanitizeValue($value);
60
61
            if (! in_array($sanitizedValue, $this->directives[$directive] ?? [])) {
62
                $this->directives[$directive][] = $sanitizedValue;
63
            }
64
        }
65
66
        return $this;
67
    }
68
69
    public function reportOnly(): self
70
    {
71
        $this->reportOnly = true;
72
73
        return $this;
74
    }
75
76
    public function enforce(): self
77
    {
78
        $this->reportOnly = false;
79
80
        return $this;
81
    }
82
83
    public function reportTo(string $uri): self
84
    {
85
        $this->directives['report-uri'] = [$uri];
86
87
        return $this;
88
    }
89
90
    public function shouldBeApplied(Request $request, Response $response): bool
0 ignored issues
show
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
    {
92
        return config('csp.enabled');
93
    }
94
95
    public function addNonceForDirective(string $directive): self
96
    {
97
        return $this->addDirective($directive, "'nonce-".app('csp-nonce')."'");
98
    }
99
100
    public function applyTo(Response $response)
101
    {
102
        $this->configure();
103
104
        $headerName = $this->reportOnly
105
            ? 'Content-Security-Policy-Report-Only'
106
            : 'Content-Security-Policy';
107
108
        if ($response->headers->has($headerName)) {
109
            return;
110
        }
111
112
        $response->headers->set($headerName, (string) $this);
113
    }
114
115
    public function __toString()
116
    {
117
        return collect($this->directives)
118
            ->map(function (array $values, string $directive) {
119
                $valueString = implode(' ', $values);
120
121
                return empty($valueString) ? "{$directive}" : "{$directive} {$valueString}";
122
            })
123
            ->implode(';');
124
    }
125
126
    protected function guardAgainstInvalidDirectives(string $directive)
127
    {
128
        if (! Directive::isValid($directive)) {
129
            throw InvalidDirective::notSupported($directive);
130
        }
131
    }
132
133
    protected function guardAgainstInvalidValues(array $values)
134
    {
135
        if (in_array(Keyword::NONE, $values, true) && count($values) > 1) {
136
            throw InvalidValueSet::noneMustBeOnlyValue();
137
        }
138
    }
139
140
    protected function isHash(string $value): bool
141
    {
142
        $acceptableHashingAlgorithms = [
143
            'sha256-',
144
            'sha384-',
145
            'sha512-',
146
        ];
147
148
        return Str::startsWith($value, $acceptableHashingAlgorithms);
149
    }
150
151
    protected function isKeyword(string $value): bool
152
    {
153
        $keywords = (new ReflectionClass(Keyword::class))->getConstants();
154
155
        return in_array($value, $keywords);
156
    }
157
158
    protected function sanitizeValue(string $value): string
159
    {
160
        if (
161
            $this->isKeyword($value)
162
            || $this->isHash($value)
163
        ) {
164
            return "'{$value}'";
165
        }
166
167
        return $value;
168
    }
169
}
170