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.
66 lines
2.8 KiB
TypeScript
66 lines
2.8 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { PairRateLimiter } from "../../src/daemon/rate-limit.js";
|
|
|
|
/** A limiter whose clock the test drives, so windows slide without sleeping. */
|
|
function limiter(rules = { perClient: { limit: 3, windowMs: 1_000 }, global: { limit: 10, windowMs: 10_000 } }) {
|
|
let now = 1_000;
|
|
const instance = new PairRateLimiter(rules, { now: () => now });
|
|
return { instance, advance: (ms: number) => (now += ms) };
|
|
}
|
|
|
|
describe("PairRateLimiter", () => {
|
|
it("allows the budget and refuses the next attempt", () => {
|
|
const { instance } = limiter();
|
|
expect(instance.check("1.2.3.4").allowed).toBe(true);
|
|
expect(instance.check("1.2.3.4").allowed).toBe(true);
|
|
expect(instance.check("1.2.3.4").allowed).toBe(true);
|
|
|
|
const blocked = instance.check("1.2.3.4");
|
|
expect(blocked.allowed).toBe(false);
|
|
expect(blocked.retryAfterMs).toBeGreaterThan(0);
|
|
expect(blocked.retryAfterMs).toBeLessThanOrEqual(1_000);
|
|
});
|
|
|
|
it("recovers once the window slides", () => {
|
|
const { instance, advance } = limiter();
|
|
for (let attempt = 0; attempt < 3; attempt += 1) instance.check("1.2.3.4");
|
|
expect(instance.check("1.2.3.4").allowed).toBe(false);
|
|
|
|
advance(1_001);
|
|
expect(instance.check("1.2.3.4").allowed).toBe(true);
|
|
});
|
|
|
|
it("keeps per-client budgets separate", () => {
|
|
const { instance } = limiter();
|
|
for (let attempt = 0; attempt < 3; attempt += 1) instance.check("1.2.3.4");
|
|
expect(instance.check("1.2.3.4").allowed).toBe(false);
|
|
expect(instance.check("5.6.7.8").allowed).toBe(true);
|
|
});
|
|
|
|
it("applies the global budget across clients", () => {
|
|
const { instance } = limiter({ perClient: { limit: 5, windowMs: 1_000 }, global: { limit: 3, windowMs: 10_000 } });
|
|
expect(instance.check("a").allowed).toBe(true);
|
|
expect(instance.check("b").allowed).toBe(true);
|
|
expect(instance.check("c").allowed).toBe(true);
|
|
// A brand-new client is still refused: the global window is full.
|
|
const blocked = instance.check("d");
|
|
expect(blocked.allowed).toBe(false);
|
|
expect(blocked.retryAfterMs).toBeLessThanOrEqual(10_000);
|
|
});
|
|
|
|
it("forgets every window on reset", () => {
|
|
const { instance } = limiter();
|
|
for (let attempt = 0; attempt < 3; attempt += 1) instance.check("1.2.3.4");
|
|
expect(instance.check("1.2.3.4").allowed).toBe(false);
|
|
instance.reset();
|
|
expect(instance.check("1.2.3.4").allowed).toBe(true);
|
|
});
|
|
|
|
it("treats an empty client key as one bucket", () => {
|
|
const { instance } = limiter();
|
|
for (let attempt = 0; attempt < 3; attempt += 1) instance.check("");
|
|
expect(instance.check("unknown").allowed).toBe(false);
|
|
});
|
|
});
|