feat: add ElementStyles.styleNonce for strict Content Security Policy#7653
Open
AKnassa wants to merge 2 commits into
Open
feat: add ElementStyles.styleNonce for strict Content Security Policy#7653AKnassa wants to merge 2 commits into
AKnassa wants to merge 2 commits into
Conversation
Injected <style> elements carried no nonce, so under a strict style-src (no 'unsafe-inline') the browser blocked them. Applications can now set `ElementStyles.styleNonce` (mirrored on FASTElement) and it is stamped onto every style element FAST creates. The nonce is read lazily at style-application time, because `css` templates build their ElementStyles at module evaluation, long before application code runs. Fixes microsoft#5444
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
AKnassa
marked this pull request as ready for review
July 14, 2026 02:25
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
janechu
requested changes
Jul 16, 2026
janechu
left a comment
Collaborator
There was a problem hiding this comment.
Please update your PR based on the skills in this project.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request
📖 Description
Under a strict Content Security Policy
style-srcthat does not include'unsafe-inline', the browser blocks the<style>elements FAST creates, so components rendered through a style-element strategy come out unstyled. The audience that runs strict CSP — enterprise and government — is exactly FAST's audience.This feature lets an application tell FAST which CSP nonce to stamp on those style elements:
ElementStyles.styleNonce— set once at startup; every style element FAST creates carries the nonce. Mirrored asFASTElement.styleNoncefor convenience.csstagged templates are evaluated at module scope, before application code has a chance to set the nonce, so capturing it at construction time would always observeundefined.StyleElementStrategyinelement-controller.tsand the fallback strategy inelement-styles.ts).## Securitysection to the fast-element README and documented the behavior in theStylessection ofDESIGN.md.Adopted stylesheets — the default on every browser FAST supports — are not subject to
style-srcand are unaffected. This closes the gap on the style-element path, which is reachable viawithStrategy()/setDefaultStrategy()and is the automatic fallback where adopted stylesheets are absent.🎫 Issues
👩💻 Reviewer Notes
styles.pw.spec.tscovers the case that matters most — setting the nonce after anElementStylesinstance already exists.FASTElement.styleNonceis defined withObject.definePropertyas a forwarding accessor (notObject.assign, which would copy the value) soElementStyles.styleNoncestays the single source of truth.StyleElementStrategyshould also be exported publicly so applications can opt into it deliberately is deliberately not bundled into this PR — feedback welcome.📑 Test Plan
New tests in
packages/fast-element/src/styles/styles.pw.spec.ts(13 cases) cover: no nonce configured, nonce set afterElementStylesconstruction,csstagged templates, multiple style elements, re-reading the nonce on every application, resetting tonull, removal of nonced style elements, application to aFASTElementshadow root, and theFASTElement.styleNoncemirror. All 13 pass locally in Chromium.Visual verification — before (styles blocked under strict CSP), after (nonce applied), and control (adopted stylesheets unaffected):
✅ Checklist
General
$ npm run changeAgents
⏭ Next Steps
element.innerHTML = styles[i]in the style-element path bypassesDOM.policy.createHTML, making it a Trusted Types sink. Out of scope for this change, but worth its own issue.StyleElementStrategyshould be exported publicly so applications can opt into it deliberately.