Failed Conditions
Pull Request — newinternal-releasecandidate (#544)
by Simon
13:56 queued 04:02
created

ContentSecurityPolicyManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Security;
10
11
use Waca\SiteConfiguration;
0 ignored issues
show
Bug introduced by
The type Waca\SiteConfiguration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class ContentSecurityPolicyManager
14
{
15
    private $policy = [
16
        'default-src'     => [],
17
        'script-src'      => ['self', 'nonce'],
18
        'script-src-elem' => ['self', 'nonce'],
19
        'script-src-attr' => [],
20
        'connect-src'     => ['self'],
21
        'style-src'       => ['self'],
22
        'style-src-elem'  => ['self'],
23
        'style-src-attr'  => [],
24
        'img-src'         => ['self', 'data:', 'https://upload.wikimedia.org', 'https://accounts-dev.wmflabs.org/'],
25
        'font-src'        => ['self'],
26
        'form-action'     => ['self', 'oauth'],
27
        'frame-ancestors' => [],
28
    ];
29
    private $nonce = null;
30
    private $reportOnly = false;
31
    /**
32
     * @var SiteConfiguration
33
     */
34
    private $configuration;
35
36
    /**
37
     * ContentSecurityPolicyManager constructor.
38
     *
39
     * @param SiteConfiguration $configuration
40
     */
41
    public function __construct(SiteConfiguration $configuration)
42
    {
43
        $this->configuration = $configuration;
44
    }
45
46
    public function getNonce()
47
    {
48
        if ($this->nonce === null) {
49
            $this->nonce = base64_encode(openssl_random_pseudo_bytes(32));
50
        }
51
52
        return $this->nonce;
53
    }
54
55
    public function getHeader(): string
56
    {
57
        $reportOnly = '';
58
        if ($this->reportOnly) {
59
            $reportOnly = '-Report-Only';
60
        }
61
62
        $constructedPolicy = "Content-Security-Policy{$reportOnly}: ";
63
64
        foreach ($this->policy as $item => $values) {
65
            $constructedPolicy .= $item . ' ';
66
67
            if (count($values) > 0) {
68
                foreach ($values as $value) {
69
                    switch ($value) {
70
                        case 'none':
71
                        case 'self':
72
                        case 'strict-dynamic':
73
                            $constructedPolicy .= "'{$value}' ";
74
                            break;
75
                        case 'nonce':
76
                            if ($this->nonce !== null) {
77
                                $constructedPolicy .= "'nonce-{$this->nonce}' ";
78
                            }
79
                            break;
80
                        case 'oauth':
81
                            $constructedPolicy .= "{$this->configuration->getOauthMediaWikiCanonicalServer()} ";
82
                            break;
83
                        default:
84
                            $constructedPolicy .= $value . ' ';
85
                            break;
86
                    }
87
                }
88
            }
89
            else {
90
                $constructedPolicy .= "'none' ";
91
            }
92
93
            $constructedPolicy .= '; ';
94
        }
95
96
        if ($this->configuration->getCspReportUri() !== null) {
97
            $constructedPolicy .= 'report-uri ' . $this->configuration->getCspReportUri();
98
        }
99
100
        return $constructedPolicy;
101
    }
102
}
103