Repository navigation
fix(http): a form body is read when its media type is not in lower case - #422
Conversation
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.
|
@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. |
|
Sorry, @calpeyser. I'm reviewing it right now. |
| 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") |
There was a problem hiding this comment.
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 = |
There was a problem hiding this comment.
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.
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.
|
Both tests are in one new commit, 9d79659. RequestSuite now reads the type with whitespace before |
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:
The handler sees no fields, the
_methodoverride 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