Skip to content

fix: reuse cached form in consumeForm (multipart + antiforgery)#154

Open
michaelglass wants to merge 1 commit intofalcoframework:masterfrom
michaelglass:fix-multipart-form-after-antiforgery
Open

fix: reuse cached form in consumeForm (multipart + antiforgery)#154
michaelglass wants to merge 1 commit intofalcoframework:masterfrom
michaelglass:fix-multipart-form-after-antiforgery

Conversation

@michaelglass
Copy link
Copy Markdown
Contributor

When antiforgery validation runs before Request.getForm (the flow getFormSecure uses internally), IAntiforgery.ValidateRequestAsync reads and buffers the form body via ReadFormAsync. For multipart/form-data, consumeForm would then take the StreamFormAsync branch and re-read the now-drained stream, failing with IOException: Unexpected end of Stream. Non-multipart was fine because ReadFormAsync returns the cached HttpRequest.Form.

Fix: detect the already-parsed form via IFormFeature.Form and reuse it before deciding between streaming and non-streaming read.

Regression test: AntiforgeryMultipartTests.POST multipart/form-data with valid CSRF token succeeds via getForm — asserted to fail on upstream/master with the IOException, passes with the fix. A second guard test covers the urlencoded path so it can't regress in the other direction.

…-read bug

When antiforgery validation runs before Request.getForm (e.g. via
getFormOptions / mapForm / validateCsrfToken), IAntiforgery.ValidateRequestAsync
reads and buffers the form body via ReadFormAsync. For multipart/form-data
POSTs, consumeForm would then branch into StreamFormAsync, which re-reads
the (now-drained) body and fails with 'Unexpected end of Stream'.

Detect that the form has already been parsed by checking the request's
IFormFeature and reuse the cached ctx.Request.Form in that case, instead
of attempting to re-read the stream.
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