Skip to content

Commit 19b1881

Browse files
Return 403 for /_image with href redirect using double slash (#10500)
Co-authored-by: James Opstad <[email protected]>
1 parent dc81221 commit 19b1881

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

.changeset/weak-trams-add.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/workers-shared": minor
3+
---
4+
5+
Block /\_image routes with href query param using double slash.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type Data = {
5353
coloRegion?: string;
5454
// blob6 - URL for analysis
5555
abuseMitigationURLHost?: string;
56+
// blob7 - XSS detection href parameter value
57+
xssDetectionImageHref?: string;
5658
};
5759

5860
export class Analytics {
@@ -107,6 +109,7 @@ export class Analytics {
107109
this.data.version, // blob4
108110
this.data.coloRegion, // blob5
109111
this.data.abuseMitigationURLHost, // blob6
112+
this.data.xssDetectionImageHref, // blob7
110113
],
111114
});
112115
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,28 @@ export default {
128128
}
129129
}
130130

131+
if (url.pathname === "/_image") {
132+
const hrefParam = url.searchParams.get("href");
133+
if (
134+
hrefParam &&
135+
hrefParam.length > 2 &&
136+
hrefParam.startsWith("//")
137+
) {
138+
try {
139+
const hrefUrl = new URL("https:" + hrefParam);
140+
const isImageFetchDest =
141+
request.headers.get("sec-fetch-dest") == "image";
142+
143+
if (hrefUrl.hostname !== url.hostname && !isImageFetchDest) {
144+
analytics.setData({ xssDetectionImageHref: hrefParam });
145+
return new Response("Blocked", { status: 403 });
146+
}
147+
} catch {
148+
console.log(`Invalid href parameter in /_image: ${hrefParam}`);
149+
}
150+
}
151+
}
152+
131153
analytics.setData({
132154
timeToDispatch: performance.now() - startTimeMs,
133155
});

0 commit comments

Comments
 (0)