Skip to content

test(csp): add regression tests for pushDirective duplication (#16594)#16595

Merged
matthewp merged 5 commits intowithastro:mainfrom
web-dev0521:fix/csp-push-directive-duplicate-16594
May 4, 2026
Merged

test(csp): add regression tests for pushDirective duplication (#16594)#16595
matthewp merged 5 commits intowithastro:mainfrom
web-dev0521:fix/csp-push-directive-duplicate-16594

Conversation

@web-dev0521
Copy link
Copy Markdown
Contributor

Changes

  • Fix pushDirective in the CSP runtime duplicating the new directive once per existing non-matching directive.
  • Root cause: the else branch inside the loop pushed newDirective on every non-match instead of once after the loop. Replaced the deduplicated flag with a matched flag and moved the newDirective push outside the loop, guarded by !matched.
  • Closes csp: pushDirective duplicates new directive on every non-matching iteration #16594.

Before: pushDirective(["img-src 'self'", "style-src 'self'"], "script-src 'self'")["img-src 'self'", "script-src 'self'", "style-src 'self'", "script-src 'self'"] (duplicated + interleaved)
After:["img-src 'self'", "style-src 'self'", "script-src 'self'"]

Also fixes Mode 2: a directive that merges with a later existing entry no longer leaves an unmerged copy in front of the merged result.

Don't forget a changeset! Run pnpm changeset. — Added: .changeset/fix-csp-push-directive-duplicate.md

Testing

  • Added a regression: issue #16594 suite in packages/astro/test/units/csp/runtime.test.ts covering:
    • Mode 1 — non-matching new directive appended exactly once.
    • Mode 2 — merge with a later match, no unmerged copy left behind.
    • Empty input list edge case.
    • Many non-matches: directive-name uniqueness asserted via a Map count.
    • Table-driven sweep across "non-match", "match in middle", "match at end" shapes, asserting both length and Set-based name uniqueness.
  • All 11 tests in csp/runtime.test.ts pass locally (node --test test/units/csp/runtime.test.ts).

Docs

No docs change required — pushDirective is internal CSP runtime; public insertDirective() semantics are unchanged (this restores them to the documented behavior).

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 4, 2026

🦋 Changeset detected

Latest commit: 13d4045

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the pkg: astro Related to the core `astro` package (scope) label May 4, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 4, 2026

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing web-dev0521:fix/csp-push-directive-duplicate-16594 (13d4045) with main (78f305e)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (774542d) during the generation of this report, so 78f305e was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@matthewp matthewp merged commit ce9b25c into withastro:main May 4, 2026
27 checks passed
@astrobot-houston astrobot-houston mentioned this pull request May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

csp: pushDirective duplicates new directive on every non-matching iteration

2 participants