Skip to content

Commit c000f40

Browse files
committed
fix: use request.url instead of req.headers.host in revalidation patch
1 parent da4b7fc commit c000f40

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
fix: use request.url instead of req.headers.host in revalidation patch
6+
7+
The `res.revalidate()` patch now derives the host and protocol from `request.url`
8+
(set by the Cloudflare Workers runtime) instead of `req.headers.host` (user-controllable
9+
HTTP header). This prevents host header injection in the `WORKER_SELF_REFERENCE.fetch()`
10+
call used for Pages Router revalidation.

packages/cloudflare/src/cli/build/patches/plugins/res-revalidate.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe("patchResRevalidate", () => {
9595
- method: 'HEAD',
9696
- headers: revalidateHeaders
9797
- });
98-
+ const res = await (await import("@opennextjs/cloudflare")).getCloudflareContext().env.WORKER_SELF_REFERENCE.fetch(\`\${req.headers.host.includes("localhost") ? "http":"https" }://\${req.headers.host}\${urlPath}\`,{method:'HEAD', headers:revalidateHeaders});
98+
+ const res = await (await import("@opennextjs/cloudflare")).getCloudflareContext().env.WORKER_SELF_REFERENCE.fetch(\`\${new URL(req.url).protocol}//\${new URL(req.url).host}\${urlPath}\`,{method:'HEAD', headers:revalidateHeaders});
9999
// we use the cache header to determine successful revalidate as
100100
// a non-200 status code can be returned from a successful revalidate
101101
// e.g. notFound: true returns 404 status code but is successful

packages/cloudflare/src/cli/build/patches/plugins/res-revalidate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ rule:
5353
has:
5454
kind: identifier
5555
56-
fix: await (await import("@opennextjs/cloudflare")).getCloudflareContext().env.WORKER_SELF_REFERENCE.fetch(\`\${$REQ.headers.host.includes("localhost") ? "http":"https" }://\${$REQ.headers.host}$URL_PATH\`,{method:'HEAD', headers:$HEADERS})
56+
fix: await (await import("@opennextjs/cloudflare")).getCloudflareContext().env.WORKER_SELF_REFERENCE.fetch(\`\${new URL($REQ.url).protocol}//\${new URL($REQ.url).host}$URL_PATH\`,{method:'HEAD', headers:$HEADERS})
5757
`;
5858

5959
export const patchResRevalidate: CodePatcher = {

0 commit comments

Comments
 (0)