Skip to content

fix(api): reject unauthorized bodies before reading them - #596

Open
walsykerem wants to merge 2 commits into
kernel:mainfrom
walsykerem:unauth-body-before-auth-problem
Open

walsykerem wants to merge 2 commits into
kernel:mainfrom
walsykerem:unauth-body-before-auth-problem

Conversation

@walsykerem

@walsykerem walsykerem commented Nov 21, 2026 •

Copy link
Copy Markdown

Anonymous requests are buffered in full before authentication

The API checks every request against its OpenAPI spec before any handler runs, and its validation library reads the entire body into memory during the security check, before the bearer token is examined. An anonymous client can post hundreds of megabytes to POST /volumes or POST /builds, get a 401, and still force the process to hold it all. A 300 MiB anonymous body moved resident memory from 41 MB to 835 MB.

Authentication now happens before the body costs anything. The validator is wrapped in one small function that, when a request carries no Authorization header, replaces the body with an empty one so no decoding starts, and lets the validator reject it as before. Callers get the same 401 status and message, and requests that do present credentials are untouched. The change is limited to the validator wiring in lib/middleware/oapi_auth.go and its one call site in cmd/api/main.go.

An 8 MiB body with no credentials now consumes 0 body bytes server side, against 8 MiB before, and the response stays 401. The regression check runs through the same constructor the server uses, so deleting the guard makes it fail again.


Note

Medium Risk
Changes authentication ordering in the OpenAPI validation path for all secured routes; intended behavior is unchanged for valid tokens but this is security-sensitive middleware.

Overview
Fixes a DoS vector where OpenAPI validation buffered the full request body before bearer auth ran, so anonymous clients could upload huge payloads and still get 401 while spiking memory.

Adds OapiRequestValidator in lib/middleware/oapi_auth.go that wraps the existing validator: for secured routes with a body, it authenticates first (via routeRequiresAuth + authenticateRequest), sets r.Body to http.NoBody when credentials fail so the validator never reads the upload, and caches the result in context for OapiAuthenticationFunc to reuse. cmd/api/main.go now wires this helper instead of inlining OapiRequestValidatorWithOptions.

Reviewed by Cursor Bugbot for commit b654c4d. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit db9c713. Configure here.

Comment thread lib/middleware/oapi_auth.go Resolved
The previous guard hid the body only when the Authorization header was
absent, so any bogus header value (a non-bearer scheme, a malformed or
revoked token) still drove the OpenAPI validator to read the whole body
into memory before authentication rejected the request.

The wrapper now authenticates first: it resolves the matched operation
and, when that operation is secured, reuses the existing token
extraction and validation path. A credential that cannot succeed hides
the body before the validator runs; a credential that succeeds attaches
the claims and passes the request through untouched.
OapiAuthenticationFunc returns the pre-checked result instead of
verifying the token again, so 401/403 bodies, messages, logs and
revocation handling stay exactly as before. Requests with no body and
unsecured operations are unchanged, and the registry routes are mounted
outside this middleware.

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.

1 participant