-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Blob/File type: preserve user-supplied MIME verbatim (WHATWG File API) #29258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
11
commits into
main
Choose a base branch
from
farm/c4850fc3/fix-blob-type-charset-append
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
27153ec
Blob/File type: preserve user-supplied MIME verbatim (WHATWG File API)
robobun fc3cf56
Blob: also fix S3File type canonicalization and slice flag
robobun a75bcd4
Blob: expand dupeWithContentType dead-code note to cover both branches
robobun 3b67da7
Blob: use slice argument (not final blob) for content_type_was_set
robobun 54ba384
ci: retrigger after infrastructure hiccup
robobun a28293f
test: fix S3Ref inspect assertion for preserved text/plain type
robobun 867e66b
Blob: note mimeType→mimeTypeInternedValue gotcha in dupeWithContentType
robobun 0afaee7
Blob: reset content_type_allocated before reassigning from interned
robobun c1d967c
test: skip elysia path.test.ts — asserts pre-WHATWG File.type canonic…
robobun cfbc969
test: fix elysia skipTests path key (relative to testParentPath)
robobun c7d8d0d
ci: normalize separators in vendor skipTests regex for Windows
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { expect, test } from "bun:test"; | ||
|
|
||
| // https://github.com/oven-sh/bun/issues/29257 | ||
| // | ||
| // Bun was rewriting `text/plain` (and `text/css`, `text/html`, | ||
| // `application/json`, ...) to their charset-appended canonical forms | ||
| // (`text/plain;charset=utf-8`, etc.) when the user set the `type` on a | ||
| // Blob/File at construction time. | ||
| // | ||
| // Per the WHATWG File API (https://w3c.github.io/FileAPI/#blob), user | ||
| // agents must NOT append a charset parameter to the media type. | ||
|
|
||
| test("new File(..., { type: 'text/plain' }).type is preserved verbatim", () => { | ||
| const file = new File([], "empty.txt", { type: "text/plain" }); | ||
| expect(file.type).toBe("text/plain"); | ||
| }); | ||
|
|
||
| test("new Blob([], { type: 'text/plain' }).type is preserved verbatim", () => { | ||
| const blob = new Blob([], { type: "text/plain" }); | ||
| expect(blob.type).toBe("text/plain"); | ||
| }); | ||
|
|
||
| test("File/Blob type is preserved for other types Bun used to canonicalize", () => { | ||
| // These are the types Compact.toMimeType() substitutes into | ||
| // charset-appended forms for HTTP responses. None of them should leak | ||
| // the substitution into the File/Blob `type` property. | ||
| const types = [ | ||
| "text/plain", | ||
| "text/css", | ||
| "text/html", | ||
| "text/javascript", | ||
| "application/json", | ||
| "application/javascript", | ||
| ]; | ||
| for (const type of types) { | ||
| expect(new File([], "x", { type }).type).toBe(type); | ||
| expect(new Blob([], { type }).type).toBe(type); | ||
| } | ||
| }); | ||
|
|
||
| test("File/Blob type with explicit charset is preserved verbatim", () => { | ||
| // A user who explicitly passes a charset parameter should get it back | ||
| // unchanged — not silently swapped for a different canonical form. | ||
| const file = new File([], "x.txt", { type: "text/plain;charset=utf-8" }); | ||
| expect(file.type).toBe("text/plain;charset=utf-8"); | ||
|
|
||
| const blob = new Blob([], { type: "text/plain;charset=utf-8" }); | ||
| expect(blob.type).toBe("text/plain;charset=utf-8"); | ||
| }); | ||
|
|
||
| test("File/Blob type is lowercased (per WHATWG spec)", () => { | ||
| // The spec requires lowercasing but not charset canonicalization. | ||
| expect(new File([], "x", { type: "TEXT/PLAIN" }).type).toBe("text/plain"); | ||
| expect(new Blob([], { type: "Text/Plain" }).type).toBe("text/plain"); | ||
| }); | ||
|
|
||
| test("uncommon MIME types still round-trip unchanged", () => { | ||
| // Types not in the interning table take the copyLowercase path. They | ||
| // should also round-trip verbatim (lowercased) — check both the File | ||
| // and Blob constructor paths since they share logic but are separate | ||
| // call sites in src/bun.js/webcore/Blob.zig. | ||
| const file = new File([], "x", { type: "application/x-custom-type" }); | ||
| expect(file.type).toBe("application/x-custom-type"); | ||
| const blob = new Blob([], { type: "application/x-custom-type" }); | ||
| expect(blob.type).toBe("application/x-custom-type"); | ||
| }); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| test("Bun.file(path, { type: 'text/plain' }).type is preserved verbatim", () => { | ||
| // Covers the `constructBunFile` path in Blob.zig. | ||
| const file = Bun.file(import.meta.path, { type: "text/plain" }); | ||
| expect(file.type).toBe("text/plain"); | ||
| }); | ||
|
|
||
| test("Bun.s3.file(path, { type: 'text/plain' }).type is preserved verbatim", () => { | ||
| // Covers the S3File constructor paths in S3File.zig (same bug, different | ||
| // file). The object is never actually touched over the network — we only | ||
| // check that the `type` field is set from our argument verbatim. | ||
| const file = Bun.s3.file("test.txt", { type: "text/plain" }); | ||
| expect(file.type).toBe("text/plain"); | ||
| }); | ||
robobun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.