Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bun.js/webcore/Blob.zig
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ pub fn fromDOMFormData(
var hex_buf: [70]u8 = undefined;
const boundary = brk: {
var random = globalThis.bunVM().rareData().nextUUID().bytes;
break :brk std.fmt.bufPrint(&hex_buf, "-WebkitFormBoundary{x}", .{&random}) catch unreachable;
break :brk std.fmt.bufPrint(&hex_buf, "----WebKitFormBoundary{x}", .{&random}) catch unreachable;
};

var context = FormDataContext{
Expand Down
41 changes: 40 additions & 1 deletion test/js/bun/http/form-data-set-append.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
import { expect, test } from "bun:test";

// https://github.com/oven-sh/bun/issues/12325
// https://github.com/oven-sh/bun/issues/29630
// FormData must produce a boundary matching WebKit exactly:
// declared: "----WebKitFormBoundary{hex}" (4 leading dashes, capital K)
// body markers: "------WebKitFormBoundary{hex}" (6 dashes)
// Previously Bun emitted "-WebkitFormBoundary{hex}" (1 dash, lowercase k),
// which produced 3-dash body markers — a format no other mainstream client
// uses and which trips buggy downstream multipart parsers.
test("FormData request body uses WebKit-compatible boundary (#29630)", async () => {
const fd = new FormData();
fd.append("file", new Blob([new Uint8Array([1, 2, 3])], { type: "application/octet-stream" }), "page-17.pdf");
fd.append("purpose", "user_data");

const req = new Request("http://localhost/", { method: "POST", body: fd });

const contentType = req.headers.get("content-type");
// Must match WebKit exactly: 4 leading dashes, capital K, followed by 32 hex chars
expect(contentType).toMatch(/^multipart\/form-data; boundary=----WebKitFormBoundary[0-9a-f]{32}$/);

const boundary = contentType.slice("multipart/form-data; boundary=".length);

const body = await req.text();
// Body markers are "--" + boundary, so 6 dashes before WebKitFormBoundary.
expect(body.startsWith(`--${boundary}\r\n`)).toBe(true);
expect(body.startsWith("------WebKitFormBoundary")).toBe(true);
// Terminator is "--" + boundary + "--\r\n".
expect(body.endsWith(`\r\n--${boundary}--\r\n`)).toBe(true);
});

test("FormData via Response uses WebKit-compatible boundary (#29630)", async () => {
const fd = new FormData();
fd.append("field", "value");

const res = new Response(fd);
const contentType = res.headers.get("content-type");
expect(contentType).toMatch(/^multipart\/form-data; boundary=----WebKitFormBoundary[0-9a-f]{32}$/);

const body = await res.text();
expect(body.startsWith("------WebKitFormBoundary")).toBe(true);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

// https://github.com/oven-sh/bun/issues/12325
test("formdata set with File works as expected", async () => {
const expected = ["617580375", "text-notes1.txt"];

Expand Down
Loading