Skip to content

Commit 1a5cc86

Browse files
authored
fix(wrangler): preserve request ports with --host flag (#13745)
1 parent 926bad5 commit 1a5cc86

4 files changed

Lines changed: 82 additions & 11 deletions

File tree

.changeset/tiny-ducks-look.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: preserve request ports in `Origin` and `Referer` headers when using `wrangler dev --host`

packages/wrangler/src/__tests__/api/startDevWorker/LocalRuntimeController.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import { fetch } from "undici";
1010
import { assert, describe, it } from "vitest";
1111
import WebSocket from "ws";
1212
import { createPostgresEchoHandler } from "../../../../e2e/helpers/postgres-echo-handler";
13-
import { LocalRuntimeController } from "../../../api/startDevWorker/LocalRuntimeController";
13+
import {
14+
getUserWorkerInnerUrlOverrides,
15+
LocalRuntimeController,
16+
} from "../../../api/startDevWorker/LocalRuntimeController";
1417
import { urlFromParts } from "../../../api/startDevWorker/utils";
1518
import { RuleTypeToModuleType } from "../../../deployment-bundle/module-collection";
1619
import { usingLocalSecretsStoreSecretAPI } from "../../../secrets-store/commands";
@@ -136,6 +139,48 @@ describe("LocalRuntimeController", () => {
136139
// Make sure teardown is declared after runInTempDir so it runs before we delete the temp directory
137140
const teardown = useTeardown();
138141

142+
describe("getUserWorkerInnerUrlOverrides", () => {
143+
it("parses host and port when origin hostname includes a port", ({
144+
expect,
145+
}) => {
146+
expect(
147+
getUserWorkerInnerUrlOverrides({
148+
dev: {
149+
persist: "./persist",
150+
origin: {
151+
hostname: "localhost:4000",
152+
secure: false,
153+
},
154+
},
155+
})
156+
).toEqual({
157+
protocol: "http:",
158+
hostname: "localhost",
159+
port: "4000",
160+
});
161+
});
162+
163+
it("clears the local dev port when origin hostname does not include one", ({
164+
expect,
165+
}) => {
166+
expect(
167+
getUserWorkerInnerUrlOverrides({
168+
dev: {
169+
persist: "./persist",
170+
origin: {
171+
hostname: "www.example.com",
172+
secure: false,
173+
},
174+
},
175+
})
176+
).toEqual({
177+
protocol: "http:",
178+
hostname: "www.example.com",
179+
port: "",
180+
});
181+
});
182+
});
183+
139184
describe("Core", () => {
140185
it("should start Miniflare with module worker", async ({ expect }) => {
141186
const bus = new FakeBus();

packages/wrangler/src/api/startDevWorker/LocalRuntimeController.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ function getName(config: StartDevWorkerOptions) {
4545
return config.name;
4646
}
4747

48+
export function getUserWorkerInnerUrlOverrides(
49+
config: Pick<StartDevWorkerOptions, "dev">
50+
) {
51+
const protocol = config.dev?.origin?.secure ? "https:" : "http:";
52+
const host = config.dev?.origin?.hostname;
53+
if (!host) {
54+
return { protocol };
55+
}
56+
57+
try {
58+
const parsedHost = new URL(`http://${host}`);
59+
return {
60+
protocol,
61+
hostname: parsedHost.hostname,
62+
port: parsedHost.port,
63+
};
64+
} catch {
65+
return {
66+
protocol,
67+
hostname: host,
68+
};
69+
}
70+
}
71+
4872
export async function convertToConfigBundle(
4973
event: BundleCompleteEvent
5074
): Promise<MF.ConfigBundle> {
@@ -350,11 +374,9 @@ export class LocalRuntimeController extends RuntimeController {
350374
},
351375
}
352376
: {}),
353-
userWorkerInnerUrlOverrides: {
354-
protocol: data.config?.dev?.origin?.secure ? "https:" : "http:",
355-
hostname: data.config?.dev?.origin?.hostname,
356-
port: data.config?.dev?.origin?.hostname ? "" : undefined,
357-
},
377+
userWorkerInnerUrlOverrides: getUserWorkerInnerUrlOverrides(
378+
data.config
379+
),
358380
headers: {
359381
// Passing this signature from Proxy Worker allows the User Worker to trust the request.
360382
"MF-Proxy-Shared-Secret":

packages/wrangler/src/api/startDevWorker/MultiworkerRuntimeController.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { castErrorCause } from "./events";
1010
import {
1111
convertToConfigBundle,
1212
getContainerDevOptions,
13+
getUserWorkerInnerUrlOverrides,
1314
LocalRuntimeController,
1415
} from "./LocalRuntimeController";
1516
import type { RemoteProxySession } from "../remoteBindings";
@@ -238,11 +239,9 @@ export class MultiworkerRuntimeController extends LocalRuntimeController {
238239
port: userWorkerInspectorUrl.port,
239240
pathname: `/core:user:${data.config.name}`,
240241
},
241-
userWorkerInnerUrlOverrides: {
242-
protocol: data.config?.dev?.origin?.secure ? "https:" : "http:",
243-
hostname: data.config?.dev?.origin?.hostname,
244-
port: data.config?.dev?.origin?.hostname ? "" : undefined,
245-
},
242+
userWorkerInnerUrlOverrides: getUserWorkerInnerUrlOverrides(
243+
data.config
244+
),
246245
headers: {
247246
// Passing this signature from Proxy Worker allows the User Worker to trust the request.
248247
"MF-Proxy-Shared-Secret":

0 commit comments

Comments
 (0)