signify-nz /
silverstripe-security-headers
| 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
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
|
|||
| 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
|
|||
| 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
|
|||
| 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
|
|||
| 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
|
|||
| 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 | $headersToSend = $headersConfig['global']; |
||
| 87 | |||
| 88 | if ($this->isReporting() && $this->config()->get('use_report_to')) { |
||
| 89 | $this->addReportToHeader($headersToSend); |
||
| 90 | } |
||
| 91 | |||
| 92 | // Update CSP header. |
||
| 93 | if (array_key_exists('Content-Security-Policy', $headersToSend)) { |
||
| 94 | $header = 'Content-Security-Policy'; |
||
| 95 | |||
| 96 | if ($this->hasCSP()) { |
||
| 97 | $headerValue = $headersToSend['Content-Security-Policy']; |
||
| 98 | |||
| 99 | // Set report only mode if appropriate. |
||
| 100 | if ($this->isCSPReportingOnly()) { |
||
| 101 | unset($headersToSend['Content-Security-Policy']); |
||
| 102 | $header = 'Content-Security-Policy-Report-Only'; |
||
| 103 | } |
||
| 104 | |||
| 105 | // Update CSP header value. |
||
| 106 | $headersToSend[$header] = $this->updateCspHeader($headerValue); |
||
| 107 | } else { |
||
| 108 | unset($headersToSend['Content-Security-Policy']); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | $this->extend('updateHeaders', $headersToSend, $request); |
||
| 112 | |||
| 113 | // Add headers to response. |
||
| 114 | foreach ($headersToSend as $header => $value) { |
||
| 115 | if (empty($value)) { |
||
| 116 | continue; |
||
| 117 | } |
||
| 118 | $value = preg_replace('/\v/', '', $value); |
||
| 119 | $this->extend('updateHeader', $header, $value, $request); |
||
| 120 | if ($value) { |
||
| 121 | $response->addHeader($header, $value); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | return $response; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Return true if the Disable CSP is unchecked |
||
| 130 | * |
||
| 131 | * @return boolean |
||
| 132 | */ |
||
| 133 | public function hasCSP() |
||
| 134 | { |
||
| 135 | return self::isCSPReportingAvailable() && |
||
| 136 | SiteConfig::current_site_config()->CSPReportingOnly != SecurityHeaderSiteconfigExtension::CSP_DISABLE; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Return true if the Disable reporting is unchecked |
||
| 141 | * |
||
| 142 | * The CMS setting can disable reporting even if the 'enable_reporting' is true |
||
| 143 | * |
||
| 144 | * @return boolean |
||
| 145 | */ |
||
| 146 | public function isReporting() |
||
| 147 | { |
||
| 148 | if ($this->hasCSP()) { |
||
| 149 | return SiteConfig::current_site_config()->CSPReportingOnly |
||
| 150 | != SecurityHeaderSiteconfigExtension::CSP_WITHOUT_REPORTING |
||
| 151 | && $this->config()->get('enable_reporting'); |
||
| 152 | } |
||
| 153 | |||
| 154 | return false; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Returns true if the Content-Security-Policy-Report-Only header should be used. |
||
| 159 | * |
||
| 160 | * @return boolean |
||
| 161 | */ |
||
| 162 | public function isCSPReportingOnly() |
||
| 163 | { |
||
| 164 | if ( |
||
| 165 | self::isCSPReportingAvailable() && |
||
| 166 | SiteConfig::current_site_config()->CSPReportingOnly == SecurityHeaderSiteconfigExtension::CSP_REPORTING_ONLY |
||
| 167 | ) { |
||
| 168 | return true; |
||
| 169 | } |
||
| 170 | |||
| 171 | return false; |
||
| 172 | } |
||
| 173 | |||
| 174 | protected function getReportURI() |
||
| 175 | { |
||
| 176 | return Director::absoluteURL($this->config()->get('report_uri')); |
||
| 177 | } |
||
| 178 | |||
| 179 | protected function getIncludeSubdomains() |
||
| 180 | { |
||
| 181 | return $this->config()->get('report_to_subdomains'); |
||
| 182 | } |
||
| 183 | |||
| 184 | protected function getReportToGroup() |
||
| 185 | { |
||
| 186 | return $this->config()->get('report_to_group'); |
||
| 187 | } |
||
| 188 | |||
| 189 | protected function getReportURIDirective() |
||
| 190 | { |
||
| 191 | return "report-uri {$this->getReportURI()}"; |
||
| 192 | } |
||
| 193 | |||
| 194 | protected function getReportToDirective() |
||
| 195 | { |
||
| 196 | return "report-to {$this->getReportToGroup()}"; |
||
| 197 | } |
||
| 198 | |||
| 199 | protected function addReportToHeader(&$headers) |
||
| 200 | { |
||
| 201 | if (array_key_exists('Report-To', $headers)) { |
||
| 202 | $headers['Report-To'] = $headers['Report-To'] . ',' . $this->getReportToHeader(); |
||
| 203 | } else { |
||
| 204 | $headers['Report-To'] = $this->getReportToHeader(); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | protected function getReportToHeader() |
||
| 209 | { |
||
| 210 | $header = [ |
||
| 211 | 'group' => $this->getReportToGroup(), |
||
| 212 | 'max_age' => 1800, |
||
| 213 | 'endpoints' => [[ |
||
| 214 | 'url' => $this->getReportURI(), |
||
| 215 | ],], |
||
| 216 | 'include_subdomains' => $this->getIncludeSubdomains(), |
||
| 217 | ]; |
||
| 218 | return json_encode($header); |
||
| 219 | } |
||
| 220 | |||
| 221 | protected function updateCspHeader($cspHeader) |
||
| 222 | { |
||
| 223 | if ($this->isReporting()) { |
||
| 224 | // Add or update report-uri directive. |
||
| 225 | if($cspHeader) { |
||
| 226 | if (strpos($cspHeader, 'report-uri')) { |
||
| 227 | $cspHeader = str_replace('report-uri', $this->getReportURIDirective(), $cspHeader); |
||
| 228 | } else { |
||
| 229 | $cspHeader = rtrim($cspHeader, ';') . "; {$this->getReportURIDirective()};"; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | else { |
||
| 233 | $cspHeader = $this->getReportURIDirective() . ';'; |
||
| 234 | } |
||
| 235 | // Add report-to directive. |
||
| 236 | // Note that unlike report-uri, only the first endpoint is used if multiple are declared. |
||
| 237 | if ($this->config()->get('use_report_to')) { |
||
| 238 | if (strpos($cspHeader, 'report-to') === false) { |
||
| 239 | $cspHeader = rtrim($cspHeader, ';') . "; {$this->getReportToDirective()};"; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | return $cspHeader; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Is the CSPReportingOnly field safe to read. |
||
| 249 | * |
||
| 250 | * If the module is installed and the codebase is flushed before the database has been built, |
||
| 251 | * accessing SiteConfig causes an error. |
||
| 252 | * |
||
| 253 | * @return boolean |
||
| 254 | */ |
||
| 255 | private static function isCSPReportingAvailable() |
||
| 256 | { |
||
| 257 | // Cached true value. |
||
| 258 | if (self::$is_csp_reporting_safe) { |
||
| 259 | return self::$is_csp_reporting_safe; |
||
| 260 | } |
||
| 261 | |||
| 262 | // Check if all tables and fields required for the class exist in the database. |
||
| 263 | $requiredClasses = ClassInfo::dataClassesFor(SiteConfig::class); |
||
| 264 | $schema = DataObject::getSchema(); |
||
| 265 | foreach (array_unique($requiredClasses) as $required) { |
||
| 266 | // Skip test classes, as not all test classes are scaffolded at once |
||
| 267 | if (is_a($required, TestOnly::class, true)) { |
||
| 268 | continue; |
||
| 269 | } |
||
| 270 | |||
| 271 | // if any of the tables aren't created in the database |
||
| 272 | $table = $schema->tableName($required); |
||
| 273 | if (!ClassInfo::hasTable($table)) { |
||
| 274 | return false; |
||
| 275 | } |
||
| 276 | |||
| 277 | // if any of the tables don't have any fields mapped as table columns |
||
| 278 | $dbFields = DB::field_list($table); |
||
| 279 | if (!$dbFields) { |
||
|
0 ignored issues
–
show
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 Loading history...
|
|||
| 280 | return false; |
||
| 281 | } |
||
| 282 | |||
| 283 | // if any of the tables are missing fields mapped as table columns |
||
| 284 | $objFields = $schema->databaseFields($required, false); |
||
| 285 | $missingFields = array_diff_key($objFields, $dbFields); |
||
| 286 | if ($missingFields) { |
||
|
0 ignored issues
–
show
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 Loading history...
|
|||
| 287 | return false; |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | self::$is_csp_reporting_safe = true; |
||
| 292 | |||
| 293 | return true; |
||
| 294 | } |
||
| 295 | } |
||
| 296 |