fix: remove dead else branch in csp insertDirective#16921
Merged
ematipico merged 3 commits intoJun 2, 2026
Merged
Conversation
The else branch in `getCsp().insertDirective` called `.push` on `state.result.directives` after the if-condition had just established that value was nullish, which would have thrown a TypeError if reached. In practice `directives` is always initialized to an array, so the else was unreachable, but the shape of the code was misleading. Simplify to match the sibling `insertScriptResource` / `insertStyleResource` methods: guard on `state.result`, then update directives via pushDirective.
|
ematipico
approved these changes
Jun 2, 2026
ematipico
added a commit
that referenced
this pull request
Jun 2, 2026
* chore: move integration tests to unit tests (#16869) * chore(deps): replace which-pm-runs with package-manager-detector (#16901) * chore(deps): update dependency hono to v4.12.18 [security] (#16669) * Fix false positive missing-content audit for hidden anchors (#16016) * fix: remove dead else branch in csp insertDirective (#16921) Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> * chore: avoid formatter conflicts (#16897) --------- Co-authored-by: ocavue <ocavue@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Felmon <felmonon@gmail.com> Co-authored-by: Stefan Machhammer <stefan.machhi@gmail.com>
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.
Description
Simplifies the
insertDirectivemethod returned bycontext.csp(inpackages/astro/src/core/fetch/fetch-state.ts).The previous implementation had a misleading
elsebranch:Two problems:
state.result.directivesis always initialized to an array (seefetch-state.ts:407—directives: manifest.csp?.directives ? [...manifest.csp.directives] : []). Because empty arrays are truthy in JS, theifbranch fires wheneverstate.resultexists. Theelseis unreachable in practice.elsewould crash if reached. Calling.pushon the value that theif-condition just established is nullish would throw aTypeError. The optional-chaining?.doesn't help — it only short-circuits up throughstate?.result?, not the final.pushcall.This PR replaces both branches with the same single-statement pattern used by the sibling
insertScriptResource/insertStyleResourcemethods directly below it.Testing
Pure refactor of unreachable code; behavior under all currently-reachable inputs (i.e. whenever
state.resultexists, which is the only state the existing if-branch matches) is unchanged.Docs
Not user-visible; no docs changes needed.
Changeset
Included.