feat(symfony): emit a csp nonce on the swagger ui and graphiql scripts#8310
Open
soyuka wants to merge 1 commit into
Open
feat(symfony): emit a csp nonce on the swagger ui and graphiql scripts#8310soyuka wants to merge 1 commit into
soyuka wants to merge 1 commit into
Conversation
c2b555d to
8ef0a96
Compare
8ef0a96 to
355ce8a
Compare
Under a CSP like "script-src 'self' 'strict-dynamic'" the bundled SwaggerUI
and GraphiQL 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 reused (so the emitted nonce matches the one nelmio adds to the
CSP response header); absent both, output is unchanged.
The nonce is resolved in PHP, so no `csp_nonce()` call appears in the
templates and plain installs without nelmio are unaffected.
Fixes api-platform#8142
355ce8a to
a9f111c
Compare
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 and GraphiQL<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 reused when available — so the emitted nonce matches the one NelmioSecurityBundle adds to the CSP response header;nonceattribute) so plain installs are unaffected.The nonce is resolved in PHP (
SwaggerUiProcessor/GraphiQlAction), not in Twig. This keeps anycsp_nonce()call out of the templates: a literalcsp_nonce()in a template would throw a TwigSyntaxErrorat parse time when the function is not registered, and a runtime{% if %}guard cannot prevent that. Resolving in PHP avoids the problem entirely — the template just emits the attribute when a nonce is present.Scope
SwaggerUi/index.html.twigviaSwaggerUiProcessor): inline data block, swagger/redoc/scalar bundles, init scripts.Graphiql/index.html.twigviaGraphiQlAction):importmap, inline data block,init-graphiql.jsmodule.Out of scope:
style-srcnonces and the Scalarcdn.jsdelivr.nethost (covered bystrict-dynamiconce a nonced script bootstraps). This PR addressesscript-srconly, per the issue.Reproduction
Visiting
/apior/graphql/graphiqlwith a strict CSP blocked the scripts (no nonce emitted).Test plan
SwaggerUiCspNonceTestandGraphiqlCspNonceTest(4 cases each): request-attribute nonce,csp_nonce()nonce, request-attr precedence over the function, and no-mechanism → nononceattribute.GraphiQlActionTestunit updated (stubsgetFunction('csp_nonce')).DocumentationActionTestregression green (9/9).Fixes #8142