From c9a74fee79c4198b817b81947c09cf5ef6928622 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Wed, 24 Jan 2024 17:09:33 +0100 Subject: [PATCH] [wrangler] fix: listen on loopback for wrangler dev port check and login The `wrangler dev` port availability check triggers a firewall prompt on macOS while it briefly opens and closes listeners. The OAuth callback server from `wrangler login` has the same issue. Fix both cases by listening on the loopback address only. Relates to #4430 --- .changeset/smart-owls-jog.md | 10 ++++++++++ packages/wrangler/src/dev.tsx | 7 ++++++- packages/wrangler/src/dev/proxy.ts | 2 +- packages/wrangler/src/user/user.ts | 2 +- 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 .changeset/smart-owls-jog.md diff --git a/.changeset/smart-owls-jog.md b/.changeset/smart-owls-jog.md new file mode 100644 index 000000000000..59394205e820 --- /dev/null +++ b/.changeset/smart-owls-jog.md @@ -0,0 +1,10 @@ +--- +"wrangler": patch +--- + +fix: listen on loopback for wrangler dev port check and login + +Avoid listening on the wildcard address to reduce the attacker's surface and +avoid firewall prompts on macOS. + +Relates to #4430. diff --git a/packages/wrangler/src/dev.tsx b/packages/wrangler/src/dev.tsx index b6f4859d370f..bc58c32d4995 100644 --- a/packages/wrangler/src/dev.tsx +++ b/packages/wrangler/src/dev.tsx @@ -626,12 +626,17 @@ export async function startApiDev(args: StartDevOptions) { }; } /** + * Get an available TCP port number. + * * Avoiding calling `getPort()` multiple times by memoizing the first result. */ function memoizeGetPort(defaultPort?: number) { let portValue: number; return async () => { - return portValue || (portValue = await getPort({ port: defaultPort })); + // Check a specific host to avoid probing all local addresses. + portValue = + portValue ?? (await getPort({ port: defaultPort, host: "localhost" })); + return portValue; }; } /** diff --git a/packages/wrangler/src/dev/proxy.ts b/packages/wrangler/src/dev/proxy.ts index fe9fa3f8c633..b37c9d95abc9 100644 --- a/packages/wrangler/src/dev/proxy.ts +++ b/packages/wrangler/src/dev/proxy.ts @@ -686,7 +686,7 @@ export async function waitForPortToBeAvailable( doReject(err); } }); - server.listen(port, () => + server.listen(port, "localhost", () => terminator .terminate() .then(doResolve, () => diff --git a/packages/wrangler/src/user/user.ts b/packages/wrangler/src/user/user.ts index 90cc3a2fbbf2..f03ce3dbdc41 100644 --- a/packages/wrangler/src/user/user.ts +++ b/packages/wrangler/src/user/user.ts @@ -1014,7 +1014,7 @@ export async function login( } }); - server.listen(8976); + server.listen(8976, "localhost"); }); if (props?.browser) { logger.log(`Opening a link in your default browser: ${urlToOpen}`);