Skip to content

fix: drop style-src 'unsafe-inline' from the CSP - #23

Merged
wonmors merged 2 commits into
wonmortech:mainfrom
ismoilovdevml:fix/csp-no-inline-styles
Nov 14, 2026
Merged

wonmors merged 2 commits into
wonmortech:mainfrom
ismoilovdevml:fix/csp-no-inline-styles

Conversation

@ismoilovdevml

Copy link
Copy Markdown
Contributor

What and why

The CSP allowed style-src 'unsafe-inline' because of five style attributes in each home page and one in the step-icon markup built by sanad-core.js (residual risk in docs/ASSURANCE.md). They are now classes in sanad.css, and the CSP is style-src 'self' plus the hash of the favicon's <style>.

  • Home pages (uz, en, ru): the chain traveller (opacity:0, rect fill), the demo meta line, and the Telegram heading and lead use CSS rules instead of style attributes. main.js still animates the traveller through CSSOM (style.opacity), which the CSP allows.
  • sanad-core.js: the step "x" icon gets position: absolute from .steps .st .i-x.
  • favicon.svg keeps its <style> (it switches colours for dark mode). The CSP allows exactly that block by its sha256 hash: 'sha256-y8K55UD19BmUb/68u/uDinnnnZwD4JjBVgr61zWsTLw='.
  • tools/check-site.mjs now fails on:
    • a style attribute or <style> element in a page,
    • a style attribute in markup built in a script, or setAttribute('style', …),
    • an SVG <style> whose hash is not in the CSP (the message prints the hash to add).
  • Docs: the residual risk is removed from ASSURANCE.md; CONTRIBUTING and CHANGELOG describe the stricter policy.

Verification

Check Result
node tools/check-site.mjs ok, 30 files
check-site negative tests: style= in en/index.html, style= back in sanad-core.js, a colour changed in favicon.svg all three fail with the expected message
npm run lint exit 0
npm test (43 unit + 29 e2e under the Caddyfile CSP + coverage gate) exit 0, run 3 times
Computed styles of every changed element (heading size and margin, lead margin, meta margin, traveller opacity and fill, step icon position) on /, /en/, /ru/ at 1280px and 390px, main vs this branch identical, 0 console errors

Checklist

  • node tools/check-site.mjs and npm test pass
  • New behaviour or a fixed bug has a test (check-site.mjs rules, run in CI)
  • All three language pages (/, /en/, /ru/) are updated where needed
  • User-visible change is in CHANGELOG.md under [Unreleased]
  • Every commit is signed off (git commit -s, DCO)
  • No inline scripts, no third-party origins, no innerHTML with data (docs/ASSURANCE.md)

Related issue: Closes #7

Five style attributes in each home page and one in the demo's step icon
markup kept 'unsafe-inline' in the CSP. They are now classes in sanad.css,
so the CSP can use style-src 'self'.

- Home pages (uz, en, ru): the chain traveller, the demo meta line and the
  Telegram heading and lead use classes; the computed styles are unchanged.
- sanad-core.js: the step "x" icon gets position:absolute from .st .i-x.
- favicon.svg keeps its <style> (it switches colours for dark mode); the CSP
  allows exactly that block by its sha256 hash.
- tools/check-site.mjs fails on a style attribute or <style> in a page, a
  style attribute in script markup, and an SVG <style> whose hash is not in
  the CSP, so an inline style cannot come back unnoticed.
- ASSURANCE.md: the residual risk is closed; CONTRIBUTING and CHANGELOG
  describe the stricter policy.

Closes #7

Signed-off-by: ismoilovdevml <ismoilovdevarchlinux@gmail.com>
@ismoilovdevml
ismoilovdevml requested a review from a team as a code owner November 14, 2026 15:41
main already has the same CSP change from #22. This keeps main's version and adds what is new here: check-site also fails on setAttribute('style', ...) in scripts.

Signed-off-by: xnosir <283333996+xnosir@users.noreply.github.com>

@xnosir xnosir left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! main already had the same CSP change from #22, so I merged main into your branch (you allowed maintainer edits) and kept what is new here: check-site now also fails on setAttribute('style', ...). Checked with a negative test; CI green.

@wonmors
wonmors merged commit 4879b2b into wonmortech:main Nov 14, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants