-
Notifications
You must be signed in to change notification settings - Fork 769
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure that the Pages dev proxy server does not change the Host …
…header Previously, when configuring `wrangler pages dev` to use a proxy to a 3rd party dev server, the proxy would replace the Host header, resulting in problems at the dev server if it was checking for cross-site scripting attacks. Now the proxy server passes through the Host header unaltered making it invisible to the 3rd party dev server. Fixes #4799
- Loading branch information
1 parent
65da40a
commit 840ed2d
Showing
11 changed files
with
236 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
fix: ensure that the Pages dev proxy server does not change the Host header | ||
|
||
Previously, when configuring `wrangler pages dev` to use a proxy to a 3rd party dev server, | ||
the proxy would replace the Host header, resulting in problems at the dev server if it was | ||
checking for cross-site scripting attacks. | ||
|
||
Now the proxy server passes through the Host header unaltered making it invisible to the | ||
3rd party dev server. | ||
|
||
Fixes #4799 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "pages-proxy-app", | ||
"version": "0.1.2", | ||
"private": true, | ||
"sideEffects": false, | ||
"main": "server/index.js", | ||
"scripts": { | ||
"build": "esbuild --bundle --platform=node server/index.ts --outfile=dist/index.js", | ||
"check:type": "tsc", | ||
"dev": "npx wrangler pages dev --compatibility-date=2024-01-17 --port 8790 --proxy 8791 -- pnpm run server", | ||
"server": "node dist/index.js", | ||
"test": "vitest run", | ||
"test:watch": "vitest", | ||
"type:tests": "tsc -p ./tests/tsconfig.json" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-tsconfig": "workspace:*", | ||
"miniflare": "workspace:*", | ||
"undici": "^5.28.2", | ||
"wrangler": "workspace:*" | ||
}, | ||
"engines": { | ||
"node": ">=14" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { createServer } from "http"; | ||
|
||
const server = createServer(); | ||
|
||
server.on("request", (req, res) => { | ||
res.write("Host:" + req.headers.host); | ||
res.end(); | ||
}); | ||
|
||
server.listen(8791); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { fork } from "node:child_process"; | ||
import { resolve } from "node:path"; | ||
import { fetch } from "undici"; | ||
import { afterAll, beforeAll, describe, it } from "vitest"; | ||
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived"; | ||
import type { ChildProcess } from "node:child_process"; | ||
|
||
describe("pages-proxy-app", async () => { | ||
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined; | ||
let devServer: ChildProcess; | ||
|
||
beforeAll(async () => { | ||
devServer = fork(resolve(__dirname, "../dist/index.js"), { | ||
stdio: "ignore", | ||
}); | ||
|
||
({ ip, port, stop } = await runWranglerPagesDev( | ||
resolve(__dirname, ".."), | ||
undefined, | ||
["--port=0", "--inspector-port=0", "--proxy=8791"] | ||
)); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stop?.(); | ||
devServer.kill(); | ||
}); | ||
|
||
it("receives the correct Host header", async ({ expect }) => { | ||
const response = await fetch(`http://${ip}:${port}/`); | ||
const text = await response.text(); | ||
expect(text).toContain(`Host:localhost:${port}`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "@cloudflare/workers-tsconfig/tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node"] | ||
}, | ||
"include": ["**/*.ts", "../../../node-types.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"include": ["server"], | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"module": "CommonJS", | ||
"lib": ["ES2020"], | ||
"types": ["node"], | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"noEmit": true, | ||
"skipLibCheck": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"$schema": "http://turbo.build/schema.json", | ||
"extends": ["//"], | ||
"pipeline": { | ||
"build": { | ||
"outputs": ["dist/**"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineProject, mergeConfig } from "vitest/config"; | ||
import configShared from "../../vitest.shared"; | ||
|
||
export default mergeConfig( | ||
configShared, | ||
defineProject({ | ||
test: {}, | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.