Skip to content

Commit bd10e19

Browse files
fix(tailscale): preserve parse errors for malformed JSON
Accept the fail-closed behavior for malformed `tailscale funnel status --json`: noisy valid JSON should parse, but malformed status output should not silently become “route absent”. Source PR: #63321 Co-authored-by: Francisco Maestre Torreblanca <[email protected]>
1 parent 650c5ca commit bd10e19

2 files changed

Lines changed: 101 additions & 6 deletions

File tree

‎src/infra/tailscale.test.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
enableTailscaleServe,
1313
disableTailscaleServe,
1414
ensureFunnel,
15+
hasTailscaleFunnelRouteForPort,
1516
tailscaleFunnelStatusCoversPort,
1617
} = tailscale;
1718
const tailscaleBin = "tailscale";
@@ -89,6 +90,47 @@ describe("tailscale helpers", () => {
8990
expect(host).toBe("noisy.tailnet.ts.net");
9091
});
9192

93+
it("parses noisy JSON output from tailscale whois", async () => {
94+
const exec = vi.fn().mockResolvedValue({
95+
stdout:
96+
'warning: stale state\n{"UserProfile":{"LoginName":"[email protected]","DisplayName":"Operator"}}\n',
97+
});
98+
99+
await expect(readTailscaleWhoisIdentity("100.64.0.11", exec)).resolves.toEqual({
100+
101+
name: "Operator",
102+
});
103+
});
104+
105+
it("caches malformed tailscale whois output on the short error TTL path", async () => {
106+
vi.useFakeTimers();
107+
vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
108+
const exec = vi
109+
.fn()
110+
.mockResolvedValueOnce({ stdout: "warning: stale state\n{not json}\n" })
111+
.mockResolvedValueOnce({
112+
stdout: JSON.stringify({ UserProfile: { LoginName: "[email protected]" } }),
113+
});
114+
115+
await expect(
116+
readTailscaleWhoisIdentity("100.64.0.12", exec, { errorTtlMs: 1_000 }),
117+
).resolves.toBeNull();
118+
await expect(
119+
readTailscaleWhoisIdentity("100.64.0.12", exec, { errorTtlMs: 1_000 }),
120+
).resolves.toBeNull();
121+
expect(exec).toHaveBeenCalledTimes(1);
122+
123+
vi.advanceTimersByTime(1_001);
124+
125+
await expect(
126+
readTailscaleWhoisIdentity("100.64.0.12", exec, { errorTtlMs: 1_000 }),
127+
).resolves.toEqual({
128+
129+
});
130+
131+
expect(exec).toHaveBeenCalledTimes(2);
132+
});
133+
92134
it("does not cache whois results when the cache expiry would exceed Date range", async () => {
93135
vi.useFakeTimers();
94136
vi.setSystemTime(new Date(8_640_000_000_000_000));
@@ -277,6 +319,40 @@ describe("tailscale helpers", () => {
277319
});
278320
});
279321

