Passed
Pull Request — master (#6)
by
unknown
02:43
created

isCSPReportingAvailable()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 39
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
c 0
b 0
f 0
nc 7
nop 0
dl 0
loc 39
rs 8.8333
1
<?php
2
3
namespace Signify\Middleware;
4
5
use Signify\Extensions\SecurityHeaderSiteconfigExtension;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Control\Middleware\HTTPMiddleware;
9
use SilverStripe\Core\ClassInfo;
10
use SilverStripe\Core\Extensible;
11
use SilverStripe\Core\Config\Configurable;
12
use SilverStripe\Dev\TestOnly;
13
use SilverStripe\ORM\DB;
14
use SilverStripe\ORM\DataObject;
15
use SilverStripe\SiteConfig\SiteConfig;
16
17
class SecurityHeaderMiddleware implements HTTPMiddleware
18
{
19
    use Configurable;
20
    use Extensible;
21
22
    /**
23
     * An array of HTTP headers.
24
     * @config
25
     * @var array
26
     */
27
    private static $headers = [
0 ignored issues
show
introduced by
The private property $headers is not used, and could be removed.
Loading history...
28
        'global' => array(),
29
    ];
30
31
    /**
32
     * Whether to automatically add the CMS report endpoint to the CSP config.
33
     * @config
34
     * @var string
35
     */
36
    private static $enable_reporting = true;
0 ignored issues
show
introduced by
The private property $enable_reporting is not used, and could be removed.
Loading history...
37
38
    /**
39
     * The URI to report CSP violations to.
40
     * See routes.yml
41
     * @config
42
     * @var string
43
     */
44
    private static $report_uri = 'cspviolations/report';
0 ignored issues
show
introduced by
The private property $report_uri is not used, and could be removed.
Loading history...
45
46
    /**
47
     * Whether to use the report-to header and CSP directive.
48
     * @config
49
     * @var string
50
     */
51
    private static $use_report_to = false;
0 ignored issues
show
introduced by
The private property $use_report_to is not used, and could be removed.
Loading history...
52
53
    /**
54
     * Whether subdomains should report to the same endpoint.
55
     * @config
56
     * @var string
57
     */
58
    private static $report_to_subdomains = false;
0 ignored issues
show
introduced by
The private property $report_to_subdomains is not used, and could be removed.
Loading history...
59
60
    /**
61
     * The group name for the report-to CSP directive.
62
     * @config
63
     * @var string
64
     */
65
    private static $report_to_group = 'signify-csp-violation';
0 ignored issues
show
introduced by
The private property $report_to_group is not used, and could be removed.
Loading history...
66
67
    /**
68
     * Can isCSPReportingOnly be used safely.
69
     *
70
     * This is not a config option.
71
     *
72
     * @var boolean
73
     */
74
    private static $is_csp_reporting_safe = false;
75
76
77
    public function process(HTTPRequest $request, callable $delegate)
78
    {
79
        $response = $delegate($request);
80
81
        $headersConfig = (array) $this->config()->get('headers');
82
        if (empty($headersConfig['global'])) {
83
            return $response;
84
        }
85
86
        // Disable CSP
87
        if ($this->disableCSP()) {
88
            return $response;
89
        }
90
91
        $headersToSend = $headersConfig['global'];
92
93
        if (!$this->disableReporting() && $this->config()->get('use_report_to')) {
94
            $this->addReportToHeader($headersToSend);
95
        }
96
97
        // Update CSP header.
98
        if (array_key_exists('Content-Security-Policy', $headersToSend)) {
99
            $header = 'Content-Security-Policy';
100
            $headerValue = $headersToSend['Content-Security-Policy'];
101
            // Set report only mode if appropriate.
102
            if ($this->isCSPReportingOnly()) {
103
                unset($headersToSend['Content-Security-Policy']);
104
                $header = 'Content-Security-Policy-Report-Only';
105
            }
106
            // Update CSP header value.
107
            $headersToSend[$header] = $this->updateCspHeader($headerValue);
108
        }
109
        $this->extend('updateHeaders', $headersToSend, $request);
110
111
        // Add headers to response.
112
        foreach ($headersToSend as $header => $value) {
113
            if (empty($value)) {
114
                continue;
115
            }
116
            $value = preg_replace('/\v/', '', $value);
117
            $this->extend('updateHeader', $header, $value, $request);
118
            if ($value) {
119
                $response->addHeader($header, $value);
120
            }
121
        }
122
123
        return $response;
124
    }
125
126
    /**
127
     * Return true if the Disable CSP is checked
128
     *
129
     * @return boolean
130
     */
131
    public function disableCSP()
132
    {
133
        if (
134
            self::isCSPReportingAvailable() &&
135
            SiteConfig::current_site_config()->CSPReportingOnly == SecurityHeaderSiteconfigExtension::CSP_DISABLE
136
        ) {
137
            return true;
138
        }
139
140
        return false;
141
    }
142
143
    /**
144
     * Return true if the Disable reporting is checked
145
     *
146
     * The CMS setting can disable reporting even if the 'enable_reporting' is true
147
     *
148
     * @return boolean
149
     */
150
    public function disableReporting()
151
    {
152
        if (self::isCSPReportingAvailable()) {
153
            return SiteConfig::current_site_config()->CSPReportingOnly == SecurityHeaderSiteconfigExtension::CSP_WITHOUT_REPORTING ||
154
            !$this->config()->get('enable_reporting');
155
        }
156
157
        return false;
158
    }
159
160
    /**
161
     * Returns true if the Content-Security-Policy-Report-Only header should be used.
162
     *
163
     * @return boolean
164
     */
165
    public function isCSPReportingOnly()
166
    {
167
        if (
168
            self::isCSPReportingAvailable() &&
169
            SiteConfig::current_site_config()->CSPReportingOnly == SecurityHeaderSiteconfigExtension::CSP_REPORTING_ONLY
170
        ) {
171
            return true;
172
        }
173
174
        return false;
175
    }
176
177
    protected function getReportURI()
178
    {
179
        return Director::absoluteURL($this->config()->get('report_uri'));
180
    }
181
182
    protected function getIncludeSubdomains()
183
    {
184
        return $this->config()->get('report_to_subdomains');
185
    }
186
187
    protected function getReportToGroup()
188
    {
189
        return $this->config()->get('report_to_group');
190
    }
191
192
    protected function getReportURIDirective()
193
    {
194
        return "report-uri {$this->getReportURI()}";
195
    }
196
197
    protected function getReportToDirective()
198
    {
199
        return "report-to {$this->getReportToGroup()}";
200
    }
201
202
    protected function addReportToHeader(&$headers)
203
    {
204
        if (array_key_exists('Report-To', $headers)) {
205
            $headers['Report-To'] = $headers['Report-To'] . ',' . $this->getReportToHeader();
206
        } else {
207
            $headers['Report-To'] = $this->getReportToHeader();
208
        }
209
    }
210
211
    protected function getReportToHeader()
212
    {
213
        $header = [
214
            'group' => $this->getReportToGroup(),
215
            'max_age' => 1800,
216
            'endpoints' => [[
217
                'url' => $this->getReportURI(),
218
            ],],
219
            'include_subdomains' => $this->getIncludeSubdomains(),
220
        ];
221
        return json_encode($header);
222
    }
223
224
    protected function updateCspHeader($cspHeader)
225
    {
226
        if (!$this->disableReporting()) {
227
            // Add or update report-uri directive.
228
            if (strpos($cspHeader, 'report-uri')) {
229
                $cspHeader = str_replace('report-uri', $this->getReportURIDirective(), $cspHeader);
230
            } else {
231
                $cspHeader = rtrim($cspHeader, ';') . "; {$this->getReportURIDirective()};";
232
            }
233
234
            // Add report-to directive.
235
            // Note that unlike report-uri, only the first endpoint is used if multiple are declared.
236
            if ($this->config()->get('use_report_to')) {
237
                if (strpos($cspHeader, 'report-to') === false) {
238
                    $cspHeader = rtrim($cspHeader, ';') . "; {$this->getReportToDirective()};";
239
                }
240
            }
241
        }
242
243
        return $cspHeader;
244
    }
245
246
    /**
247
     * Is the CSPReportingOnly field safe to read.
248
     *
249
     * If the module is installed and the codebase is flushed before the database has been built,
250
     * accessing SiteConfig causes an error.
251
     *
252
     * @return boolean
253
     */
254
    private static function isCSPReportingAvailable()
255
    {
256
        // Cached true value.
257
        if (self::$is_csp_reporting_safe) {
258
            return self::$is_csp_reporting_safe;
259
        }
260
261
        // Check if all tables and fields required for the class exist in the database.
262
        $requiredClasses = ClassInfo::dataClassesFor(SiteConfig::class);
263
        $schema = DataObject::getSchema();
264
        foreach (array_unique($requiredClasses) as $required) {
265
            // Skip test classes, as not all test classes are scaffolded at once
266
            if (is_a($required, TestOnly::class, true)) {
267
                continue;
268
            }
269
270
            // if any of the tables aren't created in the database
271
            $table = $schema->tableName($required);
272
            if (!ClassInfo::hasTable($table)) {
273
                return false;
274
            }
275
276
            // if any of the tables don't have any fields mapped as table columns
277
            $dbFields = DB::field_list($table);
278
            if (!$dbFields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dbFields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
279
                return false;
280
            }
281
282
            // if any of the tables are missing fields mapped as table columns
283
            $objFields = $schema->databaseFields($required, false);
284
            $missingFields = array_diff_key($objFields, $dbFields);
285
            if ($missingFields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $missingFields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
286
                return false;
287
            }
288
        }
289
290
        self::$is_csp_reporting_safe = true;
291
292
        return true;
293
    }
294
}
295