Skip to content

fix(architect): base64-encode response bodies that are not valid UTF-8 - #15814

Merged
brophdawg11 merged 1 commit into
remix-run:mainfrom
maxymlyskov:fix/base64-encode-non-utf-8-response-bodies
Nov 23, 2026
Merged

brophdawg11 merged 1 commit into
remix-run:mainfrom
maxymlyskov:fix/base64-encode-non-utf-8-response-bodies

Conversation

@maxymlyskov

Copy link
Copy Markdown
Contributor

sendReactRouterResponse in @react-router/architect sets isBase64Encoded only when the content type is an exact, case-sensitive match in binaryTypes.ts. Every other body is read with await nodeResponse.text(), so bytes that aren't valid UTF-8 become U+FFFD before API Gateway sees them.

let contentType = nodeResponse.headers.get("Content-Type");
let isBase64Encoded = isBinaryType(contentType);
let body: string | undefined;
if (nodeResponse.body) {
if (isBase64Encoded) {
body = await readableStreamToString(nodeResponse.body, "base64");
} else {
body = await nodeResponse.text();
}

So a resource route returning video/mp4, image/x-icon or Image/JPEG (any type missing from the list, or cased differently) gets a corrupted body. The request side had the same problem, fixed in #15652.

For a type that isn't in the list, this decodes the bytes with new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }), and if that throws, sends them as base64 with isBase64Encoded: true (same check as lib/rsc/html-stream/server.ts). Listed types and valid UTF-8 bodies come out as before, with one behavior change: a leading BOM is now kept (text() stripped it). The changeset mentions it.

Tests sit next to "handles resource routes with binary data": an it.each over the 3 types above checking body.equals(image) like #15652, and one over plain UTF-8 text and a BOM-led CSV.

On main the 3 binary cases and the BOM case fail; the plain text one passes on both:

    ✓ handles resource routes with regular data (8 ms)
    ✓ handles resource routes with binary data (49 ms)
    ✕ handles resource routes with binary data sent as video/mp4 (33 ms)
    ✕ handles resource routes with binary data sent as image/x-icon (6 ms)
    ✕ handles resource routes with binary data sent as Image/JPEG (6 ms)
    ✓ handles resource routes with utf-8 text (3 ms)
    ✕ handles resource routes with a byte order mark (5 ms)
  ● sendReactRouterResponse › handles resource routes with binary data sent as video/mp4
    Expected: true
    Received: false
  ● sendReactRouterResponse › handles resource routes with binary data sent as image/x-icon
    Expected: true
    Received: false
  ● sendReactRouterResponse › handles resource routes with binary data sent as Image/JPEG
    Expected: true
    Received: false
  ● sendReactRouterResponse › handles resource routes with a byte order mark
    - Expected  - 1
    + Received  + 1
Tests:       4 failed, 22 skipped, 3 passed, 29 total

Through the built createRequestHandler, 554828.jpeg sent as video/mp4 comes back as 47,481 bytes on main and 26,669 on the branch. Haven't tried it on a real Lambda.

Architect tests 30 passed; typecheck, lint and changes:validate pass. Full pnpm test: the only failures are special-characters-test.tsx timeouts and a data-browser-router-test.tsx hydration test that fails on main too.

sendReactRouterResponse only base64-encoded bodies whose content type is
an exact match in binaryTypes and read every other body with text(). Bytes
that are not valid UTF-8, such as a video/mp4, image/x-icon or Image/JPEG
file from a resource route, were replaced with U+FFFD before API Gateway
saw them, and text() also dropped a leading byte order mark.

Bodies outside the list are now decoded as fatal UTF-8 with the BOM kept,
and sent as base64 with isBase64Encoded when they do not decode.

@brophdawg11 brophdawg11 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed the exact head. The Architect adapter preserves binary response bytes and UTF-8 byte-order marks. Regression coverage and the relevant checks passed.

Authored by an AI agent on behalf of @brophdawg11

@brophdawg11
brophdawg11 merged commit 26f186b into remix-run:main Nov 23, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants