fix: Enforce Authentication on CTF Sidecar Widget Endpoint#512
fix: Enforce Authentication on CTF Sidecar Widget Endpoint#512prince-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
|
Hiii @saikishu , @e2hln |
|
Would this break an unauthenticated users ability to fully explore the app without sharing an email? The ability to do so is attractive to some people first viewing the app. |
|
@joshlochner The app has two session tiers:
What an unauthenticated (temporary session) user can still do — unchanged by this PR:
What this PR restricts — only the sidecar widget:
The sidecar widget is specifically a personalised dashboard -it returns your The fix gates the one endpoint that was leaking a personal data view to sessions |
|
I was under the impression that GET /api/v1/challenges/{id} was going to be impacted by this, if not, sounds good |
Yes, that's intentional! I wanted to leave the challenge details accessible to unauthenticated/temporary sessions so people can preview them. Only the sidecar needed to be locked down. |
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