Skip to content

fix: trim Options.Prefix in place so the request body survives security validation - #17

Open
blacksud0 wants to merge 1 commit into
oapi-codegen:mainfrom
blacksud0:fix-prefix-body-drain
Open

fix: trim Options.Prefix in place so the request body survives security validation#17
blacksud0 wants to merge 1 commit into
oapi-codegen:mainfrom
blacksud0:fix-prefix-body-drain

Conversation

@blacksud0

Copy link
Copy Markdown

Fixes #16.

Request.Clone copies the Body reference. With Options.Prefix set and a security scheme in the spec, openapi3filter's security validation reads the body from the live request and restores a fresh reader onto the clone only — so every POST/PUT/PATCH through a prefixed mount reached its handler with a drained, zero-byte body (even with ExcludeRequestBody enabled, since the read belongs to the security path).

This trims URL.Path — and RawPath, which the clone approach left untrimmed — in place, restoring both in a defer. RequestValidationInput.Request stays the live request, so the body restore lands where it is needed; the restore runs before next, so handlers still observe the original path and the existing prefix test's path assertion keeps passing.

The new regression test fails against the previous clone-based handling (the handler received a drained body … was []) and passes with this change. Full suite green. This is the fix we have been running in production as a vendored copy.

…ty validation

Request.Clone copies the Body reference. With Prefix set and a security
scheme in the spec, openapi3filter's security validation reads the body
from the live request and restores a fresh reader onto the clone only, so
every POST/PUT/PATCH through a prefixed mount reached its handler with a
drained, zero-byte body — even with ExcludeRequestBody enabled, since the
read belongs to the security path.

Trimming URL.Path (and RawPath, which the clone approach left untrimmed)
in place with a deferred restore keeps RequestValidationInput.Request the
live request, so the body restore lands where it is needed, and the
restore runs before next so handlers still observe the original path —
the existing prefix test's path assertion keeps passing.

The new regression test fails against the previous clone-based handling
and passes with this change.

Fixes oapi-codegen#16
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.

Options.Prefix drains the request body before the handler sees it

1 participant