Skip to content

Commit 442a5e0

Browse files
committed
fix(files): bound decoded text for encoded responses
1 parent 7323249 commit 442a5e0

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

‎apps/sim/lib/uploads/client/text-content.test.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,20 @@ describe('bounded file text decoding', () => {
7171
}
7272
)
7373

74-
it('accepts the exact limit and empty responses', async () => {
75-
await expect(
76-
readFileText(new Response('a'.repeat(MAX_TEXT_PREVIEW_BYTES)))
77-
).resolves.toHaveLength(MAX_TEXT_PREVIEW_BYTES)
74+
it.each([undefined, 'gzip'])(
75+
'accepts the exact decoded limit with encoding %s',
76+
async (encoding) => {
77+
const headers = new Headers({
78+
'content-length': String(MAX_TEXT_PREVIEW_BYTES + (encoding ? 100 : 0)),
79+
})
80+
if (encoding) headers.set('content-encoding', encoding)
81+
await expect(
82+
readFileText(new Response('a'.repeat(MAX_TEXT_PREVIEW_BYTES), { headers }))
83+
).resolves.toHaveLength(MAX_TEXT_PREVIEW_BYTES)
84+
}
85+
)
86+
87+
it('accepts empty responses', async () => {
7888
await expect(readFileText(new Response(null))).resolves.toBe('')
7989
})
8090
})

‎apps/sim/lib/uploads/client/text-content.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ export class FileTextSizeError extends Error {
1313
}
1414
}
1515

16-
/** Bounds text previews even when storage omits or underreports Content-Length. */
16+
/** Bounds decoded text; Content-Length measures wire bytes for encoded responses. */
1717
export async function readFileText(response: Response): Promise<string> {
18-
if (Number(response.headers.get('content-length')) > MAX_TEXT_PREVIEW_BYTES) {
18+
if (
19+
!response.headers.has('content-encoding') &&
20+
Number(response.headers.get('content-length')) > MAX_TEXT_PREVIEW_BYTES
21+
) {
1922
await response.body?.cancel().catch(() => {})
2023
throw new FileTextSizeError()
2124
}

0 commit comments

Comments
 (0)