Skip to content

Commit a1e0ad2

Browse files
committed
fix(oauth): bind port check to 127.0.0.1 to match callback server
checkPortAvailable() was binding to all interfaces (0.0.0.0), while OAuthCallbackServer binds to 127.0.0.1. This mismatch caused false positives when checking port 5173 availability while Vite dev server was running on 127.0.0.1:5173. The callback server would then fail with EADDRINUSE when trying to bind to the same port on 127.0.0.1. Changes: - Add OAUTH_CALLBACK_HOST constant (127.0.0.1) with documentation - Use constant in both checkPortAvailable() and OAuthCallbackServer - Add debug logging with error code when port is unavailable
1 parent 70b93c4 commit a1e0ad2

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/auth/callbackServer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as http from "http";
22
import { timingSafeEqual } from "crypto";
33
import { getLogger } from "../logging.js";
4+
import { OAUTH_CALLBACK_HOST } from "../utils/portFinder.js";
45

56
const logger = getLogger();
67

@@ -336,12 +337,11 @@ export class OAuthCallbackServer {
336337
}
337338
});
338339

339-
// Bind to loopback only to avoid triggering Windows Firewall prompts.
340-
// Previously bound to all interfaces (0.0.0.0) which triggered firewall dialogs.
340+
// Bind to OAUTH_CALLBACK_HOST (127.0.0.1) to avoid triggering Windows Firewall prompts.
341341
// Note: If users have issues with localhost resolving to ::1 (IPv6), we may need
342342
// to create dual listeners or ensure redirect URIs use 127.0.0.1 explicitly.
343-
this.server.listen(this.port, '127.0.0.1', () => {
344-
logger.info("OAuth callback server started", { port: this.port, host: '127.0.0.1' });
343+
this.server.listen(this.port, OAUTH_CALLBACK_HOST, () => {
344+
logger.info("OAuth callback server started", { port: this.port, host: OAUTH_CALLBACK_HOST });
345345
resolve();
346346
});
347347

src/utils/portFinder.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ import { getLogger } from "../logging.js";
33

44
const logger = getLogger();
55

6+
/**
7+
* The host address used for OAuth callback server binding.
8+
* Using 127.0.0.1 (IPv4 loopback) to:
9+
* - Avoid Windows Firewall prompts (binding to 0.0.0.0 triggers dialogs)
10+
* - Ensure consistent behavior across platforms
11+
*
12+
* Note: redirect_uris use "localhost" (see simple.ts), which typically resolves
13+
* to 127.0.0.1 on most systems. If IPv6 issues arise (localhost -> ::1), we may
14+
* need to align redirect_uris with this constant.
15+
*
16+
* IMPORTANT: This constant must be used by both:
17+
* - checkPortAvailable() in this file
18+
* - OAuthCallbackServer.start() in callbackServer.ts
19+
*/
20+
export const OAUTH_CALLBACK_HOST = "127.0.0.1";
21+
622
/**
723
* Find an available port starting from a given port number.
824
* Tries consecutive ports until one is available or max attempts reached.
@@ -32,16 +48,25 @@ export async function findAvailablePort(
3248
}
3349

3450
/**
35-
* Check if a specific port is available for binding.
51+
* Check if a specific port is available for binding on OAUTH_CALLBACK_HOST.
52+
* Must match the actual binding used by OAuthCallbackServer.
3653
*/
3754
export function checkPortAvailable(port: number): Promise<boolean> {
3855
return new Promise((resolve) => {
3956
const server = net.createServer();
40-
server.once("error", () => resolve(false));
57+
server.once("error", (err: NodeJS.ErrnoException) => {
58+
logger.debug("Port unavailable", {
59+
port,
60+
host: OAUTH_CALLBACK_HOST,
61+
code: err.code,
62+
message: err.message
63+
});
64+
resolve(false);
65+
});
4166
server.once("listening", () => {
4267
server.close(() => resolve(true));
4368
});
44-
// Bind to all interfaces (no host specified) to match callback server behavior
45-
server.listen(port);
69+
// Bind to OAUTH_CALLBACK_HOST to match OAuthCallbackServer behavior
70+
server.listen(port, OAUTH_CALLBACK_HOST);
4671
});
4772
}

0 commit comments

Comments
 (0)