322+
it("ensureFunnel accepts noisy JSON status output", async () => {
323+
const exec = vi
324+
.fn()
325+
.mockResolvedValueOnce({
326+
stdout: 'warning: stale state\n{"BackendState":"Running"}\n',
327+
})
328+
.mockResolvedValueOnce({ stdout: "" });
329+
const runtime = createRuntimeWithExitError();
330+
const prompt = vi.fn();
331+
332+
await ensureFunnel(8080, exec as never, runtime, prompt);
333+
334+
expect(exec).toHaveBeenCalledTimes(2);
335+
expectExecCall(exec, 2, tailscaleBin, ["funnel", "--yes", "--bg", "8080"], {
336+
maxBuffer: 200_000,
337+
timeoutMs: 15_000,
338+
});
339+
expect(prompt).not.toHaveBeenCalled();
340+
});
341+
342+
it("ensureFunnel treats malformed status output as a failure", async () => {
343+
const exec = vi.fn().mockResolvedValueOnce({ stdout: "warning: stale state\n{not json}\n" });
344+
const runtime = createRuntimeWithExitError();
345+
const prompt = vi.fn();
346+
347+
await expect(ensureFunnel(8080, exec as never, runtime, prompt)).rejects.toThrow("exit 1");
348+
349+
expect(exec).toHaveBeenCalledTimes(1);
350+
expect(prompt).not.toHaveBeenCalled();
351+
expect(runtime.error).toHaveBeenCalledWith(
352+
"Failed to enable Tailscale Funnel. Is it allowed on your tailnet?",
353+
);
354+
});
355+
280356
it("enableTailscaleServe skips sudo on non-permission errors", async () => {
281357
const exec = vi.fn().mockRejectedValueOnce(new Error("boom"));
282358

@@ -298,6 +374,23 @@ describe("tailscale helpers", () => {
298374

299375
expect(exec).toHaveBeenCalledTimes(2);
300376
});
377+
378+
it("hasTailscaleFunnelRouteForPort accepts noisy JSON status output", async () => {
379+
const exec = vi.fn().mockResolvedValue({
380+
stdout:
381+
'warning: stale state\n{"AllowFunnel":{"device.tailnet.ts.net:443":true},"Web":{"device.tailnet.ts.net:443":{"Handlers":{"/":{"Proxy":"http://127.0.0.1:18789"}}}}}\n',
382+
});
383+
384+
await expect(hasTailscaleFunnelRouteForPort(18789, exec)).resolves.toBe(true);
385+
});
386+
387+
it("hasTailscaleFunnelRouteForPort preserves malformed status parse failures", async () => {
388+
const exec = vi.fn().mockResolvedValue({
389+
stdout: "warning: stale state\n{not json}\n",
390+
});
391+
392+
await expect(hasTailscaleFunnelRouteForPort(18789, exec)).rejects.toThrow(SyntaxError);
393+
});
301394
});
302395

303396
describe("tailscaleFunnelStatusCoversPort", () => {

‎src/infra/tailscale.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ export async function ensureFunnel(
325325
try {
326326
const tailscaleBin = await getTailscaleBinary();
327327
const statusOut = (await exec(tailscaleBin, ["funnel", "status", "--json"])).stdout.trim();
328-
const parsed = statusOut ? (JSON.parse(statusOut) as Record<string, unknown>) : {};
328+
const parsed = statusOut ? parsePossiblyNoisyJsonObject(statusOut) : {};
329329
if (!parsed || Object.keys(parsed).length === 0) {
330330
runtime.error(danger("Tailscale Funnel is not enabled on this tailnet/device."));
331331
runtime.error(
@@ -426,17 +426,19 @@ export async function hasTailscaleFunnelRouteForPort(
426426
port: number,
427427
exec: typeof runExec = runExec,
428428
): Promise<boolean> {
429+
let stdout: string;
429430
try {
430431
const tailscaleBin = await getTailscaleBinary();
431-
const { stdout } = await exec(tailscaleBin, ["funnel", "status", "--json"], {
432+
const result = await exec(tailscaleBin, ["funnel", "status", "--json"], {
432433
maxBuffer: 200_000,
433434
timeoutMs: 5_000,
434435
});
435-
const parsed = stdout ? parsePossiblyNoisyJsonObject(stdout) : {};
436-
return tailscaleFunnelStatusCoversPort(parsed, port);
436+
stdout = result.stdout;
437437
} catch {
438438
return false;
439439
}
440+
const parsed = stdout ? parsePossiblyNoisyJsonObject(stdout) : {};
441+
return tailscaleFunnelStatusCoversPort(parsed, port);
440442
}
441443

442444
const TAILSCALE_LOOPBACK_PROXY_HOSTS = new Set(["127.0.0.1", "localhost", "[::1]", "::1"]);
@@ -606,11 +608,11 @@ export async function readTailscaleWhoisIdentity(
606608
const errorTtlMs = opts?.errorTtlMs ?? 5_000;
607609
try {
608610
const tailscaleBin = await getTailscaleBinary();
609-
const { stdout } = await exec(tailscaleBin, ["whois", "--json", normalized], {
611+
const result = await exec(tailscaleBin, ["whois", "--json", normalized], {
610612
timeoutMs: opts?.timeoutMs ?? 5_000,
611613
maxBuffer: 200_000,
612614
});
613-
const parsed = stdout ? parsePossiblyNoisyJsonObject(stdout) : {};
615+
const parsed = result.stdout ? parsePossiblyNoisyJsonObject(result.stdout) : {};
614616
const identity = parseWhoisIdentity(parsed);
615617
writeCachedWhois(normalized, identity, cacheTtlMs);
616618
return identity;

0 commit comments

Comments
 (0)