Skip to content

fix(proxy): stop leaking database errors from /spend/logs and /key/info - #46175

Open
libaojiang wants to merge 7 commits into
BerriAI:mainfrom
libaojiang:fix-mgmt-endpoint-exception-leak
Open

libaojiang wants to merge 7 commits into
BerriAI:mainfrom
libaojiang:fix-mgmt-endpoint-exception-leak

Conversation

@libaojiang

@libaojiang libaojiang commented Oct 13, 2026 •

Copy link
Copy Markdown

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:

  • Malformed query parameters expose database errors through /spend/logs
  • Null bytes reach key-info database queries

How it solves it:

  • Reject null bytes before querying
  • Log unexpected failures and return a generic error

User Flow

Before, malformed management queries return HTTP 500

  1. An operator sends GET /spend/logs?api_key=test%00 with admin authentication
  2. The response includes a PostgreSQL invalid-byte error
  3. GET /key/info?key=test%00 also returns HTTP 500

After, the same requests receive an actionable validation error

  1. The operator sends GET /spend/logs?api_key=test%00 with admin authentication
  2. HTTP 400 reports that api_key must not contain null bytes
  3. GET /key/info?key=test%00 returns the equivalent HTTP 400

Relevant issues

Fixes #46076

Pre-Submission checklist

  • I have added meaningful tests
  • The focused tests covering my change pass locally
  • My PR passes all required CI/CD checks
  • My PR addresses one management-error problem
  • Every automated review on the latest commit has finished and its findings are addressed

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

  1. Run curl -sS -H "Authorization: Bearer $MASTER_KEY" "http://127.0.0.1:17940/spend/logs?api_key=test%00"
  2. HTTP 500 includes PostgresError and invalid byte sequence for encoding "UTF8": 0x00

Key info

  1. Run curl -sS -H "Authorization: Bearer $MASTER_KEY" "http://127.0.0.1:17940/key/info?key=test%00"
  2. HTTP 500 returns Internal server error from /key/info. Details are in the proxy server logs.

After (720bad1)

Spend logs

  1. Run curl -sS -H "Authorization: Bearer $MASTER_KEY" "http://127.0.0.1:17941/spend/logs?api_key=test%00"
  2. HTTP 400 returns {"detail":{"error":"api_key must not contain null bytes"}}

Key info

  1. Run curl -sS -H "Authorization: Bearer $MASTER_KEY" "http://127.0.0.1:17941/key/info?key=test%00"
  2. HTTP 400 returns {"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

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
@codspeed

codspeed Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 35 untouched benchmarks


Comparing libaojiang:fix-mgmt-endpoint-exception-leak (720bad1) with main (72f1ac4)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (dddbaa8) during the generation of this report, so 72f1ac4 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

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.
@libaojiang
libaojiang requested a review from a team October 22, 2026 08:22
@CLAassistant

CLAassistant commented Nov 3, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@libaojiang
libaojiang force-pushed the fix-mgmt-endpoint-exception-leak branch from 720bad1 to 19a65aa Compare November 4, 2026 07:41
# 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})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@veria-ai

veria-ai Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

PR overview

The 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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Management endpoints return raw database exception text in 500 responses

2 participants