fix: harden tailscale serve auth
This commit is contained in:
@@ -125,6 +125,7 @@ describe("gateway auth", () => {
|
||||
const res = await authorizeGatewayConnect({
|
||||
auth: { mode: "token", token: "secret", allowTailscale: true },
|
||||
connectAuth: null,
|
||||
tailscaleWhois: async () => ({ login: "peter", name: "Peter" }),
|
||||
req: {
|
||||
socket: { remoteAddress: "127.0.0.1" },
|
||||
headers: {
|
||||
@@ -143,6 +144,28 @@ describe("gateway auth", () => {
|
||||
expect(res.user).toBe("peter");
|
||||
});
|
||||
|
||||
it("rejects mismatched tailscale identity when required", async () => {
|
||||
const res = await authorizeGatewayConnect({
|
||||
auth: { mode: "none", allowTailscale: true },
|
||||
connectAuth: null,
|
||||
tailscaleWhois: async () => ({ login: "alice@example.com", name: "Alice" }),
|
||||
req: {
|
||||
socket: { remoteAddress: "127.0.0.1" },
|
||||
headers: {
|
||||
host: "gateway.local",
|
||||
"x-forwarded-for": "100.64.0.1",
|
||||
"x-forwarded-proto": "https",
|
||||
"x-forwarded-host": "ai-hub.bone-egret.ts.net",
|
||||
"tailscale-user-login": "peter@example.com",
|
||||
"tailscale-user-name": "Peter",
|
||||
},
|
||||
} as never,
|
||||
});
|
||||
|
||||
expect(res.ok).toBe(false);
|
||||
expect(res.reason).toBe("tailscale_user_mismatch");
|
||||
});
|
||||
|
||||
it("treats trusted proxy loopback clients as direct", async () => {
|
||||
const res = await authorizeGatewayConnect({
|
||||
auth: { mode: "none", allowTailscale: true },
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { timingSafeEqual } from "node:crypto";
|
||||
import type { IncomingMessage } from "node:http";
|
||||
import type { GatewayAuthConfig, GatewayTailscaleMode } from "../config/config.js";
|
||||
import { isTrustedProxyAddress, resolveGatewayClientIp } from "./net.js";
|
||||
import { readTailscaleWhoisIdentity, type TailscaleWhoisIdentity } from "../infra/tailscale.js";
|
||||
import { isTrustedProxyAddress, parseForwardedForClientIp, resolveGatewayClientIp } from "./net.js";
|
||||
export type ResolvedGatewayAuthMode = "none" | "token" | "password";
|
||||
|
||||
export type ResolvedGatewayAuth = {
|
||||
@@ -29,11 +30,17 @@ type TailscaleUser = {
|
||||
profilePic?: string;
|
||||
};
|
||||
|
||||
type TailscaleWhoisLookup = (ip: string) => Promise<TailscaleWhoisIdentity | null>;
|
||||
|
||||
function safeEqual(a: string, b: string): boolean {
|
||||
if (a.length !== b.length) return false;
|
||||
return timingSafeEqual(Buffer.from(a), Buffer.from(b));
|
||||
}
|
||||
|
||||
function normalizeLogin(login: string): string {
|
||||
return login.trim().toLowerCase();
|
||||
}
|
||||
|
||||
function isLoopbackAddress(ip: string | undefined): boolean {
|
||||
if (!ip) return false;
|
||||
if (ip === "127.0.0.1") return true;
|
||||
@@ -58,6 +65,12 @@ function headerValue(value: string | string[] | undefined): string | undefined {
|
||||
return Array.isArray(value) ? value[0] : value;
|
||||
}
|
||||
|
||||
function resolveTailscaleClientIp(req?: IncomingMessage): string | undefined {
|
||||
if (!req) return undefined;
|
||||
const forwardedFor = headerValue(req.headers?.["x-forwarded-for"]);
|
||||
return forwardedFor ? parseForwardedForClientIp(forwardedFor) : undefined;
|
||||
}
|
||||
|
||||
function resolveRequestClientIp(
|
||||
req?: IncomingMessage,
|
||||
trustedProxies?: string[],
|
||||
@@ -118,6 +131,39 @@ function isTailscaleProxyRequest(req?: IncomingMessage): boolean {
|
||||
return isLoopbackAddress(req.socket?.remoteAddress) && hasTailscaleProxyHeaders(req);
|
||||
}
|
||||
|
||||
async function resolveVerifiedTailscaleUser(params: {
|
||||
req?: IncomingMessage;
|
||||
tailscaleWhois: TailscaleWhoisLookup;
|
||||
}): Promise<{ ok: true; user: TailscaleUser } | { ok: false; reason: string }> {
|
||||
const { req, tailscaleWhois } = params;
|
||||
const tailscaleUser = getTailscaleUser(req);
|
||||
if (!tailscaleUser) {
|
||||
return { ok: false, reason: "tailscale_user_missing" };
|
||||
}
|
||||
if (!isTailscaleProxyRequest(req)) {
|
||||
return { ok: false, reason: "tailscale_proxy_missing" };
|
||||
}
|
||||
const clientIp = resolveTailscaleClientIp(req);
|
||||
if (!clientIp) {
|
||||
return { ok: false, reason: "tailscale_whois_failed" };
|
||||
}
|
||||
const whois = await tailscaleWhois(clientIp);
|
||||
if (!whois?.login) {
|
||||
return { ok: false, reason: "tailscale_whois_failed" };
|
||||
}
|
||||
if (normalizeLogin(whois.login) !== normalizeLogin(tailscaleUser.login)) {
|
||||
return { ok: false, reason: "tailscale_user_mismatch" };
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
user: {
|
||||
login: whois.login,
|
||||
name: whois.name ?? tailscaleUser.name,
|
||||
profilePic: tailscaleUser.profilePic,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveGatewayAuth(params: {
|
||||
authConfig?: GatewayAuthConfig | null;
|
||||
env?: NodeJS.ProcessEnv;
|
||||
@@ -155,29 +201,26 @@ export async function authorizeGatewayConnect(params: {
|
||||
connectAuth?: ConnectAuth | null;
|
||||
req?: IncomingMessage;
|
||||
trustedProxies?: string[];
|
||||
tailscaleWhois?: TailscaleWhoisLookup;
|
||||
}): Promise<GatewayAuthResult> {
|
||||
const { auth, connectAuth, req, trustedProxies } = params;
|
||||
const tailscaleWhois = params.tailscaleWhois ?? readTailscaleWhoisIdentity;
|
||||
const localDirect = isLocalDirectRequest(req, trustedProxies);
|
||||
|
||||
if (auth.allowTailscale && !localDirect) {
|
||||
const tailscaleUser = getTailscaleUser(req);
|
||||
const tailscaleProxy = isTailscaleProxyRequest(req);
|
||||
|
||||
if (tailscaleUser && tailscaleProxy) {
|
||||
const tailscaleCheck = await resolveVerifiedTailscaleUser({
|
||||
req,
|
||||
tailscaleWhois,
|
||||
});
|
||||
if (tailscaleCheck.ok) {
|
||||
return {
|
||||
ok: true,
|
||||
method: "tailscale",
|
||||
user: tailscaleUser.login,
|
||||
user: tailscaleCheck.user.login,
|
||||
};
|
||||
}
|
||||
|
||||
if (auth.mode === "none") {
|
||||
if (!tailscaleUser) {
|
||||
return { ok: false, reason: "tailscale_user_missing" };
|
||||
}
|
||||
if (!tailscaleProxy) {
|
||||
return { ok: false, reason: "tailscale_proxy_missing" };
|
||||
}
|
||||
return { ok: false, reason: tailscaleCheck.reason };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,7 +235,7 @@ export async function authorizeGatewayConnect(params: {
|
||||
if (!connectAuth?.token) {
|
||||
return { ok: false, reason: "token_missing" };
|
||||
}
|
||||
if (connectAuth.token !== auth.token) {
|
||||
if (!safeEqual(connectAuth.token, auth.token)) {
|
||||
return { ok: false, reason: "token_mismatch" };
|
||||
}
|
||||
return { ok: true, method: "token" };
|
||||
|
||||
@@ -36,7 +36,7 @@ function stripOptionalPort(ip: string): string {
|
||||
return ip;
|
||||
}
|
||||
|
||||
function parseForwardedForClientIp(forwardedFor?: string): string | undefined {
|
||||
export function parseForwardedForClientIp(forwardedFor?: string): string | undefined {
|
||||
const raw = forwardedFor?.split(",")[0]?.trim();
|
||||
if (!raw) return undefined;
|
||||
return normalizeIp(stripOptionalPort(raw));
|
||||
|
||||
@@ -100,6 +100,10 @@ function formatGatewayAuthFailureMessage(params: {
|
||||
return "unauthorized: tailscale identity missing (use Tailscale Serve auth or gateway token/password)";
|
||||
case "tailscale_proxy_missing":
|
||||
return "unauthorized: tailscale proxy headers missing (use Tailscale Serve or gateway token/password)";
|
||||
case "tailscale_whois_failed":
|
||||
return "unauthorized: tailscale identity check failed (use Tailscale Serve auth or gateway token/password)";
|
||||
case "tailscale_user_mismatch":
|
||||
return "unauthorized: tailscale identity mismatch (use Tailscale Serve auth or gateway token/password)";
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -213,6 +213,18 @@ type ExecErrorDetails = {
|
||||
code?: unknown;
|
||||
};
|
||||
|
||||
export type TailscaleWhoisIdentity = {
|
||||
login: string;
|
||||
name?: string;
|
||||
};
|
||||
|
||||
type TailscaleWhoisCacheEntry = {
|
||||
value: TailscaleWhoisIdentity | null;
|
||||
expiresAt: number;
|
||||
};
|
||||
|
||||
const whoisCache = new Map<string, TailscaleWhoisCacheEntry>();
|
||||
|
||||
function extractExecErrorText(err: unknown) {
|
||||
const errOutput = err as ExecErrorDetails;
|
||||
const stdout = typeof errOutput.stdout === "string" ? errOutput.stdout : "";
|
||||
@@ -381,3 +393,73 @@ export async function disableTailscaleFunnel(exec: typeof runExec = runExec) {
|
||||
timeoutMs: 15_000,
|
||||
});
|
||||
}
|
||||
|
||||
function getString(value: unknown): string | undefined {
|
||||
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
||||
}
|
||||
|
||||
function readRecord(value: unknown): Record<string, unknown> | null {
|
||||
return value && typeof value === "object" ? (value as Record<string, unknown>) : null;
|
||||
}
|
||||
|
||||
function parseWhoisIdentity(payload: Record<string, unknown>): TailscaleWhoisIdentity | null {
|
||||
const userProfile =
|
||||
readRecord(payload.UserProfile) ?? readRecord(payload.userProfile) ?? readRecord(payload.User);
|
||||
const login =
|
||||
getString(userProfile?.LoginName) ??
|
||||
getString(userProfile?.Login) ??
|
||||
getString(userProfile?.login) ??
|
||||
getString(payload.LoginName) ??
|
||||
getString(payload.login);
|
||||
if (!login) return null;
|
||||
const name =
|
||||
getString(userProfile?.DisplayName) ??
|
||||
getString(userProfile?.Name) ??
|
||||
getString(userProfile?.displayName) ??
|
||||
getString(payload.DisplayName) ??
|
||||
getString(payload.name);
|
||||
return { login, name };
|
||||
}
|
||||
|
||||
function readCachedWhois(ip: string, now: number): TailscaleWhoisIdentity | null | undefined {
|
||||
const cached = whoisCache.get(ip);
|
||||
if (!cached) return undefined;
|
||||
if (cached.expiresAt <= now) {
|
||||
whoisCache.delete(ip);
|
||||
return undefined;
|
||||
}
|
||||
return cached.value;
|
||||
}
|
||||
|
||||
function writeCachedWhois(ip: string, value: TailscaleWhoisIdentity | null, ttlMs: number) {
|
||||
whoisCache.set(ip, { value, expiresAt: Date.now() + ttlMs });
|
||||
}
|
||||
|
||||
export async function readTailscaleWhoisIdentity(
|
||||
ip: string,
|
||||
exec: typeof runExec = runExec,
|
||||
opts?: { timeoutMs?: number; cacheTtlMs?: number; errorTtlMs?: number },
|
||||
): Promise<TailscaleWhoisIdentity | null> {
|
||||
const normalized = ip.trim();
|
||||
if (!normalized) return null;
|
||||
const now = Date.now();
|
||||
const cached = readCachedWhois(normalized, now);
|
||||
if (cached !== undefined) return cached;
|
||||
|
||||
const cacheTtlMs = opts?.cacheTtlMs ?? 60_000;
|
||||
const errorTtlMs = opts?.errorTtlMs ?? 5_000;
|
||||
try {
|
||||
const tailscaleBin = await getTailscaleBinary();
|
||||
const { stdout } = await exec(tailscaleBin, ["whois", "--json", normalized], {
|
||||
timeoutMs: opts?.timeoutMs ?? 5_000,
|
||||
maxBuffer: 200_000,
|
||||
});
|
||||
const parsed = stdout ? parsePossiblyNoisyJsonObject(stdout) : {};
|
||||
const identity = parseWhoisIdentity(parsed);
|
||||
writeCachedWhois(normalized, identity, cacheTtlMs);
|
||||
return identity;
|
||||
} catch {
|
||||
writeCachedWhois(normalized, null, errorTtlMs);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user