Skip to content

middleware: parse quoted ContentCharset media parameters - #1218

Merged
VojtechVitek merged 3 commits into
masterfrom
charset-media-parameters-simplified
Oct 13, 2026
Merged

VojtechVitek merged 3 commits into
masterfrom
charset-media-parameters-simplified

Conversation

@VojtechVitek

Copy link
Copy Markdown
Contributor

Summary

ContentCharset rejects valid quoted charset values such as Content-Type: text/plain; charset="UTF-8" with a 415. It also reads charset= from inside an earlier quoted parameter. This PR switches the lookup to mime.ParseMediaType and keeps the old lenient parsing as a fallback for headers that are not valid media types.

Attribution

The first commit is the work from go-chi/chi#1205 by @tianrking, applied unchanged. It keeps the original author, commit message, and validation. Credit for the fix belongs to them.

The second commit simplifies that change after review of #1205.

What the second commit changes

  • Removes normalizeHTTPQuotedPairs (about 25 lines). It only handles backslash-escaped non-special characters such as charset="UTF\-8", which no real client sends.
  • Removes TestContentCharsetMediaParametersOverHTTP. It tests chunked framing, not charset parsing, and the in-process table test covers the logic.
  • Drops the quoted pair row from the table test.

Notes

  • No API change. mime.ParseMediaType adds one map allocation per request.
  • Also accepts charset = utf-8 and charset*=UTF-8''utf-8.
  • #1139 tried the same bug with a smaller strings.Trim fix. It was closed without merging. This change also handles the quoted ; cases that fix(middleware): accept quoted charset in Content-Type #1139 does not.

Testing

  • go vet ./middleware
  • go test -race ./middleware

🤖 This message was generated by Claude on behalf of Vojtech.

tianrking and others added 3 commits October 5, 2026 05:18
Drop the hand-written quoted-pair normalizer and the httptest-based
test from the original change. mime.ParseMediaType handles quoted
charset values on its own, and the table test covers the remaining
cases in-process.

Co-Authored-By: tianrking <10758833+tianrking@users.noreply.github.com>
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Vojtech Vitek <vojtech.vitek@golang.cz>
Rename the backslash row to describe what it tests, and add rows for
xcharset (not a charset parameter) and a malformed parameter that
falls back to the legacy parser. Document quoted charset values on
ContentCharset.

Co-Authored-By: tianrking <10758833+tianrking@users.noreply.github.com>
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Vojtech Vitek <vojtech.vitek@golang.cz>

@VojtechVitek VojtechVitek left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

LGTM

@VojtechVitek
VojtechVitek merged commit 6075570 into master Oct 13, 2026
16 checks passed
@VojtechVitek
VojtechVitek deleted the charset-media-parameters-simplified branch October 13, 2026 23:23
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.

2 participants