Completed
Push — newinternal-releasecandidate ( 45827b...b95206 )
by Simon
08:27
created

ContentSecurityPolicyManager::getNonce()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 0
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
class ContentSecurityPolicyManager
12
{
13
    private $policy = [
14
        'default-src'     => [],
15
        'script-src-elem' => ['self', 'nonce'],
16
        'connect-src'     => ['self'],
17
        'style-src-elem'  => ['self'],
18
        'img-src'         => ['self', 'data:', 'https://upload.wikimedia.org'],
19
        'font-src'        => ['self'],
20
        'form-action'     => ['self'],
21
        'frame-ancestors' => [],
22
    ];
23
24
    private $nonce = null;
25
    private $reportOnly = false;
26
27
    public function getNonce()
28
    {
29
        if($this->nonce === null) {
30
            $this->nonce = base64_encode(openssl_random_pseudo_bytes(32));
31
        }
32
33
        return $this->nonce;
34
    }
35
36
    public function getHeader() : string
37
    {
38
        $reportOnly = '';
39
        if($this->reportOnly) {
40
            $reportOnly = '-Report-Only';
41
        }
42
43
        $constructedPolicy = "Content-Security-Policy{$reportOnly}: ";
44
45
        foreach ($this->policy as $item => $values) {
46
            $constructedPolicy .= $item . ' ';
47
48
            if (count($values) > 0) {
49
                foreach ($values as $value) {
50
                    switch ($value) {
51
                        case 'none':
52
                        case 'self':
53
                        case 'strict-dynamic':
54
                            $constructedPolicy .= "'{$value}' ";
55
                            break;
56
                        case 'nonce':
57
                            if($this->nonce !== null) {
58
                                $constructedPolicy .= "'nonce-{$this->nonce}' ";
59
                            }
60
                            break;
61
                        default:
62
                            $constructedPolicy .= $value . ' ';
63
                            break;
64
                    }
65
                }
66
            }
67
            else {
68
                $constructedPolicy .= "'none' ";
69
            }
70
71
            $constructedPolicy .= '; ';
72
        }
73
74
        return $constructedPolicy;
75
    }
76
}
77