fix: replace unsafe-inline CSP with per-request nonce#783
Open
rager306 wants to merge 1 commit intoRightNow-AI:mainfrom
Open
fix: replace unsafe-inline CSP with per-request nonce#783rager306 wants to merge 1 commit intoRightNow-AI:mainfrom
rager306 wants to merge 1 commit intoRightNow-AI:mainfrom
Conversation
The dashboard CSP uses 'unsafe-inline' for script-src, which permits
any inline <script> block to execute — including attacker-injected
scripts if any endpoint reflects user input (agent names, message
content, channel descriptions, etc.).
Replace with a per-request cryptographic nonce (UUID v4):
- webchat_page generates a unique nonce on every request
- All <script> tags embed the nonce at compile time via __NONCE__ placeholder
- CSP becomes: script-src 'self' 'nonce-{nonce}' 'unsafe-eval'
('unsafe-eval' is still required for Alpine.js x-data expressions)
- API endpoints receive a strict default-src 'none'; frame-ancestors 'none'
policy instead of the permissive dashboard policy
This is a standard CSP Level 2 hardening; all modern browsers support nonces.
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
The dashboard
Content-Security-Policycurrently allows'unsafe-inline'forscript-src:'unsafe-inline'permits any inline<script>block to execute, including attacker-injected scripts. If any part of the dashboard reflects user-controlled data — agent names, message content, channel descriptions, persona fields — an attacker can store a payload that executes JavaScript in the admin's browser (stored XSS).Fix
Generate a cryptographic nonce (
uuid::Uuid::new_v4()) on every dashboard request. All<script>tags carrynonce="__NONCE__"at compile time; the nonce is substituted at response time. The CSP becomes:(
'unsafe-eval'is retained — required for Alpine.jsx-dataexpression evaluation.)API endpoints receive a separate strict policy:
default-src 'none'; frame-ancestors 'none'.'unsafe-inline'is fully removed. No new dependencies —uuidis already a workspace dependency.Testing
cargo build -p openfang-apipasses'unsafe-inline'removed from CSP