Repository navigation
fix(api): reject unauthorized bodies before reading them - #596
Open
walsykerem wants to merge 2 commits into
Open
walsykerem wants to merge 2 commits into
walsykerem wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ 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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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 /volumesorPOST /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.goand its one call site incmd/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
OapiRequestValidatorinlib/middleware/oapi_auth.gothat wraps the existing validator: for secured routes with a body, it authenticates first (viarouteRequiresAuth+authenticateRequest), setsr.Bodytohttp.NoBodywhen credentials fail so the validator never reads the upload, and caches the result in context forOapiAuthenticationFuncto reuse.cmd/api/main.gonow wires this helper instead of inliningOapiRequestValidatorWithOptions.Reviewed by Cursor Bugbot for commit b654c4d. Bugbot is set up for automated code reviews on this repo. Configure here.