Files
OwnCord/Client/tests/unit/capabilities-scope.test.ts
T
Claude 474bb2175c test: give the cross-stack contracts a named tier (RL-11)
`Client/tests/unit/admin-static-channel-perms.test.ts` reads and executes
`Server/admin/static/index.html`. Filed under `tests/unit`, nothing about its
location or name said it locks a server-owned artifact, so a Go developer
editing the admin SPA got a red check called "Client Unit Tests" with no clue
why.

The register describes this as one file. It is not, and the measured set does
not match the description in either direction:
- Client -> Server: exactly ONE test crosses by filesystem read, not two.
  `main-page.test.ts` was named in the plan but only carries a prose comment
  citing `Server/admin/update_handlers.go:181` at line 1046 — no read, no
  import, nothing to move.
- Server -> Client: the four tests the plan named do not cross.
  `waf_test.go`/`waf_crs_test.go` set a `User-Agent: OwnCordClient/1.0`
  literal that appears nowhere under `Client/`; `ws_integration_test.go:289`
  and `sanitize_content_fuzz_test.go:46` are comments. The real crossing is
  one the register never named: `Server/updater/updater_test.go:630` does
  `os.ReadFile` on `Client/src-tauri/tauri.conf.json`.

The obvious fixes are both wrong. Moving the invariant "to the owning server
test" cannot work: `Server/go.mod` carries no JavaScript engine (no goja,
otto, v8go, quickjs, rogchap, duktape), so a Go port could only assert at the
text level like `admin/perm_grid_test.go` does — and that is not a
substitute. Flipping the guard at `admin/static/index.html:1182` to
`targetIsTouchedRole=false` reintroduces OC-0154 in full while leaving every
greppable identifier intact, so a text-level test passes on a broken file.
Relocating it to the e2e admin journey is worse: that job is
`continue-on-error: true` and deliberately unpinned ("requiring it is
theatre" — `docs/plans/b0-dev-branch-protection.sh`), so it would convert a
blocking, pinned gate into one that is green regardless. And the journey does
not cover the invariant today: `grep -Eic "perm|access|role|override|matrix"`
over its 142 lines returns 0, so the "if e2e already covers it, delete"
branch never fires.

Done — one tier, applied to the whole set, defined by artifact coupling and
placed by runtime capability:
- New `Client/tests/contract/`, holding
  `server-admin-static-channel-perms.test.ts`. Same directory depth, so
  `../../../Server/...` still resolves; the body is byte-identical apart from
  a header naming the owner and the runner.
- `Server/updater/tauri_key_contract_test.go` splits the one cross-component
  Go test out of `updater_test.go` verbatim, same `package updater`. It stays
  in Go — placement follows capability, and Go parses JSON fine — so only the
  file name has to declare the crossing. Without this the item would have
  been "moved one file and declared the class closed".
- `npm run test:contract`, and the tier, the membership rule and a
  blocking/non-blocking table in `docs/contributing.md#testing`, which
  previously described no tiers at all.
- `Client/CLAUDE.md`'s tier list was missing `tests/e2e/admin` and
  `tests/e2e/native` before this; it now lists all seven and states the rule.
  `Server/CLAUDE.md` records why the SPA's execution-level invariant is
  locked from the client tree, so nobody "fixes" it into a regex.
- Ledger `OC-0154.fix.test` re-pointed and `FINDINGS.md` re-rendered;
  `.claude/workflows/bughunt.js` — the workflow that produced OC-0154 — no
  longer describes the TS test surface as `tests/unit/*.test.ts` only.
- Three stale cross-stack pointers of exactly the class this item is about:
  `tests/e2e/helpers.ts:348,351` and `tests/unit/types.test.ts:13` named
  `docs/brain/06-Specs/PROTOCOL.md`, which does not exist (`docs/brain/` is a
  gitignored path); all now name `docs/protocol.md`.

15 files, 125 insertions, 33 deletions. No CI job, workflow, vitest,
tsconfig, eslint, knip or stryker change, and no new pinned check —
`ci.yml`'s `npx vitest run --coverage` has no path filter and
`vitest.config.ts` includes `tests/**/*.test.ts`, so enforcement after the
move is bit-identical to enforcement before it. That is deliberate: `dev`
pins 11 contexts and a 12th is a branch-protection API write, not something a
PR can do, so any new job would be advisory until someone separately changed
repository settings — strictly less protection than today.

Verified: both directions, and the assertion was not weakened. Flipping
`admin/static/index.html:1182` to `const targetIsTouchedRole=false;` makes
the moved test fail (`AssertionError: expected 'DELETE' not to be 'DELETE'`);
`git checkout` of that file makes it pass again — so the invariant survived
the move intact rather than becoming a test that passes anywhere. The split
Go test's cross-boundary read is live too: with
`Client/src-tauri/tauri.conf.json` moved away, `go test ./updater/` fails
with `ReadFile(../../Client/src-tauri/tauri.conf.json): no such file or
directory` from `tauri_key_contract_test.go:20`, and passes once restored.
The full client suite is 192 files / 5257 tests passing, identical to the
count before the move; `npm run typecheck` passes, which proves
`tests/contract/` is inside the tsconfig graph and that `tests/types/jsdom.d.ts`
still resolves the moved test's `import { JSDOM }`. `npm run lint`,
`npx prettier --check .`, `go vet ./...` and `go test ./updater/` all pass.
`git grep "tests/unit/admin-static-channel-perms"` finds no survivor outside
the B1 plan itself.

Not included: nothing was deleted, because no e2e sibling covers OC-0154.
`Client/tests/types/jsdom.d.ts` was neither moved nor deleted — it is still
the only type source for the moved test's `jsdom` import. `capabilities-scope.test.ts`
and `tauri-conf-webview2-args.test.ts` read `src-tauri/` and stay in
`tests/unit`: `src-tauri` is inside the `Client` component, so they are not
contract tests, and the rule earns that rather than hand-waving it — moving
them would have forced repoints of ledger entry OC-0089 and
`docs/security.md:64` for no gain. Each gained a one-line header saying why.
`Server/admin/perm_grid_test.go` and `emoji_section_test.go` read their own
package's embedded asset and are unchanged; they are the text-level
complement to the execution-level test, not duplicates. No JS engine was
added to `go.mod`, no npm root was created under `Server/`, and no root-level
`tests/` tier was created — there is no runner for one and no way to make it
blocking from a PR. Separately noticed and NOT fixed here:
`docs/contributing.md:221` still says "All ten required checks" while
`docs/plans/b0-dev-branch-protection.sh` pins eleven since B1-3 added
`Repository Hygiene`, and `docs/plans/hp-0-scorecard-2026-08-25.md:109` is
stale the same way — that is the branch-protection item's to fix, not this
one's, and one register item per commit.

Refs RL-11, L-11
2026-08-26 20:13:26 +00:00

105 lines
3.9 KiB
TypeScript

// Regression guard for the Tauri HTTP capability scope.
//
// Capabilities are enforced by the Rust/Tauri ACL at compile time, so TS
// cannot exercise them. What TS *can* do is lock the shape of the grant so a
// widening (or a re-added inert scope) has to be deliberate. See
// docs/plans/tauri-capability-narrowing.md for why only `http:allow-fetch`
// carries a scope: tauri-plugin-http validates the URL once, in the `fetch`
// command — `fetch_send`/`fetch_read_body` take an already-validated
// ResourceId and never consult a scope.
import { describe, expect, it } from "vitest";
// Asserts src-tauri/capabilities/default.json, which is inside the Client
// component — not a cross-component contract test. See
// docs/contributing.md#testing.
import capabilityJson from "../../src-tauri/capabilities/default.json";
interface ScopeEntry {
readonly url?: string;
readonly path?: string;
}
interface ScopedPermission {
readonly identifier: string;
readonly allow?: readonly ScopeEntry[];
readonly deny?: readonly ScopeEntry[];
}
type Permission = string | ScopedPermission;
const permissions = capabilityJson.permissions as readonly Permission[];
function find(identifier: string): Permission {
const entry = permissions.find((p) =>
typeof p === "string" ? p === identifier : p.identifier === identifier,
);
expect(entry, `${identifier} missing from default capability`).toBeDefined();
return entry as Permission;
}
function urls(entries: readonly ScopeEntry[] | undefined): string[] {
return (entries ?? []).map((e) => e.url ?? "");
}
describe("Tauri default capability — HTTP scope", () => {
it("http:allow-fetch allows exactly the https wildcard plus loopback http", () => {
const fetchPerm = find("http:allow-fetch") as ScopedPermission;
expect(urls(fetchPerm.allow).sort()).toEqual(
["http://127.0.0.1:*", "https://*", "https://*:*"].sort(),
);
});
it("http:allow-fetch denies https loopback literals", () => {
const fetchPerm = find("http:allow-fetch") as ScopedPermission;
// All legitimate server traffic reaches loopback over http (the Rust TOFU
// proxy). An https loopback fetch can only be an attempt to reach some
// other local service, so deny it — deny wins over allow in Tauri's scope.
expect(urls(fetchPerm.deny).sort()).toEqual(
[
"https://127.0.0.1",
"https://127.0.0.1:*",
"https://localhost",
"https://localhost:*",
].sort(),
);
});
it.each(["http:allow-fetch-send", "http:allow-fetch-read-body"])(
"%s is a bare identifier (a scope there would be inert)",
(identifier) => {
expect(find(identifier)).toBe(identifier);
},
);
it("no permission grants a plaintext-http or any-scheme wildcard", () => {
const allUrls = permissions.flatMap((p) =>
typeof p === "string" ? [] : [...urls(p.allow), ...urls(p.deny)],
);
for (const url of allUrls) {
expect(url.startsWith("http://") && !url.startsWith("http://127.0.0.1")).toBe(false);
expect(url).not.toMatch(/^\*|^[a-z]*:\/\/\*\.?\*/);
}
});
it("grants core:window:allow-request-user-attention (the Flash Taskbar notification setting needs it)", () => {
// core:window:default's implicit permission set is getters only — no
// request-user-attention — so without this explicit grant, every
// win.requestUserAttention() call is ACL-rejected and the default-on
// "Flash Taskbar" setting silently does nothing.
expect(find("core:window:allow-request-user-attention")).toBe(
"core:window:allow-request-user-attention",
);
});
it("filesystem grants stay under $APPDATA/$APPLOG", () => {
const fsPaths = permissions.flatMap((p) =>
typeof p !== "string" && p.identifier.startsWith("fs:")
? (p.allow ?? []).map((e) => e.path ?? "")
: [],
);
expect(fsPaths.length).toBeGreaterThan(0);
for (const path of fsPaths) {
expect(path).toMatch(/^\$APP(DATA|LOG)\//);
}
});
});