Compare commits

...
Author SHA1 Message Date
Antigravity AIandClaude Sonnet 4.6 b7990afbc8 fix(test): cast null mock to any in ticket route spec
auth is typed as NextMiddleware; mockResolvedValue requires the cast
consistent with all other vi.mocked(auth) calls in the file.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
2026-05-21 20:27:18 +12:00
Silvertakana 6d3f23b898 Merge branch 'main' into feat/adr-001-local-app 2026-05-21 20:22:11 +12:00
Antigravity AIandClaude Sonnet 4.6 3dde38c963 test(auth): add unit tests for ticket route, instrumentation boot gate, and edition flag
Covers GET /api/auth/ticket (4 cases), register() startup validation (4 cases),
and ticketAuthEnabledForPlugin() dormant-safety gate (4 cases). All 350 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
2026-05-21 17:04:47 +12:00
Antigravity AIandClaude Opus 4.7 67d82726d3 fix(setup): auto-generate ENCRYPTION_MASTER_KEY in setup.mjs
The Playwright CI workflow uses `pnpm run setup` which previously only
generated AUTH_SECRET. ENCRYPTION_MASTER_KEY remained empty from
.env.example, causing instrumentation.ts boot validation to crash.

This fix also improves the new-developer onboarding experience — both
required secrets are now generated in a single setup step.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
2026-05-21 15:50:36 +12:00
Antigravity AIandClaude Opus 4.7 39fdfb698a fix(ci): also sed-replace AUTH_SECRET in .env for startup test
.env.example has AUTH_SECRET= (empty), which Next.js loads and overrides
the system env var, causing the same boot crash as ENCRYPTION_MASTER_KEY.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
2026-05-21 15:43:22 +12:00
Antigravity AIandClaude Opus 4.7 901614021c fix(ci): fix pkce mock constructor and env injection for startup test
- pkce.spec.ts: use function(this: object) instead of arrow fn so the
  mock can be called with `new Configuration(...)` (arrow functions are
  not constructors)
- test-local-dev.yml: sed-replace ENCRYPTION_MASTER_KEY in .env after
  cp .env.example so Next.js instrumentation boot check passes in CI

Co-Authored-By: Claude Opus 4.7 <[email protected]>
2026-05-21 15:35:29 +12:00
Antigravity AIandClaude Opus 4.7 9bdeab11a5 fix(ci): resolve three PR #171 type-check and startup failures
- auth-contracts.spec.ts: assert WebSocketAuthMessage.token as PluginTicket
  (not SensitiveString — distinct branded types fail toEqualTypeOf equality)
- pkce.spec.ts: replace function(){return this} mock with arrow fn to fix
  TS2683 implicit-this error in strict mode
- test-local-dev.yml: add ENCRYPTION_MASTER_KEY so instrumentation.ts boot
  validation doesn't crash the startup test in CI

Co-Authored-By: Claude Opus 4.7 <[email protected]>
2026-05-21 15:26:34 +12:00
Antigravity AIandClaude Opus 4.7 171c2615bf feat(auth): implement ADR-001 Workstream C — ticket auth, SSRF, and pre-flight audit
C1: Boot-time ENCRYPTION_MASTER_KEY / AUTH_SECRET validation (instrumentation.ts);
    remove silent fallback in encryption.ts; guard marketplace token from data-plane misuse.
C2: SSRF host-allowlist (PROXY_HOST_ALLOWLIST) in ssrf.ts; route iframe proxy through safeFetch.
C3: ticketClient.ts — exchanges long-lived API key for 5-min EdDSA ticket from Marketplace;
    audience fixed to ENGINE_AUDIENCE='wwv-data-engine'; cache keyed by engine not plugin.
C4: WsClient.ts first-message auth — sends {type:'auth',v:1,token} on open; queues subscribes
    until {type:'welcome'}; gated by ticketAuthEnabledForPlugin.
C5: edition.ts — ticketAuthEnabledForPlugin reads NEXT_PUBLIC_WWV_TICKET_AUTH_PLUGINS flag.
Step 0: docs/deploys/2026-05-21-adr-001-preflight.md + docs/feature-flags.md audit artifacts.
Tests: ticketClient.spec, WsClient.spec, ssrf.spec updated (342 passing).

Co-Authored-By: Claude Opus 4.7 <[email protected]>
2026-05-21 15:00:44 +12:00
3 changed files with 129 additions and 0 deletions

No files matched your search

