Skip to content

fix(receiving): forward raw email bytes without re-decoding them as UTF-8 - #1117

Merged
dielduarte merged 2 commits into
resend:canaryfrom
RaphaelFakhri:fix/forward-raw-email-bytes
Oct 6, 2026
Merged

dielduarte merged 2 commits into
resend:canaryfrom
RaphaelFakhri:fix/forward-raw-email-bytes

Conversation

@RaphaelFakhri

@RaphaelFakhri RaphaelFakhri commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

emails.receiving.forward() downloads the raw message with response.text(). That call decodes the bytes as UTF-8, so any byte that is not valid UTF-8 becomes U+FFFD before the message is used.

  • In wrapped mode (passthrough: false), the .eml attachment is built from the decoded string. The forwarded attachment no longer matches the original message.
  • In passthrough mode, postal-mime receives a string, so it cannot apply the charset declared in the message. A body such as text/plain; charset="ISO-8859-1" with an 8-bit 0xE9 byte forwards as caf� instead of café.

This change reads the download with arrayBuffer() in both paths. postal-mime decodes the bytes with the declared charset, and the wrapped attachment is the exact original bytes encoded as base64.

Testing

  • Two new tests in src/emails/receiving/receiving.spec.ts forward an ISO-8859-1 message in each mode.
  • With the source change reverted, both fail. With the change, the 24 tests in the file pass.
  • The full suite (vitest run --exclude e2e), biome check and tsc --noEmit pass.

Summary by cubic

Fixes emails.receiving.forward() so forwarded emails keep their original raw bytes instead of corrupting non-UTF-8 content.

response.text() decoded the downloaded message as UTF-8, so bytes that weren't valid UTF-8 became U+FFFD. In wrapped mode the .eml attachment no longer matched the original message; in passthrough mode postal-mime couldn't apply the declared charset, so an ISO-8859-1 body forwarded as caf� instead of café.

The download is now read with arrayBuffer(). postal-mime decodes the exact bytes using the declared charset, and the wrapped attachment is the original bytes as base64. Adds tests for an ISO-8859-1 message in both modes and bumps the package version to 6.32.1.

Written for commit 58e2bdb. Summary will update on new commits.

Review in cubic

@RaphaelFakhri
RaphaelFakhri requested a review from a team as a code owner September 29, 2026 14:06
@github-actions github-actions Bot added the linear-synced PR has been synced to Linear label Sep 30, 2026
Comment on lines +1153 to +1167
fetchMock.mockOnce(JSON.stringify(getEmailResponse), {
status: 200,
headers: { 'content-type': 'application/json' },
});
fetchMock.mockOnce(
async () =>
new Response(latin1Message, {
status: 200,
headers: { 'content-type': 'message/rfc822' },
}) as unknown as string,
);
fetchMock.mockOnce(JSON.stringify({ id: 'new-email-id' }), {
status: 200,
headers: { 'content-type': 'application/json' },
});

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.

looks like we are repeating these mocks on each tests, can you please move to a beforeEach block?

Comment thread src/emails/receiving/receiving.spec.ts Outdated
new Response(latin1Message, {
status: 200,
headers: { 'content-type': 'message/rfc822' },
}) as unknown as string,

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.

The as unknown as string casts aren't needed. vitest-fetch-mock 0.4.5 types its mock responses as MockResponse | ResponseBody | Response, so a real Response is already allowed

@RaphaelFakhri
RaphaelFakhri force-pushed the fix/forward-raw-email-bytes branch from de6e31f to 9004bcd Compare October 3, 2026 16:21
@RaphaelFakhri

Copy link
Copy Markdown
Contributor Author

Thanks. I rebased on canary (the fix now lives in downloadRaw), moved the mocks into a beforeEach and dropped the casts.

@pkg-pr-new

pkg-pr-new Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/resend@1117

commit: 58e2bdb

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@dielduarte
dielduarte merged commit a56aa49 into resend:canary Oct 6, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linear-synced PR has been synced to Linear

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants