-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[wrangler] fix: listen on loopback for wrangler dev port check and login #4830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| "wrangler": patch | ||
| --- | ||
|
|
||
| fix: listen on loopback for wrangler dev port check and login | ||
|
|
||
| Avoid listening on the wildcard address by default to reduce the attacker's | ||
| surface and avoid firewall prompts on macOS. | ||
|
|
||
| Relates to #4430. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -626,12 +626,16 @@ 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) { | ||||||
| function memoizeGetPort(defaultPort: number, host: string) { | ||||||
| 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: host })); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT - this can also be:
Suggested change
|
||||||
| return portValue; | ||||||
| }; | ||||||
| } | ||||||
| /** | ||||||
|
|
@@ -705,14 +709,16 @@ async function validateDevServerSettings( | |||||
| ); | ||||||
|
|
||||||
| const { zoneId, host, routes } = await getZoneIdHostAndRoutes(args, config); | ||||||
| const getLocalPort = memoizeGetPort(DEFAULT_LOCAL_PORT); | ||||||
| const getInspectorPort = memoizeGetPort(DEFAULT_INSPECTOR_PORT); | ||||||
| const initialIp = args.ip || config.dev.ip; | ||||||
| const initialIpListenCheck = initialIp === "*" ? "0.0.0.0" : initialIp; | ||||||
| const getLocalPort = memoizeGetPort(DEFAULT_LOCAL_PORT, initialIpListenCheck); | ||||||
| const getInspectorPort = memoizeGetPort(DEFAULT_INSPECTOR_PORT, "localhost"); | ||||||
|
|
||||||
| // Our inspector proxy server will be binding to the result of | ||||||
| // `getInspectorPort`. If we attempted to bind workerd to the same inspector | ||||||
| // port, we'd get a port already in use error. Therefore, generate a new port | ||||||
| // for our runtime to bind its inspector service to. | ||||||
| const getRuntimeInspectorPort = memoizeGetPort(); | ||||||
| const getRuntimeInspectorPort = memoizeGetPort(0, "localhost"); | ||||||
|
|
||||||
| if (config.services && config.services.length > 0) { | ||||||
| logger.warn( | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -154,7 +154,7 @@ export async function startPreviewServer({ | |||
| accessTokenRef, | ||||
| }); | ||||
|
|
||||
| await waitForPortToBeAvailable(port, { | ||||
| await waitForPortToBeAvailable(port, ip, { | ||||
| retryPeriod: 200, | ||||
| timeout: 2000, | ||||
| abortSignal: abortController.signal, | ||||
|
|
@@ -295,7 +295,7 @@ export function usePreviewServer({ | |||
| return; | ||||
| } | ||||
|
|
||||
| waitForPortToBeAvailable(port, { | ||||
| waitForPortToBeAvailable(port, ip, { | ||||
| retryPeriod: 200, | ||||
| timeout: 2000, | ||||
| abortSignal: abortController.signal, | ||||
|
|
@@ -636,9 +636,13 @@ function createStreamHandler( | |||
| */ | ||||
| export async function waitForPortToBeAvailable( | ||||
| port: number, | ||||
| host: string, | ||||
| options: { retryPeriod: number; timeout: number; abortSignal: AbortSignal } | ||||
| ): Promise<void> { | ||||
| return new Promise((resolve, reject) => { | ||||
| if (host === "*") { | ||||
| host = "0.0.0.0"; | ||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure how I can trigger this code path as making Based on the following existing code, I added the explicit check for
|
||||
| } | ||||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||||
| options.abortSignal.addEventListener("abort", () => { | ||||
| const abortError = new Error("waitForPortToBeAvailable() aborted"); | ||||
|
|
@@ -686,7 +690,7 @@ export async function waitForPortToBeAvailable( | |||
| doReject(err); | ||||
| } | ||||
| }); | ||||
| server.listen(port, () => | ||||
| server.listen(port, host, () => | ||||
| terminator | ||||
| .terminate() | ||||
| .then(doResolve, () => | ||||
|
|
||||
Uh oh!
There was an error while loading. Please reload this page.