fix(website): harden Astro CSP and verify the built output against it - #318
parthrohit22 wants to merge 8 commits into
Conversation
|
@parthrohit22, I am taking the lead security review on this. Before final approval, please rebase onto current |
m-khan-97
left a comment
There was a problem hiding this comment.
Parth, removing the browser PAT flow is the right security decision, and the central DOMPurify path is a major improvement over both the original unsafe renderer and the blanket textContent workaround. I traced the current dynamic sinks: the remaining innerHTML assignments are routed through sanitization, and the URL canonicalization fix is valid.
I found two release-blocking gaps against #297’s acceptance criteria:
-
The new Playwright suite does not exercise the CSP at all. Its local Python server does not apply
vercel.jsonheaders, and there is no assertion for theContent-Security-Policyresponse header or asecuritypolicyviolationevent. The issue explicitly requires Playwright coverage for CSP violations, so all 34 tests can pass while the deployed header is absent or broken. Please run the browser suite through a server that applies the production header (or add an equivalent production-header harness), assert the header itself, and add positive/negative CSP behavior coverage. -
script-srcstill permitsunsafe-inline, with many static inline handlers and two inline script blocks retained. That means the CSP is not a meaningful fallback if any HTML injection path escapes sanitization. For this security-boundary PR, please move the static handlers and inline blocks into trusted same-origin JavaScript and removeunsafe-inlinefromscript-src. If Tailwind requires inline styles, keep that decision isolated tostyle-src; it does not justify inline script execution.
The branch also needs the already-requested rebase onto current dev. Please address these on the rebased head and rerun the complete browser/security suite; I will re-review promptly.
ritiksah141
left a comment
There was a problem hiding this comment.
Requesting changes for two findings not covered by the existing review thread, both verified locally on 153a6fc.
-
The
testscript inwebsite/package.jsonis broken. It runsnode --test tests/toEmbedUrl.test.mjs, but that file does not exist (the real test lives atwebsite/test_toEmbedUrl.mjs). Runningnpm testinside website/ fails withCould not find tests/toEmbedUrl.test.mjs. CI never invokesnpm test(both website jobs call the binaries directly), which is why this slipped through. Point the script at the existing file, or move the file to match. -
The accessibility spec flakes under parallel load. In a full parallel run locally, all four axe tests in tests/accessibility.spec.js timed out inside goToSection() waiting on waitForSelector visibility (reproduced in 1 of 2 full runs; the same spec passes 7/7 serially, and a clean parallel rerun passes 34/34). Root cause: the pre-existing showSection() races a 300ms setTimeout against a requestAnimationFrame. Under CPU contention the rAF callback can land more than 300ms late, so the timeout inlines display:none on the section being activated, and the visibility wait never resolves. The fix is the pattern this suite already uses in the mobile-menu and FAQ tests: in goToSection() (tests/helpers.js), wait for DOM state instead of rendered visibility, e.g. waitForFunction that the section has the active class and its inline display is not none. The CI retries: 1 can mask this flake rather than fix it.
For the record, I agree with the two blocking points in the other review (the browser suite never exercises the CSP header despite #297 listing CSP-violation coverage in its acceptance criteria, and unsafe-inline should leave script-src) plus the rebase onto dev.
|
@m-khan-97 @ritiksah141 Both blocking findings and the two additional ones are addressed on the current head, rebased onto Your two blockers, m-khan-97:
Your two findings, ritiksah141: Two things I caught myself while doing this, fixed along the way rather than leaving for a next round: removing the inline handlers broke one existing test selector ( Verified: full backend suite (845 passed, 5 skipped — pre-existing/environment-only), |
m-khan-97
left a comment
There was a problem hiding this comment.
Parth, I verified the current head end to end. The browser PAT flow is removed; dynamic HTML and Markdown sinks remain centralized through DOMPurify; the inline script blocks and static event-handler attributes are gone; and script-src no longer permits unsafe-inline. The Playwright server now applies the actual vercel.json headers, and the security suite proves both sides of the boundary: injected inline script is blocked with a CSP violation while the site’s allowed scripts and navigation continue to work. Ritik’s additional findings are also closed: npm test targets the real file and the accessibility wait no longer depends on the flaky rendered-visibility race.
I ran this head independently: the unit suite passed, followed by all 37 Playwright/axe/CSP/XSS tests. GitHub’s 21 checks are green as well. Approving.
|
@ritiksah141, your two additional blockers are fixed on |
|
@ritiksah141 Both your findings — the broken |
66778fc to
f917549
Compare
|
@ritiksah141 this branch is now rebased onto the latest Re-review requested — your two findings from the 153a6fc review look resolved on the current tip:
The CSP points from the other review are also covered: the browser suite now exercises the CSP response header, and |
f917549 to
350ef1f
Compare
Signed-off-by: parthrohit22 <parthrohit60@gmail.com>
350ef1f to
fba8ae9
Compare
TFT444
left a comment
There was a problem hiding this comment.
Thank you for removing 'unsafe-inline' from script-src and adding form-action 'self'. Both are real improvements.
One issue remains: frame-ancestors 'self' in a CSP tag is silently ignored by all major browsers per the CSP Level 2 spec. Only HTTP response headers enforce this directive. Since GitHub Pages cannot set HTTP response headers, this directive provides no clickjacking protection regardless of where it appears in the document.
The current state is worse than simply omitting it: verify-site.mjs now checks that frame-ancestors 'self' is present in the meta CSP, so CI reports this as verified when the browser ignores it entirely. That gives false confidence that clickjacking protection exists.
Please either remove frame-ancestors 'self' from both the CSP string and the verify check and add a comment explaining the GitHub Pages limitation, or keep the directive but update the verify check to note the limitation and not treat its presence as security evidence. The first option is cleaner.
m-khan-97
left a comment
There was a problem hiding this comment.
Parth, I reassessed fba8ae9 after the Astro migration. My previous approval concerned a different implementation and its HTTP-header test server; it cannot validate this new meta-policy implementation.
Tanvir's finding is still present: Base.astro includes frame-ancestors in a meta CSP, and verify-site.mjs requires it as if it were effective protection. Browsers do not enforce frame-ancestors from meta policies (https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/frame-ancestors).
Please remove that directive from the meta policy and its required-directive check, and document the hosting limitation. If clickjacking protection is required, supply it through an actual HTTP response header on supported hosting and verify that response. Keep the supported script-src/form-action improvements. Please also verify the built Astro site in a browser so tightening script-src does not break its actual scripts.
Signed-off-by: parthrohit22 <parthrohit60@gmail.com>
TFT444 / m-khan-97 review: frame-ancestors 'self' in a <meta http-equiv> CSP is silently ignored by browsers (only an HTTP response header enforces it), and GitHub Pages cannot set that header. verify-site.mjs required the directive, so CI reported clickjacking protection that does not exist. - Base.astro: remove frame-ancestors from the meta CSP; expand the comment to state which directives a meta policy cannot enforce and that clickjacking protection needs a real header at a configurable edge. - verify-site.mjs: stop requiring frame-ancestors; document why. Also addresses m-khan-97's 'verify the built site so tightening script-src does not break its actual scripts': the Astro build was inlining 19 hoisted <script> blocks per page (nav toggle, tab switcher, docs/rules search, animated counters). With script-src 'self' and no unsafe-inline/nonce/hash the browser blocks every one of them on the deployed site, while verify-site.mjs's CSP-string check still passed. - astro.config.mjs: build.inlineStylesheets 'never' + build/vite assetsInlineLimit 0, so every hoisted script is emitted as a same-origin file inside script-src 'self'. - verify-site.mjs: fail the build if any rendered page carries an executable inline <script> (data blocks like application/json and application/ld+json are still allowed). Verified against the production build in a browser: CSP delivered without frame-ancestors/unsafe-inline, zero inline executable scripts in the live DOM, and nav toggle, rules filter (126 -> 2 -> 126), and docs search (45 -> 0 -> 45) all work with no CSP violations in the console. Signed-off-by: parthrohit22 <parthrohit60@gmail.com>
The literal '<script>' substring next to an interpolated ${relative} in the
failure string tripped Semgrep's unknown-value-with-script-tag XSS rule
(the string is a console error message, not HTML output - a false positive).
Reworded to 'inline script element'; the check is unchanged.
Signed-off-by: parthrohit22 <parthrohit60@gmail.com>
CodeQL js/bad-tag-filter flagged the /<script...>...<\/script>/ regex in verify-site.mjs as an incomplete HTML filter (it would miss '</script >'). The check is a build-output linter, not a security sanitizer, but the finding is fair. Replaced with a plain case-insensitive index walk from each opening tag to its close - same three outcomes (external src -> ok, json/ld+json data -> ok, non-empty inline body -> fail), no tag regex. Signed-off-by: parthrohit22 <parthrohit60@gmail.com>
|
@TFT444 @m-khan-97 Re-review requested for the meta-CSP finding. The current head removes |
|
@TFT444 @m-khan-97 — re-review please. The
|
| Control | Result |
|---|---|
| Nav toggle | aria-expanded false → true |
| Demo carousel "Next rule" | advanced AZ-STOR-001 → AZ-NET-001 |
| "Pause motion" | toggled to "Resume motion" |
| Hero three.js canvas | rendering, non-zero dimensions |
| Rules page search | 253 → 25 entries for storage |
| Rules page domain filter | domainFilter=net applied |
And the policy is actually enforced, not just present — injecting an inline script gets blocked:
{ "inlineScriptExecuted": false,
"violations": ["script-src-elem <- inline", "img-src <- https://evil.example.com/x.png"] }Swept every page (/, /rules/, /docs/, /architecture/, /blog/, /community/, /evidence/, 404): CSP present on all, frame-ancestors on none, zero inline script blocks, zero inline on* handler attributes.
npm run check passes for both CMS configurations (14 pages verified, largest JS asset 482 KiB), and all 21 checks are green on f2e79c8.
One thing I want to be accurate about: this was a manual browser run on my machine, not a CI job — there is still no automated browser suite for the Astro site. The earlier Playwright/CSP suite covered the pre-migration implementation and its HTTP-header test server, which is exactly why you withdrew the approval. If you'd like that coverage restored against the meta-policy build before this merges, say so and I'll add it here rather than as a follow-up.
TFT444
left a comment
There was a problem hiding this comment.
Re-reviewed. The frame-ancestors concern is resolved: it has been removed from the meta CSP and the verify script documents why meta policies cannot enforce it. However, Ritik's finding still stands: website/package.json test script references tests/toEmbedUrl.test.mjs which does not exist. Please fix that broken test reference before merge.
Signed-off-by: parthrohit22 <parthrohit60@gmail.com>
Signed-off-by: PARTH ROHIT <parthrohit60@gmail.com>
137b6ba to
ee71795
Compare
|
@TFT444 @m-khan-97 @ritiksah141 addressed the remaining CSP-verifier concern in ee71795. The production verifier now requires exactly script-src 'self', so remote script sources cannot silently weaken the policy. |
ritiksah141
left a comment
There was a problem hiding this comment.
Requesting changes on the PR scope and description only. The code change itself is sound and I verified it locally end to end; what blocks merge in the current state is that this PR would close #297 against acceptance criteria the diff does not meet, with a description that still describes code that no longer exists in the repo.
Blocker: "Closes #297" plus a description for the wrong codebase
Issue #297 is flagged as a release blocker and its acceptance criteria explicitly require:
- Playwright tests covering rendering, navigation, editor removal, CSP violations and an XSS regression corpus.
- Keyboard and axe checks running in CI.
This diff contains neither (no workflow changes, no test files). Merging with Closes #297 in the description would auto-close a security issue that is still open on those criteria.
The rest of the description describes the pre-migration static site: PAT flow removal in script.js, DOMPurify sanitization, a vercel.json CSP, Playwright suites under website/tests/, test_toEmbedUrl.mjs passing 8/8. All of those files were removed when #329 migrated the website to Astro, and none of that work appears in the final diff. What the diff actually is, in 4 files and +75/-6: the CSP tightening in Base.astro, external-asset build settings in astro.config.mjs, production-build assertions in verify-site.mjs, and README documentation.
One description claim is also factually wrong against the final diff: it says https://api.github.com was dropped from connect-src because nothing calls it. The site still calls it (the star-count fetch in the hoisted Base.astro script), and the CSP correctly keeps it.
What I would like before approving:
- Rewrite the description to describe the actual diff: CSP hardening for the Astro site, the verifier ratchets, and the honest frame-ancestors documentation.
- Drop
Closes #297or downgrade it toPart of #297/Refs #297. If the position is that the issue should close, that is a maintainer rescoping decision that belongs on the issue: required-work items 1 through 4 were obsoleted by #329 (no PAT flow and no innerHTML renderer survive in the new site), but the Playwright/axe acceptance criteria remain open and need a tracking home.
Non-blocking findings
- Top-level
build.assetsInlineLimit: 0inastro.config.mjsis dead config. Astro 7.3.1's build schema carries only format, assets, assetsPrefix, serverEntry, redirects, inlineStylesheets and concurrency, and zod strips unknown keys silently. The effective setting isvite.build.assetsInlineLimit: 0, which you also set. The comments inastro.config.mjsandverify-site.mjsboth point at the dead key; remove the key or correct the comments. - The verifier excludes
/admin/, and the generated Decap admin shell carries no CSP at all. The exclusion is necessary (decap-cms.min.js loads from jsdelivr and could never passscript-src 'self'), but a dedicated admin CSP would be a good follow-up:script-src 'self' https://cdn.jsdelivr.net,connect-src https://api.github.com, style-src with unsafe-inline. Today only SRI and the robots.txt exclusion protect that page. - The inline-script scanner does not assert that external script src values are same-origin. A remote-src script would pass verification and be silently CSP-blocked at runtime, which is the same build-green-but-site-broken class the scan exists to prevent. A one-line check that non-admin srcs start with
/openshield/would close it. - Theoretical scanner limits, fine to defer: an attribute value containing
>truncates the attrs scan, and a literal</script>inside a JSON data block would break out in the browser because JSON.stringify does not escape<. The data is repo-static and the new CSP bounds the blast radius. Optional hardening: escape<as<in the data blocks fed to set:html.
What I verified locally
npm run check(exactly what CI runs: build, configure-cms, verify against both CMS-disabled and CMS-enabled output): passes, 14 pages, largest JS asset 482 KiB against the 520 KiB budget.- Non-vacuity of the new assertions, in both directions: planting
<script>alert(1)</script>into the built index.html fails the verifier; restoring and flipping the built CSP toscript-src 'self' 'unsafe-inline'fails the exact-match ratchet with exit 1; restoring passes again. - The built output confirms the model: the three application/ld+json blocks and the application/json orb-data block are correctly exempted by the scanner, and all four hoisted scripts are emitted as external module files under /openshield/_astro/.
- The exact-match script-src ratchet is the right guard: a future remote source or a reintroduced unsafe-inline fails verification instead of quietly weakening the policy.
connect-src https://api.github.comis required, not vestigial: the hoisted script inBase.astrofetches the repo for the star count, uses textContent and swallows failures.form-action 'self'blocks nothing today (there is no form element in any .astro source), so it is pure hardening.- The README and Base.astro comments are accurate about frame-ancestors, report-uri/report-to and sandbox being ignored in a meta policy, and about clickjacking protection not actually being in place on GitHub Pages. The verifier deliberately not asserting frame-ancestors, so CI never reports coverage that does not exist, is exactly the right call.
The security substance here is good work. Once the description matches the diff and the #297 closure is handled explicitly, I would switch to approve.
TFT444
left a comment
There was a problem hiding this comment.
@parthrohit22 two blockers remain before this can merge:
website/package.jsontest script referencestests/toEmbedUrl.test.mjswhich does not exist. This was flagged on Sep 14 and is still unaddressed. Please fix the path to point at the existing test file.- The PR description describes the old DOMPurify/PAT-removal flow, not the actual changes in this diff (Astro CSP hardening, inline-script prevention, verifier ratchets). Please rewrite the description to match the diff, and change
Closes #297toRefs #297since the Playwright/axe criteria from that issue were superseded by the Astro migration.
The security substance of the code is correct. Once these two are fixed this is approvable.
…ld key Addresses the review findings on OWASP#318: - astro.config.mjs: remove the top-level build.assetsInlineLimit key. Astro's build schema has no such key and zod strips it silently, so the effective setting was always vite.build.assetsInlineLimit. Comments in both files now point at the key that actually takes effect. - verify-site.mjs: assert every external script src is same-origin. A remote src previously passed verification and would then be CSP-blocked at runtime - the same build-green-but-site-broken case the inline-script scan prevents. - docs/input-validation-audit.md: the media-URL row referenced website/test_toEmbedUrl.mjs, which the Astro migration removed along with the browser editor it tested. Replaced both stale rows with the build-time content model that is actually in place. Signed-off-by: parthrohit22 <parthrohit60@gmail.com>
|
@TFT444 @ritiksah141 both blockers are addressed, plus two of Ritik's non-blocking findings. 1. The broken 2. Description and issue linkage. Rewritten from scratch to describe this diff only — the CSP tightening in Also fixed, from the non-blocking list:
Findings #2 (a dedicated CSP for the Decap admin shell) and #4 (the scanner's theoretical attribute/
|
What does this PR do?
Hardens the Content Security Policy of the Astro website and adds build-time verification that keeps the policy and the built output in agreement.
Refs #297.
Type of change
Scope note
This PR was opened before #329 migrated the website to Astro. The original work (removing the browser PAT flow, DOMPurify sanitization of
innerHTMLsinks, avercel.jsonheader CSP, the Playwright suites underwebsite/tests/) targeted files that no longer exist: the migration deleted the browser editor, the client-side renderer and the PAT flow outright, so those changes were obsoleted rather than merged. The description below reflects only what this diff actually contains against currentdev.Closes #297has been downgraded toRefs #297. Items 1-4 of that issue (PAT flow,innerHTMLrenderer) no longer have any code to fix after #329, but its Playwright/axe acceptance criteria are still open and this diff does not satisfy them, so the issue must not auto-close on merge. Rescoping or closing #297 is a maintainer decision that belongs on the issue itself.What changed
src/layouts/Base.astro— CSP tighteningscript-src 'self' 'unsafe-inline'→script-src 'self'. With Astro emitting every hoisted script as an external same-origin file,unsafe-inlinebuys nothing and removing it makes the policy a real fallback against injected inline script.form-action 'self'. There is no<form>in any.astrosource today, so this blocks nothing currently — pure hardening.connect-srckeepshttps://api.github.com: the hoisted script in this layout fetches the repository for the star count. It is required, not vestigial.frame-ancestorsis deliberately not present. Browsers ignore it in a<meta http-equiv>policy, and GitHub Pages cannot set the HTTP response header that would make it effective. The comment in the file andwebsite/README.mdstate plainly that clickjacking protection is not in place and needs a real response header from a configurable edge.scripts/verify-site.mjs— production-build ratchetsscript-src 'self'. An exact match, not an "absence ofunsafe-inline" check, so a future remote source also fails verification instead of quietly weakening the policy.object-src 'none',base-uri 'self'andform-action 'self'are present. It deliberately does not assertframe-ancestors, so CI never reports coverage that does not exist.<script>blocks, whichscript-src 'self'would silently block on the deployed site. Data blocks (application/json,application/ld+json,speculationrules) are exempt because they are not executed. The scan walks tag indices by hand rather than using a tag-filter regex (CodeQLjs/bad-tag-filter).srcis same-origin. A remotesrcpreviously passed verification and would then be CSP-blocked in the browser — the same build-green-but-site-broken case the inline scan exists to prevent.astro.config.mjsinlineStylesheets: 'never'andvite.build.assetsInlineLimit: 0so nothing gets inlined into the HTML.build.assetsInlineLimitkey: Astro's build schema has no such key and zod strips it silently, so it was dead config. The comments here and in the verifier now point at the setting that actually takes effect.Docs
website/README.mddocuments the meta-policy limitation honestly.docs/input-validation-audit.mdhad a row pointing atwebsite/test_toEmbedUrl.mjs, which website: migrate to Astro + Decap CMS with GitHub Pages pipeline #329 removed along with the browser editor it tested. Both editor-era rows are replaced with the build-time content model that is actually in place.Testing
npm run checkinwebsite/— exactly what CI runs (build, configure-cms, verify against both CMS-disabled and CMS-enabled output): passes, 14 pages, largest JS asset 482 KiB.Non-vacuity checked in both directions on the built output: planting
<script>alert(1)</script>intodist/index.htmlfails verification; planting<script src="https://cdn.example.com/x.js">fails with the new same-origin message; flipping the built CSP toscript-src 'self' 'unsafe-inline'fails the exact-match ratchet; restoring passes again.The three
application/ld+jsonblocks and theapplication/jsonorb-data block are correctly exempted, and all four hoisted scripts are emitted as external files under/openshield/_astro/.All CI checks pass
Returns correct JSON output — n/a, no API/JSON endpoint changed
No hardcoded credentials or secrets