Skip to content

fix(auth): remove X-User-Id identity trust#76

Open
Ayush-Raj-Chourasia wants to merge 3 commits into
Stanzin7:masterfrom
Ayush-Raj-Chourasia:fix/remove-x-user-id-identity-trust
Open

fix(auth): remove X-User-Id identity trust#76
Ayush-Raj-Chourasia wants to merge 3 commits into
Stanzin7:masterfrom
Ayush-Raj-Chourasia:fix/remove-x-user-id-identity-trust

Conversation

@Ayush-Raj-Chourasia
Copy link
Copy Markdown

@Ayush-Raj-Chourasia Ayush-Raj-Chourasia commented Apr 2, 2026

Summary

This PR fixes an authentication bypass by removing support for client-controlled identity headers.

Changes made

  • Removed identity fallback from the X-User-Id header
  • Restricted identity source to the authenticated request state user_id
  • Extracted the identity helper into a dedicated module for isolated testing
  • Added regression tests to verify that header spoofing is ignored

Security impact

Unauthenticated requests can no longer inject arbitrary user IDs through request headers.

Testing

  • PYTHONPATH=src python -m pytest -q tests/api/test_user_identity_source.py
  • Result: 3 passed

Linked issue

Closes #50

Copilot AI review requested due to automatic review settings April 2, 2026 21:38
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes trust in the client-controlled X-User-Id header and ensures API user identity is sourced exclusively from authenticated request state, closing the header-spoofing authentication bypass described in #50.

Changes:

  • Replaced the inline _get_user_id() implementation in api/main.py with a dedicated auth_identity.get_user_id() helper that only reads request.state.user_id.
  • Added regression tests ensuring X-User-Id is ignored even when present.
  • Introduced a lightweight auth_identity module to enable isolated testing without pulling in heavier API/runtime dependencies.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/extension_shield/api/main.py Removes header-based identity fallback and uses the new helper for user identity resolution.
src/extension_shield/api/auth_identity.py Adds a small, testable helper that derives identity only from authenticated request state.
tests/api/test_user_identity_source.py Adds regression coverage proving header spoofing does not affect identity.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings April 2, 2026 21:59
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

raise HTTPException(status_code=404, detail="Extension not found")

if not can_view_private_scan(user_id, results):
raise HTTPException(status_code=404, detail="File not found")
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 404 raised on private-scan authorization failure uses detail="File not found", but this endpoint returns a file list (not a single file). Consider using a more accurate generic detail (e.g., "Not found" / "Scan results not found") to avoid confusing clients while still not leaking existence.

Suggested change
raise HTTPException(status_code=404, detail="File not found")
raise HTTPException(status_code=404, detail="Scan results not found")

Copilot uses AI. Check for mistakes.
raise HTTPException(status_code=404, detail="Extension not found")

if not can_view_private_scan(user_id, results):
raise HTTPException(status_code=404, detail="File not found")
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Authorization failure for private scans returns a 404 with detail="File not found". Since the failure is about access to the scan rather than the specific path, consider using the same generic 404 detail used by /api/scan/results/{identifier} (e.g., "Scan results not found") for consistency while still avoiding existence leaks.

Suggested change
raise HTTPException(status_code=404, detail="File not found")
raise HTTPException(status_code=404, detail="Scan results not found")

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +7
"""
Tests for API user identity extraction helper.

Security regression coverage:
- Identity must come from authenticated request state only.
- X-User-Id header must never be trusted.
"""
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description lists 3 passed, but this new test module defines 6 test_... functions. Please update the PR description’s test result (or clarify what subset was run) so reviewers can accurately assess coverage.

Copilot uses AI. Check for mistakes.
@github-actions github-actions Bot added the area: backend Changes to the Python backend and scanning pipeline label Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: backend Changes to the Python backend and scanning pipeline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Authentication bypass via X-User-Id header

2 participants