fix(security): remove CSRF leak and add CSP headers#504
Open
prince-shakyaa wants to merge 2 commits into
Open
Conversation
Removed the CSRF token from the JSON response of /api/session/status to prevent XSS leakage. Replaced the deprecated X-XSS-Protection header with a modern Content-Security-Policy and Referrer-Policy header. Fixes GenAI-Security-Project#503
Author
Added tailwindcss, jsdelivr, gravatar, and websocket protocols to the Content-Security-Policy to ensure the frontend loads correctly without breaking styles or live updates.
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
Fixes two security issues in the HTTP response layer: removes the CSRF token from a
publicly accessible JSON endpoint and adds a
Content-Security-Policyheader whileremoving the deprecated
X-XSS-Protectionheader.Fixes #503
Changes
finbot/main.py1. CSRF token removed from
/api/session/statusresponse.CSRF tokens must only be injected into HTML (meta tags / hidden form fields).
Returning them from a GET JSON endpoint allows any XSS payload to trivially read
and replay the token.
return { "session_id": session_context.session_id[:8] + "...", "user_id": session_context.user_id, "is_temporary": session_context.is_temporary, "namespace": session_context.namespace, "security_status": session_context.get_security_status(), - "csrf_token": session_context.csrf_token, + # csrf_token intentionally omitted - injected via HTML meta tag only }finbot/core/auth/middleware.py2. Added
Content-Security-PolicyandReferrer-Policy; removed deprecatedX-XSS-Protection.def _add_security_headers(self, response: Response): response.headers["X-Content-Type-Options"] = "nosniff" response.headers["X-Frame-Options"] = "DENY" - response.headers["X-XSS-Protection"] = "1; mode=block" + response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin" + response.headers["Content-Security-Policy"] = ( + "default-src 'self'; " + "script-src 'self' 'unsafe-inline' https://cdnjs.cloudflare.com; " + "style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; " + "font-src 'self' https://fonts.gstatic.com; " + "img-src 'self' data:; " + "connect-src 'self';" + )Why This Matters
csrf_tokenin GET JSON responsefetch()Content-Security-Policyheader<script>tags execute without restrictionX-XSS-Protection: 1; mode=blockTesting
GET /api/session/statusresponse no longer containscsrf_tokenfield/auth/magic-linketc.) still work correctly —token is read from the HTML meta tag as before
Content-Security-Policyon every pageX-XSS-Protectionis no longer present in response headersReferrer-Policy: strict-origin-when-cross-originis present in response headersNotes for Reviewers
'unsafe-inline'for scripts and styles because the currenttemplates use inline
<script>and<style>blocks. A follow-up issue shouldintroduce nonce-based CSP to remove
'unsafe-inline'entirely.tags in the base templates; this PR only removes the redundant JSON exposure.