Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 46 additions & 12 deletions sdk/typescript/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,18 +465,8 @@ function preferredAuthUrl(value: string): string | null {
)) {
const url = match[0].replace(/[.,;:!?)\]}]+$/, "");
try {
const hostname = new URL(url).hostname.toLowerCase().replace(/\.$/, "");
if (
hostname !== "localhost" &&
!hostname.endsWith(".localhost") &&
!(isIP(hostname) === 4 && hostname.startsWith("127.")) &&
hostname !== "0.0.0.0" &&
hostname !== "[::1]" &&
hostname !== "[::]" &&
hostname !== "[::ffff:0:0]" &&
!hostname.startsWith("[::ffff:7f") &&
!hostname.startsWith("[::7f")
) {
const hostname = new URL(url).hostname;
if (!isLoopbackHost(hostname)) {
return url;
}
} catch {
Expand All @@ -486,6 +476,50 @@ function preferredAuthUrl(value: string): string | null {
return null;
}

function isLoopbackHost(hostname: string): boolean {
const host = hostname
.toLowerCase()
.replace(/\.$/, "")
.replace(/^\[|\]$/g, "");
if (host === "localhost" || host.endsWith(".localhost")) {
return true;
}
const ipVersion = isIP(host);
if (ipVersion === 4) {
return host === "0.0.0.0" || host.startsWith("127.");
}
if (ipVersion === 6) {
if (
host === "::1" ||
host === "0:0:0:0:0:0:0:1" ||
host === "::" ||
host === "0:0:0:0:0:0:0:0"
) {
return true;
}
if (
host === "::ffff:0.0.0.0" ||
host === "::ffff:0:0" ||
host === "0:0:0:0:0:ffff:0:0"
) {
return true;
}
if (
host.startsWith("::ffff:127.") ||
host.startsWith("::127.") ||
host.startsWith("0:0:0:0:0:ffff:127.") ||
host.startsWith("0:0:0:0:0:0:127.") ||
host.startsWith("::ffff:7f") ||
host.startsWith("::7f") ||
host.startsWith("0:0:0:0:0:ffff:7f") ||
host.startsWith("0:0:0:0:0:0:7f")
) {
return true;
}
}
return false;
}

function userCodeFromOutput(value: string): string | null {
const output = plainTerminalText(value);
return (
Expand Down
2 changes: 2 additions & 0 deletions sdk/typescript/tests-ts/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ if (args.join(" ") === "login --with-api-key") {
console.error("Listening on http://[::1]:1455.");
console.error("Listening on http://[::]:1455.");
console.error("Listening on http://[::ffff:127.0.0.1]:1455.");
console.error("Listening on http://[::ffff:7f00:1]:1455.");
console.error("Listening on http://[0:0:0:0:0:0:0:1]:1455.");
console.error("Listening on http://[::ffff:0.0.0.0]:1455.");
console.error("Listening on http://[::127.0.0.1]:1455.");
console.error("Open \\u001b[32mhttps://127.auth.example.test/device\\u001b[0m");
Expand Down