feat(daemon)!: phone-first binding and a two-step pairing flow
Implements ADR 0001 (docs/adr/0001-phone-first-pairing.md): the console is
meant to be opened from a phone, so reachability stops being a mode and the
pairing link becomes the only access control that matters.
Binding / CORS
- `PI2_HOST` defaults to `0.0.0.0`; `PI2_ACCESS_MODE` (local|lan|tailscale) is
deleted, together with its parser, `AccessMode` type and LAN probe.
- `src/core/network.ts` is the single source of truth for reachable addresses:
kind classification, ordering (public → lan → other → tailscale → loopback,
physical NICs first, link-local skipped), bind-aware `networkBases()`,
`primaryBase()` and `localOrigins()`. Config, the pairing route, doctor and
the CLI all consume it, so the CORS allowlist always matches the pairing URL.
- `/api/v1/info` and `daemon.json` report `host` instead of `access`; doctor's
`access` check becomes `bind` (loopback ok, wildcard warns unless an `https:`
public URL is configured).
Pairing
- `POST /api/v1/pair` only mints, requires a token, and returns the code, the
chosen URL, the expiry, the nonce, the QR matrix and every usable base.
`base` may pin one of those bases; anything else is a 400.
- `POST /api/v1/pair/redeem` is the public half, rate limited per client
(5/min) and globally (50/h) → 429 `E_RATE_LIMITED` with `retry-after`.
- `GET /pair/:code` answers 302 to `/connect?code=…&auto=1`, so a scanned link
needs no JavaScript to reach the console.
- `src/core/qr.ts` (moved out of the CLI) is the one encoder: byte mode, EC
level M, versions 1–10, rows without a quiet zone, shared by the HTTP mint
response and the terminal renderer.
- `pi2 pairing` prints a human report plus a half-block terminal QR and keeps
`--base`; `--json` and `--public-url` are gone (the Neovim client calls the
HTTP mint directly).
Device scopes
- `Scope`, `SCOPES`, `DEFAULT_SCOPES`, `DeviceRecord.scopes` and the `scopes`
field in device/hello payloads are removed: nothing enforced them, and a
paired device now holds the same rights as the local browser. A permission
model, if wanted, gets its own ADR.
Breaking: `PI2_ACCESS_MODE`, `info.access`, `pi2 status --json`'s `access`,
`pi2 pairing --json`/`--public-url`, `POST /api/v1/pair {code}`, and the
`scopes` field. All in-repo callers and the e2e drivers are updated; the plugin
side (`:PiWebAccess`, `env.PI2_ACCESS_MODE`, text-format parsing) follows in
pi2.nvim.
Verification: root typecheck clean; 369/370 tests (the one failure is the
pre-existing supervisor timing flake, 24/24 in isolation ×3); `smoke.sh` green;
web suite 299/299; visual matrix 24/24.
This commit is contained in:
1 parent
3789394c85
commit
a57c874095
34 files changed
+2157
-488
No files matched your search
+63
-74
@@ -1,7 +1,9 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { loadConfig } from "../../src/config.js";
|
||||
import type { NetworkInterfaceInfo } from "node:os";
|
||||
import { DEFAULT_HOST, loadConfig } from "../../src/config.js";
|
||||
import { localOrigins } from "../../src/core/network.js";
|
||||
|
||||
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..");
|
||||
|
||||
@@ -11,12 +13,22 @@ const LOOPBACK = (port: number): string[] => [
|
||||
`http://[::1]:${port}`,
|
||||
];
|
||||
|
||||
const ipv4 = (address: string, overrides: Partial<NetworkInterfaceInfo> = {}): NetworkInterfaceInfo => ({
|
||||
address,
|
||||
netmask: "255.255.255.0",
|
||||
family: "IPv4",
|
||||
mac: "00:00:00:00:00:00",
|
||||
internal: false,
|
||||
cidr: `${address}/24`,
|
||||
...overrides,
|
||||
});
|
||||
|
||||
describe("loadConfig", () => {
|
||||
it("uses protocol defaults (access mode local)", () => {
|
||||
const config = loadConfig({});
|
||||
expect(config.host).toBe("127.0.0.1");
|
||||
it("uses protocol defaults and binds every interface", () => {
|
||||
const config = loadConfig({}, { interfaces: {} });
|
||||
expect(DEFAULT_HOST).toBe("0.0.0.0");
|
||||
expect(config.host).toBe("0.0.0.0");
|
||||
expect(config.port).toBe(7676);
|
||||
expect(config.access).toBe("local");
|
||||
expect(config.publicUrl).toBeNull();
|
||||
expect(config.staticDir).toBe(join(repoRoot, "web", "dist"));
|
||||
expect(config.corsOrigins).toEqual(LOOPBACK(7676));
|
||||
@@ -28,14 +40,17 @@ describe("loadConfig", () => {
|
||||
});
|
||||
|
||||
it("honours environment overrides; PI2_ORIGINS is additive", () => {
|
||||
const config = loadConfig({
|
||||
PI2_PORT: "0",
|
||||
PI2_HOST: "0.0.0.0",
|
||||
PI2_STATIC_DIR: "/srv/pi2/web",
|
||||
PI2_ORIGINS: "http://a.test, http://b.test ,,",
|
||||
});
|
||||
const config = loadConfig(
|
||||
{
|
||||
PI2_PORT: "0",
|
||||
PI2_HOST: "127.0.0.1",
|
||||
PI2_STATIC_DIR: "/srv/pi2/web",
|
||||
PI2_ORIGINS: "http://a.test, http://b.test ,,",
|
||||
},
|
||||
{ interfaces: {} },
|
||||
);
|
||||
expect(config.port).toBe(0);
|
||||
expect(config.host).toBe("0.0.0.0");
|
||||
expect(config.host).toBe("127.0.0.1");
|
||||
expect(config.staticDir).toBe("/srv/pi2/web");
|
||||
// Loopback origins stay in place; the extras are appended, normalized,
|
||||
// and empty entries dropped.
|
||||
@@ -48,80 +63,54 @@ describe("loadConfig", () => {
|
||||
expect(() => loadConfig({ PI2_PORT: "70000" })).toThrow(/PI2_PORT/);
|
||||
});
|
||||
|
||||
it("derives loopback origins from the port", () => {
|
||||
const config = loadConfig({ PI2_PORT: "8080" });
|
||||
expect(config.corsOrigins).toEqual(LOOPBACK(8080));
|
||||
});
|
||||
|
||||
it("derives local mode origins without a LAN address", () => {
|
||||
it("allows every local IPv4 origin at the bound port", () => {
|
||||
const config = loadConfig(
|
||||
{ PI2_ACCESS_MODE: "local" },
|
||||
{ detectLanIp: () => "192.168.2.10" },
|
||||
);
|
||||
expect(config.access).toBe("local");
|
||||
expect(config.host).toBe("127.0.0.1");
|
||||
expect(config.corsOrigins).toEqual(LOOPBACK(7676));
|
||||
});
|
||||
|
||||
it("lan mode binds 0.0.0.0 and adds the detected LAN origin", () => {
|
||||
const config = loadConfig(
|
||||
{ PI2_ACCESS_MODE: "lan" },
|
||||
{ detectLanIp: () => "192.168.2.10" },
|
||||
);
|
||||
expect(config.access).toBe("lan");
|
||||
expect(config.host).toBe("0.0.0.0");
|
||||
expect(config.corsOrigins).toEqual([...LOOPBACK(7676), "http://192.168.2.10:7676"]);
|
||||
});
|
||||
|
||||
it("lan mode skips the extra origin when detection fails or throws", () => {
|
||||
expect(
|
||||
loadConfig({ PI2_ACCESS_MODE: "lan" }, { detectLanIp: () => null }).corsOrigins,
|
||||
).toEqual(LOOPBACK(7676));
|
||||
expect(
|
||||
loadConfig(
|
||||
{ PI2_ACCESS_MODE: "lan" },
|
||||
{
|
||||
detectLanIp: () => {
|
||||
throw new Error("no interfaces");
|
||||
},
|
||||
{ PI2_PORT: "8080" },
|
||||
{
|
||||
interfaces: {
|
||||
lo: [ipv4("127.0.0.1", { internal: true })],
|
||||
wlan0: [ipv4("192.168.11.7"), ipv4("169.254.10.1")],
|
||||
tailscale0: [ipv4("100.64.0.14")],
|
||||
},
|
||||
).corsOrigins,
|
||||
).toEqual(LOOPBACK(7676));
|
||||
},
|
||||
);
|
||||
// Loopback aliases plus each reachable address; link-local is skipped.
|
||||
for (const origin of [...LOOPBACK(8080), "http://192.168.11.7:8080", "http://100.64.0.14:8080"]) {
|
||||
expect(config.corsOrigins).toContain(origin);
|
||||
}
|
||||
expect(config.corsOrigins).toHaveLength(LOOPBACK(8080).length + 2);
|
||||
});
|
||||
|
||||
it("tailscale mode stays on loopback and adds the public URL origin", () => {
|
||||
const config = loadConfig({
|
||||
PI2_ACCESS_MODE: "tailscale",
|
||||
PI2_PUBLIC_URL: "https://myhost.tail1234.ts.net/",
|
||||
});
|
||||
expect(config.access).toBe("tailscale");
|
||||
it("narrows the bind on PI2_HOST", () => {
|
||||
const config = loadConfig({ PI2_HOST: "127.0.0.1" }, { interfaces: { wlan0: [ipv4("192.168.11.7")] } });
|
||||
expect(config.host).toBe("127.0.0.1");
|
||||
expect(config.publicUrl).toBe("https://myhost.tail1234.ts.net");
|
||||
expect(config.corsOrigins).toEqual([...LOOPBACK(7676), "https://myhost.tail1234.ts.net"]);
|
||||
});
|
||||
|
||||
it("tailscale without PI2_PUBLIC_URL keeps only loopback origins", () => {
|
||||
const config = loadConfig({ PI2_ACCESS_MODE: "tailscale" });
|
||||
expect(config.publicUrl).toBeNull();
|
||||
// A loopback-only daemon is unreachable from the LAN, so the allowlist
|
||||
// must not advertise an address nothing is listening on.
|
||||
expect(config.corsOrigins).toEqual(LOOPBACK(7676));
|
||||
});
|
||||
|
||||
it("lets PI2_HOST win over the mode default", () => {
|
||||
expect(loadConfig({ PI2_ACCESS_MODE: "lan", PI2_HOST: "127.0.0.1" }).host).toBe("127.0.0.1");
|
||||
expect(loadConfig({ PI2_ACCESS_MODE: "tailscale", PI2_HOST: "0.0.0.0" }).host).toBe("0.0.0.0");
|
||||
expect(loadConfig({ PI2_ACCESS_MODE: "local", PI2_HOST: "192.168.2.5" }).host).toBe("192.168.2.5");
|
||||
it("adds the public URL origin and keeps loopback", () => {
|
||||
const config = loadConfig({ PI2_PUBLIC_URL: "https://myhost.tail1234.ts.net/" }, { interfaces: {} });
|
||||
expect(config.publicUrl).toBe("https://myhost.tail1234.ts.net");
|
||||
expect(config.corsOrigins).toContain("https://myhost.tail1234.ts.net");
|
||||
for (const origin of LOOPBACK(7676)) expect(config.corsOrigins).toContain(origin);
|
||||
});
|
||||
|
||||
it("normalizes and de-duplicates explicit origins", () => {
|
||||
const config = loadConfig({
|
||||
PI2_ORIGINS: "https://a.test/, http://127.0.0.1:7676, ,http://a.test",
|
||||
});
|
||||
expect(config.corsOrigins).toEqual([...LOOPBACK(7676), "https://a.test", "http://a.test"]);
|
||||
const config = loadConfig(
|
||||
{ PI2_ORIGINS: "https://a.test/, http://127.0.0.1:7676, ,http://a.test" },
|
||||
{ interfaces: {} },
|
||||
);
|
||||
expect(config.corsOrigins.filter((origin) => origin.endsWith("a.test"))).toEqual([
|
||||
"https://a.test",
|
||||
"http://a.test",
|
||||
]);
|
||||
expect(config.corsOrigins.filter((origin) => origin === "http://127.0.0.1:7676")).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("fails fast on an unknown access mode", () => {
|
||||
expect(() => loadConfig({ PI2_ACCESS_MODE: "public" })).toThrow(
|
||||
"PI2_ACCESS_MODE must be local, lan or tailscale",
|
||||
);
|
||||
it("derives only loopback origins for link-local addresses", () => {
|
||||
expect(
|
||||
localOrigins({ port: 7676, bindHost: "0.0.0.0", interfaces: { wlan0: [ipv4("169.254.10.1")] } }),
|
||||
).toEqual(LOOPBACK(7676));
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user