fix: Enforce Authentication on CTF Sidecar Widget Endpoint#512
Open
prince-shakyaa wants to merge 1 commit into
Open
fix: Enforce Authentication on CTF Sidecar Widget Endpoint#512prince-shakyaa wants to merge 1 commit into
prince-shakyaa wants to merge 1 commit into
Conversation
Swap get_session_context → get_authenticated_session_context on GET /api/v1/sidecar so temporary/anonymous sessions are rejected with HTTP 401 instead of silently returning HTTP 200. Every other personal-data endpoint in the CTF app already uses get_authenticated_session_context; this brings sidecar into line. Fixes GenAI-Security-Project#511
Author
|
Hiii @saikishu , @e2hln |
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.
fix: Enforce Authentication on CTF Sidecar Widget Endpoint
Fixes #511
Summary
The
GET /api/v1/sidecarendpoint was usingget_session_context(which allowsanonymous/temporary sessions) instead of
get_authenticated_session_context(whichenforces a bound-email session). This meant any unauthenticated visitor could call this
endpoint and receive an HTTP 200 instead of HTTP 401.
This PR fixes the dependency with a 2-line code change and adds a dedicated unit test
file to prevent regressions.
Changes
1. Code Fix :
finbot/apps/ctf/routes/sidecar.pyWhat: Swap the FastAPI dependency on
get_sidecar_datafrom the weakerget_session_contextto the authenticatedget_authenticated_session_context.Why:
get_authenticated_session_contextraisesHTTP 401 Unauthorizedwhensession_context.is_temporaryisTrue(i.e., the caller has not bound their email).This matches the auth guard used by every other personal-data endpoint in the CTF app
(
/api/v1/profile,PUT /api/v1/profile,PUT /api/v1/profile/featured-badges).Lines changed: 2
Risk: Very low - no change in behavior for authenticated users.
2. New Test File :
tests/unit/apps/ctf/test_sidecar_auth.pyA new unit test file modelled on the existing
test_share_card_cache_key.pypattern.Tests included:
test_temporary_session_is_rejectedis_temporary=Trueraises HTTP 401test_authenticated_session_is_acceptedis_temporary=Falsedoes NOT raise 401test_auth_guard_uses_correct_dependencyget_sidecar_datafunction depends onget_authenticated_session_contextand NOTget_session_contexttest_unauthenticated_request_returns_401/api/v1/sidecarwith no session cookie returns HTTP 401Approach: Following the same style as
test_share_card_cache_key.py:@pytest.mark.unitfastapi.testclient.TestClientfor the end-to-end route testTest file location:
tests/unit/apps/ctf/test_sidecar_auth.pyFiles Changed
finbot/apps/ctf/routes/sidecar.pytests/unit/apps/ctf/test_sidecar_auth.pyTotal: 2 files, ~122 lines
Before vs After
GET /api/v1/sidecarGET /api/v1/sidecarHow to Test
Checklist
sidecar.py(2 lines)test_sidecar_auth.pycreatedCloses #511