fix(security): protect rag streaming endpoints with internal auth#548
fix(security): protect rag streaming endpoints with internal auth#548Namraa310806 wants to merge 2 commits into
Conversation
|
Warning Review limit reached
More reviews will be available in 25 minutes and 5 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Suggested GSSoC Labels: |
|
@Namraa310806 is attempting to deploy a commit to the firefistisdead's projects Team on Vercel. A member of the Team first needs to authorize it. |
| except Exception: | ||
| pass | ||
|
|
||
| return StreamingResponse(_generate_and_stream(), media_type="text/event-stream; charset=utf-8") |
Summary
This PR fixes a security issue where protected RAG service endpoints could potentially be accessed directly, bypassing the intended API gateway protections.
The update strengthens service-to-service authentication by protecting streaming endpoints, introducing prefix-based route protection, normalizing route matching behavior, and adding comprehensive regression tests to prevent future authentication bypasses.
Changes Made
Internal Authentication Protection
/ask/stream.X-Internal-Tokenfor protected routes.Prefix-Based Route Protection
Added subtree-level protection for:
This ensures any future nested endpoints are automatically protected without requiring manual updates.
Examples:
are now protected automatically.
Route Normalization
Added path normalization to prevent authentication bypass through trailing slash variations.
Examples:
are now treated identically by the middleware.
Secure Token Validation
secrets.compare_digest()for constant-time token comparison.Fail-Closed Security Behavior
When
INTERNAL_RAG_TOKENis not configured:Regression Test Coverage
Added automated tests covering:
/ask/streamprotectionSecurity Impact
This change prevents direct access to internal RAG service endpoints when the service is exposed outside the intended gateway architecture.
The fix reduces the risk of:
Files Modified
rag-service/main.pyrag-service/tests/test_main.pyVerification Checklist
/ask/streamprotectedRelated Issue
Fixes: #500