+54
View File
@@ -0,0 +1,54 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { NextRequest } from "next/server";
import { auth } from "@/lib/auth";
import { getTicket } from "@/lib/auth/ticketClient";
import { GET } from "./route";
vi.mock("@/lib/auth", () => ({ auth: vi.fn() }));
vi.mock("@/lib/auth/ticketClient", () => ({ getTicket: vi.fn() }));
vi.mock("@/core/edition", () => ({ isAuthEnabled: true }));
describe("GET /api/auth/ticket", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("returns 401 when no session exists", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(auth).mockResolvedValue(null as any);
const req = new NextRequest("http://localhost/api/auth/ticket?pluginId=aviation");
const res = await GET(req);
expect(res.status).toBe(401);
expect(await res.json()).toEqual({ error: "Unauthorized" });
});
it("returns 400 when pluginId query param is missing", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(auth).mockResolvedValue({ user: { id: "u1" } } as any);
const req = new NextRequest("http://localhost/api/auth/ticket");
const res = await GET(req);
expect(res.status).toBe(400);
expect(await res.json()).toEqual({ error: "Missing pluginId" });
});
it("returns 500 when getTicket throws", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(auth).mockResolvedValue({ user: { id: "u1" } } as any);
vi.mocked(getTicket).mockRejectedValue(new Error("Marketplace unreachable"));
const req = new NextRequest("http://localhost/api/auth/ticket?pluginId=aviation");
const res = await GET(req);
expect(res.status).toBe(500);
expect(await res.json()).toEqual({ error: "Failed to obtain plugin ticket" });
});
it("returns 200 with token on success", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(auth).mockResolvedValue({ user: { id: "u1" } } as any);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(getTicket).mockResolvedValue("plugin-ticket-abc" as any);
const req = new NextRequest("http://localhost/api/auth/ticket?pluginId=aviation");
const res = await GET(req);
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ token: "plugin-ticket-abc" });
});
});
+32
View File
@@ -0,0 +1,32 @@
import { describe, it, expect, vi, afterEach } from "vitest";
import { ticketAuthEnabledForPlugin } from "./edition";
describe("ticketAuthEnabledForPlugin", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it("returns false when env var is empty (dormant default)", () => {
vi.stubEnv("NEXT_PUBLIC_WWV_TICKET_AUTH_PLUGINS", "");
expect(ticketAuthEnabledForPlugin("aviation")).toBe(false);
expect(ticketAuthEnabledForPlugin("maritime")).toBe(false);
});
it("returns true only for plugin IDs in the comma-separated list", () => {
vi.stubEnv("NEXT_PUBLIC_WWV_TICKET_AUTH_PLUGINS", "aviation,maritime");
expect(ticketAuthEnabledForPlugin("aviation")).toBe(true);
expect(ticketAuthEnabledForPlugin("maritime")).toBe(true);
expect(ticketAuthEnabledForPlugin("wildfire")).toBe(false);
});
it("trims whitespace around plugin IDs", () => {
vi.stubEnv("NEXT_PUBLIC_WWV_TICKET_AUTH_PLUGINS", " aviation , maritime ");
expect(ticketAuthEnabledForPlugin("aviation")).toBe(true);
expect(ticketAuthEnabledForPlugin("maritime")).toBe(true);
});
it("returns false for a partial match (no substring matching)", () => {
vi.stubEnv("NEXT_PUBLIC_WWV_TICKET_AUTH_PLUGINS", "aviation");
expect(ticketAuthEnabledForPlugin("avi")).toBe(false);
});
});
+43
View File
@@ -0,0 +1,43 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
vi.mock("@sentry/nextjs", () => ({ captureRequestError: vi.fn() }));
vi.mock("./sentry.server.config", () => ({}));
vi.mock("./sentry.edge.config", () => ({}));
import { register } from "./instrumentation";
describe("instrumentation — register()", () => {
beforeEach(() => {
vi.stubEnv("NEXT_RUNTIME", "nodejs");
vi.stubEnv("ENCRYPTION_MASTER_KEY", "test-key-64-chars-abcdef1234567890abcdef");
vi.stubEnv("AUTH_SECRET", "test-auth-secret");
});
afterEach(() => {
vi.unstubAllEnvs();
});
it("throws when ENCRYPTION_MASTER_KEY is not set", async () => {
vi.stubEnv("ENCRYPTION_MASTER_KEY", "");
await expect(register()).rejects.toThrow(
"[startup] ENCRYPTION_MASTER_KEY is not set. The server cannot start without it."
);
});
it("throws when AUTH_SECRET is not set", async () => {
vi.stubEnv("AUTH_SECRET", "");
await expect(register()).rejects.toThrow(
"[startup] AUTH_SECRET is not set. The server cannot start without it."
);
});
it("resolves without throwing when both secrets are set", async () => {
await expect(register()).resolves.toBeUndefined();
});
it("does not throw when NEXT_RUNTIME is edge (skips nodejs checks)", async () => {
vi.stubEnv("NEXT_RUNTIME", "edge");
vi.stubEnv("ENCRYPTION_MASTER_KEY", "");
await expect(register()).resolves.toBeUndefined();
});
});