fix(upload): compare media type only when enforcing pinned Content-Type - #29
Merged
Conversation
The signed direct-upload handler (PUT /uploads/{token}) enforced the grant's
pinned Content-Type with an exact string match, so a client that adds a standard
parameter — e.g. 'application/pdf; charset=utf-8' against a grant for
'application/pdf' — was wrongly rejected with 'content type mismatch'.
Compare only the media type (type/subtype) via mime.ParseMediaType on both sides,
per RFC 7231 §3.1.1.1. This is not a weakening: the body is streamed to storage
without sniffing, so the pin was always a declared-media-type check — ignoring
parameters that don't change the media type preserves the constraint. A real
mismatch (text/plain vs application/pdf) is still rejected, and a malformed/empty
client Content-Type parses to "" and is rejected (fail-closed). ParseMediaType
also lowercases, so the match is correctly case-insensitive.
Add TestUploadDirect_ContentTypeAllowsParams (charset param accepted); the
existing TestUploadDirect_ContentTypeMismatch (text/plain rejected) still passes.
go build ./... ok; gofmt/go vet clean; go test -run TestUploadDirect_ContentType = 2 pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The signed direct-upload handler (
PUT /uploads/{token}) enforced the grant's pinnedContent-Typewith an exact string match:So a client that adds a standard parameter — e.g.
application/pdf; charset=utf-8against a grant forapplication/pdf— was wrongly rejected, even though it's the same media type.Fix: compare only the media type (type/subtype) via
mime.ParseMediaTypeon both sides, per RFC 7231 §3.1.1.1.Why it's safe (not a constraint bypass)
text/plainvsapplication/pdf→got != want→ fail (existingTestUploadDirect_ContentTypeMismatchstill passes).Content-Typeparses to""and is rejected when the grant pins a type.ParseMediaTypelowercases, so the comparison is correctly case-insensitive (media types are case-insensitive per RFC) — not a regression.g.ContentType != ""(unpinned grants accept anything, unchanged).Tests
TestUploadDirect_ContentTypeAllowsParams: grantapplication/pdf, PUTapplication/pdf; charset=utf-8→200 ok. Genuine regression guard (fails on the old exact-match). The existing mismatch test is retained.Verification
go build ./...ok;gofmt/go vetclean.go test -run TestUploadDirect_ContentType .— 2 pass (mismatch + new allows-params); dropbox tests run on the in-memory bucket.