1 /**
2 * Regression tests for #14120: binary file content (e.g. an image produced via
3 * `buffer.toString("binary")`, as the upload docs instruct) must not be
4 * corrupted by a UTF-8 decode, while UTF-8 text content (CSV special characters
5 * from #13649) and base64 content must still be handled correctly. Exercises the
6 * real service with a mocked S3 client `send`.
7 */
8
9 /* eslint-disable @typescript-eslint/no-var-requires */
10 var mockS3Send: jest.Mock
11
12 jest.mock("@aws-sdk/client-s3", () => {
13 const actual = jest.requireActual(
14 "@aws-sdk/client-s3"
15 ) as typeof import("@aws-sdk/client-s3")
16 mockS3Send = jest.fn().mockResolvedValue({})
17 return {
18 ...actual,
19 S3Client: jest.fn().mockImplementation(() => ({
20 send: (...args: unknown[]) => mockS3Send(...args),
21 })),
22 }
23 })
24
25 jest.mock("@aws-sdk/s3-request-presigner", () => ({
26 getSignedUrl: jest
27 .fn()
28 .mockResolvedValue("https://bucket.s3.amazonaws.com/signed"),
29 }))
30
31 jest.mock("@aws-sdk/lib-storage", () => ({
32 Upload: jest.fn().mockImplementation(() => ({
33 done: jest.fn().mockResolvedValue(undefined),
34 })),
35 }))
36
37 import { PutObjectCommand } from "@aws-sdk/client-s3"
38 import { S3FileService } from "../s3-file"
39
40 describe("S3FileService content decoding (#14120)", () => {
41 const logger = {
42 error: jest.fn(),
43 info: jest.fn(),
44 warn: jest.fn(),
45 debug: jest.fn(),
46 }
47
48 const baseOptions = {
49 file_url: "https://mybucket.s3.amazonaws.com",
50 region: "us-east-1",
51 bucket: "test-bucket",
52 access_key_id: "test-key",
53 secret_access_key: "test-secret",
54 }
55
56 const uploadedBody = async (file: any): Promise<Buffer> => {
57 const service = new S3FileService({ logger } as any, baseOptions)
58 await service.upload(file)
59 const command = mockS3Send.mock.calls[0][0] as InstanceType<
60 typeof PutObjectCommand
61 >
62 return command.input.Body as Buffer
63 }
64
65 beforeEach(() => {
66 mockS3Send.mockClear()
67 mockS3Send.mockResolvedValue({})
68 })
69
70 it("preserves binary image bytes passed via buffer.toString('binary') (no UTF-8 corruption)", async () => {
71 const png = Buffer.from([
72 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0xff, 0x80,
73 ])
74
75 const body = await uploadedBody({
76 filename: "image.png",
77 mimeType: "image/png",
78 content: png.toString("binary"),
79 access: "public",
80 })
81
82 // Before the fix the leading 0x89 was re-encoded to the UTF-8 sequence
83 // 0xC2 0x89, corrupting the file.
84 expect(Buffer.compare(body, png)).toBe(0)
85 expect([body[0], body[1], body[2], body[3]]).toEqual([
86 0x89, 0x50, 0x4e, 0x47,
87 ])
88 })
89
90 it("decodes base64-encoded content back to the original bytes", async () => {
91 const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x00, 0xfe])
92
93 const body = await uploadedBody({
94 filename: "image.png",
95 mimeType: "image/png",
96 content: png.toString("base64"),
97 access: "public",
98 })
99
100 expect(Buffer.compare(body, png)).toBe(0)
101 })
102
103 it("preserves UTF-8 special characters in text/CSV content (guards #13649)", async () => {
104 const csv = "name,city\nJoão,São Paulo\n"
105
106 const body = await uploadedBody({
107 filename: "data.csv",
108 mimeType: "text/csv",
109 content: csv,
110 access: "public",
111 })
112
113 expect(Buffer.compare(body, Buffer.from(csv, "utf8"))).toBe(0)
114 })
115 })