fix/sessions token exposed in url query parameters #42
Open
ilikeworking2005 wants to merge 2 commits intowindoze95:mainfrom
Open
fix/sessions token exposed in url query parameters #42ilikeworking2005 wants to merge 2 commits intowindoze95:mainfrom
ilikeworking2005 wants to merge 2 commits intowindoze95:mainfrom
Conversation
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.
Overview
This PR addresses Issue #30, where highly sensitive session tokens were being exposed in URL query parameters for the video streaming and preview endpoints. To secure the authentication flow and prevent token leakage in server logs and browser history, this PR migrates the application from using URL query parameters and custom headers (X-User-Token) to HttpOnly Secure Cookies.
Changes Proposed
1. app/api/auth.py (Login/Session Creation): Updated the /select route to inject the Response object. Upon successful session creation, the token is now attached to the HTTP response as an HttpOnly, Secure, and SameSite=Lax cookie rather than just being returned in the JSON body.
2. app/api/auth.py (Auth Dependency): Refactored the get_current_user dependency to read the incoming Request object. It now extracts the session token directly from request.cookies.get("session_token") instead of relying on the X-User-Token header.
3. app/api/videos.py (Video Endpoints): Removed the token URL query parameter from both /{video_id}/stream and /{video_id}/preview-stream. These routes now rely entirely on the updated get_current_user cookie dependency.
Security Benefits
By leveraging browser-managed cookies, we resolve the vulnerabilities outlined in the original issue while gaining additional security benefits:
- No URL Leakage: Session tokens will no longer appear in proxy logs, server access logs, or the user's browser history.
- XSS Protection: Setting the HttpOnly flag ensures that malicious JavaScript cannot access or exfiltrate the session token if a Cross-Site Scripting vulnerability is ever introduced.
- Native Media Support: HTML
Breaking Changes & Frontend Impact
This PR introduces a breaking change to how the frontend communicates with the API. The frontend developer will need to implement the following updates:
- Remove Manual Headers: The frontend no longer needs to manually attach the X-User-Token header to API calls.
- Enable Credentials: All XHR, Fetch, or Axios requests must be configured to include credentials (e.g., credentials: "include" in Fetch, or withCredentials: true in Axios) so the browser knows to send the cookie.
- Video Tags: Video players can now simply use
How to Test
Check out this branch and start the backend server.
Authenticate via the /select endpoint and verify via your browser's DevTools (Application > Cookies tab) that the session_token cookie is set correctly with the HttpOnly flag.
Attempt to access a protected API route without the cookie (e.g., via standard curl or incognito window); verify it returns a 401 Unauthorized.
Load a video stream using a standard HTML