Security: Harden CSP by removing unsafe-inline and unsafe-eval (#325)#500
Merged
filthyrake merged 2 commits intodevfrom Jan 4, 2026
Merged
Security: Harden CSP by removing unsafe-inline and unsafe-eval (#325)#500filthyrake merged 2 commits intodevfrom
filthyrake merged 2 commits intodevfrom
Conversation
- Remove 'unsafe-inline' and 'unsafe-eval' from script-src directives - Remove 'unsafe-inline' from style-src directives - Replace Alpine.js standard build with CSP-compatible build - Public pages: alpine.csp.min.js from @alpinejs/csp - Admin: npm package @alpinejs/csp instead of alpinejs - Extract inline JavaScript to external files: - web/public/static/js/pages/home.js (from index.html) - web/public/static/js/pages/category.js (from category.html) - web/public/static/js/pages/tag.js (from tag.html) - Move [x-cloak] CSS to external stylesheets: - web/public/static/css/bundle.css - web/admin/src/styles/tokens.css - Add TypeScript declaration for @alpinejs/csp module The CSP-compatible Alpine.js build avoids using eval() and Function() constructors, enabling stricter Content Security Policy headers that provide real XSS protection. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Fix global namespace pollution in page JS files: - Wrap all page components in IIFEs with 'use strict' - Register components via Alpine.data() instead of global functions - Encapsulate constants (MAX_SEARCH_LENGTH, SLUG_PATTERN) in IIFE scope - Improve script loading consistency: - Add defer attribute to all page script tags - Remove parentheses from x-data attributes (use string reference) - Ensure consistent loading order across all pages - Strengthen CSP headers: - Add object-src 'none' to block plugin-based XSS vectors - Add base-uri 'self' to prevent base tag injection attacks - Apply to all HTML files (index, category, tag, watch, admin) - Document vendor file provenance: - Add source URL, version, license info to alpine.csp.min.js - Include instructions for verifying integrity 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.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.
Summary
This PR addresses the security issue #325 by hardening the Content Security Policy (CSP) headers to remove
'unsafe-inline'and'unsafe-eval'directives that were defeating XSS protection.Changes
Remove unsafe CSP directives from all HTML files:
script-src 'self'(removed'unsafe-inline'and'unsafe-eval')style-src 'self'(removed'unsafe-inline')Replace Alpine.js with CSP-compatible build:
alpine.csp.min.jsfrom@alpinejs/cspCDNalpinejsto@alpinejs/cspExtract inline scripts to external files:
web/public/static/js/pages/home.js- Home page Alpine componentweb/public/static/js/pages/category.js- Category page Alpine componentweb/public/static/js/pages/tag.js- Tag page Alpine componentMove inline styles to external CSS:
[x-cloak]rule toweb/public/static/css/bundle.css[x-cloak]rule toweb/admin/src/styles/tokens.cssTypeScript support:
@alpinejs/cspmoduleSecurity Impact
The CSP-compatible Alpine.js build avoids using
eval()andFunction()constructors, allowing stricter CSP headers that provide actual XSS protection. Previously, the'unsafe-inline'and'unsafe-eval'directives made the CSP essentially ineffective against XSS attacks.Files Changed
web/public/index.htmlweb/public/category.htmlweb/public/tag.htmlweb/public/watch.htmlweb/admin/index.htmlweb/admin/package.jsonweb/admin/src/main.tsweb/public/static/css/bundle.cssweb/admin/src/styles/tokens.cssweb/public/static/js/pages/home.jsweb/public/static/js/pages/category.jsweb/public/static/js/pages/tag.jsweb/public/static/vendor/alpine.csp.min.jsweb/admin/src/types/alpinejs-csp.d.tsTest plan
Closes #325
🤖 Generated with Claude Code