Skip to content

Commit

Permalink
Better logic for adding CSP nonces. Bump to 1.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mmikkel committed May 19, 2022
1 parent 12aa712 commit 6418d30
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# ToolMate Changelog

## Unreleased
## 1.4.1 - 2022-05-19
### Improved
- ToolMate now removes any hard-coded nonces or hashes set in the CSP config, for any directives that also contain the `unsafe-inline` policy
- ToolMate now avoids inadvertently creating CSP directives that could be empty, when adding nonces

## 1.4.0 - 2022-05-14
### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vaersaagod/toolmate",
"description": "Is that a tool in your pocket, or are you just happy to see me, mate?",
"type": "craft-plugin",
"version": "1.4.0",
"version": "1.4.1",
"keywords": [
"craft",
"cms",
Expand Down
15 changes: 7 additions & 8 deletions src/services/CspService.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,16 @@ public function setHeader(Response $response): void
$carry[StringHelper::toKebabCase($field)] = $policies;
return $carry;
}, []);

// Add memoized nonces
foreach ($this->nonces as $directive => $nonces) {
$directives[$directive] = $directives[$directive] ?? [];
foreach ($nonces as $nonce) {
if (in_array("'unsafe-inline'", $directives[$directive], true)) {
// Skip nonces for directives with unsafe-inline
continue;
}
$directives[$directive][] = "'nonce-" . $nonce . "'";
// Skip nonces for directives with unsafe-inline
if (empty($nonces) || in_array("'unsafe-inline'", $directives[$directive] ?? [])) {
continue;
}
$directives[$directive] = array_merge($directives[$directive] ?? [], array_map(static function (string $nonce) {
return "'nonce-$nonce'";
}, $nonces));
}

// Clear memoized nonces
Expand Down

0 comments on commit 6418d30

Please sign in to comment.