Skip to content

Commit

Permalink
Special case set-cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed Feb 16, 2024
1 parent 466e7a0 commit 2f7a0e0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
5 changes: 3 additions & 2 deletions fixtures/worker-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
"type:tests": "tsc -p ./tests/tsconfig.json"
},
"devDependencies": {
"wrangler": "workspace:*",
"@cloudflare/workers-tsconfig": "workspace:^",
"undici": "^5.28.2"
"undici": "^5.28.2",
"wrangler": "workspace:*"
},
"dependencies": {
"cookie": "^0.6.0",
"isomorphic-random-example": "workspace:^"
}
}
28 changes: 27 additions & 1 deletion fixtures/worker-app/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import cookie from "cookie";
import { randomBytes } from "isomorphic-random-example";
import { now } from "./dep";
import { logErrors } from "./log";
Expand All @@ -15,10 +16,35 @@ export default {
async fetch(request) {
console.log("request log");

const { pathname, origin } = new URL(request.url);
const { pathname, origin, hostname, host } = new URL(request.url);
if (pathname === "/random") return new Response(hexEncode(randomBytes(8)));
if (pathname === "/error") throw new Error("Oops!");
if (pathname === "/redirect") return Response.redirect(`${origin}/foo`);
if (pathname === "/cookie")
return new Response("", {
headers: [
[
"Set-Cookie",
cookie.serialize("hello", "world", {
domain: hostname,
}),
],
[
"Set-Cookie",
cookie.serialize("hello2", "world2", {
domain: host,
secure: true,
}),
],
[
"Set-Cookie",
cookie.serialize("hello2", "world2", {
domain: `${hostname}:1`,
}),
],
],
});

if (request.headers.get("X-Test-URL") !== null) {
return new Response(request.url);
}
Expand Down
13 changes: 13 additions & 0 deletions fixtures/worker-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,17 @@ describe("'wrangler dev' correctly renders pages", () => {
`http://${ip}:${port}/foo`
);
});

it("rewrites set-cookie headers to the hostname, not host", async ({
expect,
}) => {
const response = await fetch(`http://${ip}:${port}/cookie`);
expect(response.headers.getSetCookie()).toMatchInlineSnapshot(`
[
"hello=world; Domain=prod.example.org",
"hello2=world2; Domain=prod.example.org; Secure",
"hello2=world2; Domain=prod.example.org:1",
]
`);
});
});
17 changes: 13 additions & 4 deletions packages/wrangler/templates/startDevWorker/ProxyWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,22 @@ function insertLiveReloadScript(
* so that this proxy is transparent to the Client Browser and User Worker.
*/
function rewriteUrlRelatedHeaders(headers: Headers, from: URL, to: URL) {
const setCookie = headers.getAll("Set-Cookie");
headers.delete("Set-Cookie");
for (const cookie of setCookie) {
headers.append(
"Set-Cookie",
cookie.replace(
new RegExp(`Domain=${from.hostname}($|;|,)`),
`Domain=${to.hostname}$1`
)
);
}
headers.forEach((value, key) => {
if (typeof value === "string" && value.includes(from.hostname)) {
if (typeof value === "string" && value.includes(from.host)) {
headers.set(
key,
value
.replaceAll(from.origin, to.origin)
.replaceAll(from.hostname, to.hostname)
value.replaceAll(from.origin, to.origin).replaceAll(from.host, to.host)
);
}
});
Expand Down
4 changes: 3 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2f7a0e0

Please sign in to comment.