api: keep the OpenAPI validator off upload bodies so kin-openapi can be upgraded - #10531
Open
drewmaring wants to merge 1 commit into
Open
Conversation
…be upgraded kin-openapi >= 0.132 reads the entire request body in openapi3filter.ValidateSecurityRequirements, before any AuthenticationFunc runs, so that an authenticator could re-read it (getkin/kin-openapi#1064). For lakeFS that means every uploadObject body is buffered in memory, and even unauthenticated uploads are read in full before the 401. That is the regression behind treeverse#9409, which pinned kin-openapi at 0.131.0 and blocks the dependabot bump in treeverse#10505. Options.ExcludeRequestBody does not help because the read happens earlier. lakeFS never needs the validator to see an upload body: uploadObject is marked x-validation-exclude-body and the handler streams the body to the block store itself. So for routes with that extension, and for any application/octet-stream or multipart/form-data request, hand the validator a body-less clone of the request. The handler still receives the original request. JSON bodies are still validated. Measured on a 300 MB upload (VmRSS peak of the lakefs process): v1.86.0 217 MB; v1.86.0 with kin-openapi 0.149.0 990 MB; the same with this change 215 MB. An unauthenticated upload is rejected in ~6 ms with no growth. Malformed JSON still returns 400. The new test asserts the validator reads zero bytes of an upload body and that the handler still gets the whole body; it fails on kin-openapi 0.149.0 without this change and passes with it. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
Drew Maring seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
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.
Related: #9409 (the regression this removes), #10505 (the kin-openapi bump this unblocks)
Change Description
Background
Since #9408 lakeFS pins
github.com/getkin/kin-openapiat 0.131.0 because 0.132+ made uploads read the whole object into memory. That pin now carries a CRITICAL advisory (GHSA-r277-6w6q-xmqw) and two HIGH ones, and blocks the dependabot bump in #10505. Root-causing the memory growth showed it is not in request-body validation at all, so it can be fixed on the lakeFS side without waiting for kin-openapi.Bug Fix
uploadObjectdrives the lakefs process from ~215 MB to ~990 MB RSS, and an unauthenticated upload is read in full before the 401 (lakeFS memory grows while upload object #9409).openapi3filter.ValidateSecurityRequirementsreads the entire request body up front so that anAuthenticationFunccan re-read it (openapi3filter: don't consume request body inAuthenticatorFuncgetkin/kin-openapi#1064). It runs before request-body validation, soOptions.ExcludeRequestBodyand thex-validation-exclude-bodyextension never get a chance to skip it.validateRequest, for routes markedx-validation-exclude-bodyand for anyapplication/octet-stream/multipart/form-datarequest, give the validator a body-less clone of the request (Body = http.NoBody). The handler still receives the original request and streams the body to the block store as before. lakeFS usesNoopAuthenticationFunc, so nothing in the validator needs the body. JSON bodies are unchanged and still validated.Testing Details
pkg/api/validate_request_test.go: the validator reads zero bytes of an upload body and the handler still receives all of it; a malformed JSON body still yields 400. Fails on kin-openapi 0.149.0 without the change, passes with it (and on 0.131.0).go test ./pkg/api -run TestValidateRequest,go vet ./pkg/api,gofmtclean.lakefs run --quickstart, 300 MBuploadObject, VmRSS of PID 1 sampled during the upload): v1.86.0 217 MB peak; v1.86.0 + kin-openapi 0.149.0 990 MB; v1.86.0 + kin-openapi 0.149.0 + this change 215 MB. Unauthenticated upload with the change: 401 in ~6 ms, no growth.This PR deliberately does not bump kin-openapi itself, so it is safe on the current pin; with it merged, #10505 (or a fresh bump to 0.149.0, which also needs
apache/thrift0.24.0 andgrpc1.83.1 for the remaining advisories) should be clear to land.Breaking Change?
No. Behaviour only changes for requests whose body the validator was already told to skip.