Skip to content

fix(bind): match request media type case-insensitively in BindBody - #3213

Open
yxshee wants to merge 1 commit into
labstack:masterfrom
yxshee:fix/bind-media-type-case
Open

yxshee wants to merge 1 commit into
labstack:masterfrom
yxshee:fix/bind-media-type-case

Conversation

@yxshee

@yxshee yxshee commented Nov 8, 2026 •

Copy link
Copy Markdown

BindBody compares the request media type against the lowercase MIME constants as-is, so a client that sends a supported type in a different case gets a 415. Media type and subtype names are case-insensitive (RFC 9110 section 8.3.1).

e.POST("/", func(c *echo.Context) error {
	var v struct{ ID int `json:"id"` }
	return c.Bind(&v)
})
// POST / with Content-Type: Application/JSON and body {"id": 1}
// got: 415 Unsupported Media Type, expected: bound with ID=1

The same happens for Application/XML, Text/XML, Application/X-WWW-Form-Urlencoded and Multipart/Form-Data.

The fix lowercases the media type before the switch. Parameters are left untouched, and the form and multipart branches already go through net/http, which parses the type with mime.ParseMediaType. #3095 had the same change and was labelled bug/v5 before its author closed it.

TestDefaultBinder_BindBody_MediaTypeIsCaseInsensitive covers all five types. On master every case fails with code=415, message=Unsupported Media Type. With the fix, go test -race ./..., go vet, staticcheck and golint all pass.

Summary by CodeRabbit

  • Bug Fixes
    • Body binding now recognizes content types regardless of capitalization, including JSON, XML, and form submissions.

@yxshee
yxshee force-pushed the fix/bind-media-type-case branch from e4f7d7b to b56520d Compare November 28, 2026 07:22
@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Body media type handling

Layer / File(s) Summary
Media type normalization and coverage
bind.go, bind_test.go
bindBodyMediaType lowercases the trimmed base media type. Tests check mixed-case content types across JSON, XML, URL-encoded form, and multipart form.

Suggested reviewers: vishr

Change: Bug fix

Merge Risk: Minimal · up to b5652

BindBody now accepts request media types regardless of letter case, as RFC 9110 specifies, and the new tests cover the supported body formats. No concrete risk was found, so this is ready to merge.

Security architecture risk: Minimal · up to b5652

Requests using mixed-case media types now reach the same decoders as their lowercase equivalents. The change does not add a format or an endpoint, and the review found no material weakening of access controls or request isolation.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed acceptance behavior applies wherever a handler invokes BindBody directly or uses the default binder. The same parser families and caller-supplied destinations were already reachable with lowercase labels.

Trust Boundaries and Controls

  • observed — The client-supplied Content-Type selects a decoder. The changed code normalizes that selection value; it does not change request identity or the authorization checks a caller applies around binding.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e231783a-5f75-4913-a130-9e49c5110c63
Commits

Reviewing files that changed from the base of the PR and between 59aacdd and b56520d.

Files selected for processing: 2

  • bind.go
  • bind_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

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