Skip to content

fix(http): a form body is read when its media type is not in lower case - #422

Merged
rcardin merged 2 commits into
Eezo-framework:devfrom
calpeyser:fix-form-media-type-case
Nov 27, 2026
Merged

rcardin merged 2 commits into
Eezo-framework:devfrom
calpeyser:fix-form-media-type-case

Conversation

@calpeyser

Copy link
Copy Markdown
Contributor

A media type is case insensitive under RFC 9110, but eezo checks the form content type with a case sensitive prefix. So a client that sends the type in mixed case gets an empty form:

POST /widgets HTTP/1.1
Content-Type: Application/X-WWW-Form-Urlencoded

name=Lamp&csrf=...

The handler sees no fields, the _method override does not run, and the CSRF check finds no token. Browsers send lower case, so this hits only other clients.

The check now takes the media type before any parameters and compares it without regard to case. A lower case type, with or without ; charset=UTF-8, reads as before. The new test fails before the fix and passes after it.

Prepared with the help of AI

RFC 9110 says a media type is case insensitive, but the form check
compared the Content-Type header with a case sensitive prefix. A body
sent as Application/X-WWW-Form-Urlencoded read as an empty form, so the
handler saw no fields, the _method override did not run and CSRF found
no token. The check now takes the media type before any parameters and
compares it without regard to case.
@calpeyser

Copy link
Copy Markdown
Contributor Author

@rcardin When you have a moment, could you take a look at this one? It is a small fix, CI passes and it merges cleanly.

@rcardin

rcardin commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Sorry, @calpeyser. I'm reviewing it right now.

@rcardin rcardin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The fix is correct and the gate is green at 85d83fd. Two behaviours have no test. Each inline comment gives one test to add.

private[http] def isFormEncoded: Boolean =
header("Content-Type").exists(_.startsWith("application/x-www-form-urlencoded"))
header("Content-Type").exists(
_.takeWhile(_ != ';').trim.equalsIgnoreCase("application/x-www-form-urlencoded")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No test exercises .trim. I removed it, and RequestSuite, FormSuite, MethodOverrideSuite and CsrfSuite stayed green (83 of 83).

RFC 9110 permits optional whitespace before ;, and that is the construct .trim reads. CODING_STANDARDS.md asks for one test for each construct of a format the code reads.

Please add this value to the Seq in the new RequestSuite test:

"application/x-www-form-urlencoded ; charset=UTF-8"

* The media type is compared without its parameters and without regard to case, as RFC 9110
* says, so `Application/X-WWW-Form-Urlencoded; charset=UTF-8` is a form too.
*/
private[http] def isFormEncoded: Boolean =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The PR body says that the _method override now runs for a mixed case form type. No suite shows it: the mixed case values are only in RequestSuite, and that test reads request.form only.

I put the old case sensitive startsWith check back in withMethodOverride only, and MethodOverrideSuite, CsrfSuite and RequestSuite stayed green (43 of 43). Thus form and the override can disagree again and no test fails. The scaladoc here says that they have to agree.

Please add a test to MethodOverrideSuite, for example "a mixed case form type is form encoded for the override too". A POST with Content-Type: Application/X-WWW-Form-Urlencoded and _method=PUT in the body becomes a PUT. Do the same check with _method=PUT in the query string.

@rcardin rcardin added bug Something isn't working http labels Nov 25, 2026
@rcardin rcardin added this to the 0.5.0 milestone Nov 25, 2026
Two cases the review asked for. RequestSuite reads a form type with
optional whitespace before ';', which is what the trim handles.
MethodOverrideSuite shows that a mixed case form type also gets the
_method override, from the body and from the query string.
@calpeyser

Copy link
Copy Markdown
Contributor Author

Both tests are in one new commit, 9d79659. RequestSuite now reads the type with whitespace before ;, and MethodOverrideSuite checks a mixed case type from the body and from the query string. Each fails without its line: with no .trim, and with the old startsWith back in withMethodOverride.

@rcardin
rcardin merged commit f389f6f into Eezo-framework:dev Nov 27, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working http

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants