Skip to content

Commit 2765b88

Browse files
authored
allow plain text with charset (#9935)
1 parent 6c4a652 commit 2765b88

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

.changeset/plain-baths-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/workers-shared": patch
3+
---
4+
5+
allow plain text with charset

packages/workers-shared/router-worker/src/worker.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,18 @@ export default {
136136

137137
if (shouldBlockNonImageResponse) {
138138
const resp = await env.USER_WORKER.fetch(maybeSecondRequest);
139-
const isImage = resp.headers
140-
.get("content-type")
141-
?.startsWith("image/");
142-
const isPlainText =
143-
resp.headers.get("content-type") === "text/plain";
144-
if (!isImage && !isPlainText && resp.status !== 304) {
139+
140+
const contentType = resp.headers.get("content-type") || "";
141+
142+
// Allow:
143+
// - images
144+
// - text/plain - used by Next errors
145+
const isImageOrPlainText =
146+
contentType.startsWith("image/") ||
147+
// Matches "text/plain", "text/plain;charset=UTF-8"
148+
contentType.split(";")[0] === "text/plain";
149+
150+
if (!isImageOrPlainText && resp.status !== 304) {
145151
analytics.setData({ abuseMitigationBlocked: true });
146152
return new Response("Blocked", { status: 403 });
147153
}

packages/workers-shared/router-worker/tests/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,18 @@ describe("unit tests", async () => {
313313
expectedStatus: 200,
314314
expectedBody: "fake image data",
315315
},
316+
{
317+
description:
318+
"allows /_next/image requests with remote URLs that have content type text/plain with charset",
319+
url: `https://example.com${subpath}_next/image?url=https://example.com/image.jpg`,
320+
userWorkerResponse: {
321+
body: "fake image data",
322+
headers: { "content-type": "text/plain;charset=UTF-8" },
323+
status: 200,
324+
},
325+
expectedStatus: 200,
326+
expectedBody: "fake image data",
327+
},
316328
{
317329
description:
318330
"allows /_next/image with remote URL and image header regardless of response content",

0 commit comments

Comments
 (0)