forked from TYPO3/typo3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Add CSP nonce helper for inline styles in lit-element templates
When using Content-Security-Policy for `style-src` with a `nonce-...` value, it requires that inline styles (those using a `<style>` element) have to be granted with a corresponding `nonce="..."` attribute. Note: 'unsafe-inline' is ignored when using a nonce or hashes. This behavior is decribed in CSP L3 in section 6.7.3.2.:2.1 (https://w3c.github.io/webappsec-csp/#allow-all-inline) > If expression matches the nonce-source or hash-source grammar, > return "Does Not Allow". Even if `<style>` usages in lit-element templates are static in most cases, it is considered a "inline style" in the scope of CSP. This change introduces a work-around, exposing `window.litNonce` in the global JavaScript context. In case a malicous script manages to retrieve this information, it does not really matter, since the malicious script was already executed with a valid nonce before... Resolves: #100140 Releases: main Change-Id: I53c2967f2c80c0f862145a4c94d75a5fc1349205 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/78231 Tested-by: core-ci <[email protected]> Reviewed-by: Benni Mack <[email protected]> Tested-by: Benni Mack <[email protected]> Tested-by: Andreas Fernandez <[email protected]> Reviewed-by: Andreas Fernandez <[email protected]>
- Loading branch information
1 parent
b599199
commit dfa794f
Showing
8 changed files
with
94 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* duplicated from https://github.com/egoist/style-inject/blob/04ca45c34f20f0aa63d3d68e668de037d24579ad/src/index.js | ||
* extended by nonce capabilities | ||
*/ | ||
export default function styleInject(css, { insertAt } = {}) { | ||
if (!css || typeof document === 'undefined') return | ||
|
||
const head = document.head || document.getElementsByTagName('head')[0] | ||
const style = document.createElement('style') | ||
style.type = 'text/css' | ||
if (window['litNonce']) { | ||
style.setAttribute('nonce', window['litNonce']); | ||
} | ||
if (insertAt === 'top' && head.firstChild) { | ||
head.insertBefore(style, head.firstChild) | ||
} else { | ||
head.appendChild(style) | ||
} | ||
if (style.styleSheet) { | ||
style.styleSheet.cssText = css | ||
} else { | ||
style.appendChild(document.createTextNode(css)) | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters