fix(symfony): emit a csp nonce on the swagger ui scripts#8310
Open
soyuka wants to merge 1 commit into
Open
Conversation
Under a CSP like "script-src 'self' 'strict-dynamic'" the bundled SwaggerUI
scripts were blocked because they carried no nonce. Emit one, reusing existing
mechanisms with no new config key: a `_csp_nonce` request attribute takes
precedence, otherwise the nelmio `csp_nonce('script')` Twig function is used
when available; absent both, output is unchanged.
The csp_nonce() call lives in an included child template so Twig does not fail
at parse time when the function is not registered.
Fixes api-platform#8142
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.
Summary
Under a Content Security Policy such as
script-src 'self' 'strict-dynamic', the bundled SwaggerUI<script>tags were blocked because they carried no nonce. They now receive one, reusing existing mechanisms with no new config key:_csp_noncerequest attribute (set by a proxy/middleware) takes precedence;csp_nonce('script')Twig function is used when available;nonceattribute) so plain installs are unaffected.The
csp_nonce()call lives in aninclude()'d child template (csp-nonce.html.twig) because a literalcsp_nonce()in the main template would throw a TwigSyntaxErrorat parse time when the function is not registered — a runtime{% if %}guard does not prevent parse-time failure.Reproduction
Visiting
/apiwith a strict CSP blocked the SwaggerUI scripts (no nonce emitted).Test plan
SwaggerUiCspNonceTest(4 cases): request-attribute nonce,csp_nonce()nonce, request-attr precedence over the function, and no-mechanism → nononceattribute.DocumentationActionTestregression green (9/9).Fixes #8142