Repository navigation
fix(proxy): stop leaking database errors from /spend/logs and /key/info - #46175
libaojiang wants to merge 7 commits into
Conversation
A null byte in a string query param makes Postgres reject the statement, and the raw driver error used to reach the caller in the 500 body of /spend/logs and /key/info. Reject null bytes up front with a 400, and when the database does fail under /spend/logs, keep the full exception in the server logs while returning a generic message. Fixes BerriAI#46076
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
The type discipline gate counts **kwargs signatures as LIT008 and rejects any growth over the merge-base count.
ef5d3e6 to
05ce6e2
Compare
This reverts commit 10015c5.
720bad1 to
19a65aa
Compare
| # Resolve key_aliases to tokens so we never pass token=None (unbounded query) | ||
| tokens_to_query: Final = list(data.keys) if data.keys else [] | ||
| for token in tokens_to_query: | ||
| validate_no_null_bytes({"keys": token}) |
There was a problem hiding this comment.
Medium: New validation does not cover alias lookups
This loop validates only data.keys, so an authenticated request containing {"key_aliases":["test\u0000"]} passes the NUL-containing alias directly to find_many; unexpected database errors then reach the response through handle_exception_on_proxy, whose _client_message preserves str(e). Extend the pre-query validation to aliases and return a generic response for unexpected errors.
PR overviewThe PR updates error handling for /spend/logs and /key/info to stop returning database error details, including adding validation to key lookups. One issue remains open: validation covers keys but not aliases, allowing an authenticated caller to submit a NUL-containing alias that triggers a database error whose details are returned in the response. This leaves a limited error-information disclosure path in the intended fix; no issues have been addressed yet. Open issues (1)
Fixed/addressed: 0 · PR risk: 5/10 |
Requests containing null bytes now receive HTTP 400 before reaching the database. Unexpected /spend/logs failures return a generic message while keeping the full error in server logs. The branch is synchronized with current main and retains its newer /key/info sanitization tests
Details
TLDR
Problem this solves:
How it solves it:
User Flow
Before, malformed management queries return HTTP 500
After, the same requests receive an actionable validation error
Relevant issues
Fixes #46076
Pre-Submission checklist
Validation
Four focused null-byte and sanitized database-error cases and the PostgreSQL HTTP proof below were recorded at 720bad1
Changed Python files parse, import-order checks pass and scoped type-discipline counts do not grow. Fresh runtime tests cannot collect locally because the new main requires the compiled Rust bridge, which is unavailable in this Windows environment. Latest GitHub Actions runs require maintainer approval before execution, and fresh CI and CLA results remain pending
Screenshots / Proof of Fix
Current tip 19a65aa retains the earlier live proof below. The null-byte validator and bulk-key validation have matching ASTs with the tested tip. The single-key and spend-log handlers preserve rejection and sanitization while retaining main's error-param normalization. The spend-log validator is imported locally to follow main's circular-import guidance
Ran the standard proxy CLI against an isolated real PostgreSQL database, with no mocked database operations. Before uses main at dddbaa8 on port 17940. After uses this PR at 720bad1 on port 17941. MASTER_KEY below is the local fixture credential
Before (dddbaa8)
Spend logs
curl -sS -H "Authorization: Bearer $MASTER_KEY" "http://127.0.0.1:17940/spend/logs?api_key=test%00"PostgresErrorandinvalid byte sequence for encoding "UTF8": 0x00Key info
curl -sS -H "Authorization: Bearer $MASTER_KEY" "http://127.0.0.1:17940/key/info?key=test%00"Internal server error from /key/info. Details are in the proxy server logs.After (720bad1)
Spend logs
curl -sS -H "Authorization: Bearer $MASTER_KEY" "http://127.0.0.1:17941/spend/logs?api_key=test%00"{"detail":{"error":"api_key must not contain null bytes"}}Key info
curl -sS -H "Authorization: Bearer $MASTER_KEY" "http://127.0.0.1:17941/key/info?key=test%00"{"detail":{"error":"key must not contain null bytes"}}Type
Bug fix
Dependency follow-up
Synchronized with main at 527d882, which already includes #48302. This PR has no manifest, lockfile, OSV policy or workflow changes relative to main
Removed the obsolete merge ancestry that introduced an unsigned committer. Original feature commits are retained, and the previous head is backed up on the fork