Files
clean-architecture-frontend…/tests/unit/auth-session-adapter.test.ts
T
DongHyeonkaandClaude Opus 5.5 5e62b7e1e5 fix(auth): validate credential header values as control-free ByteStrings (R-3.4)
The shared credential admission (V2 and V3) only rejected CR/LF. A token
with NUL, a non-Latin-1 character or another control character passed,
then Fetch rejected it after V3 had marked the attempt dispatched: a query
burned its retry budget and a command ended MAYBE_APPLIED with
NETWORK_FAILURE. The external session adapter also merged header names
that differ only in case, silently keeping the last value.

- admitCredentialHeaders: the value must match [\x20-\x7E\xA0-\xFF]+ (a
  ByteString without C0/C1 controls); the size limit is now the string
  length, which is the wire byte count for a ByteString. Failure stays
  CREDENTIAL_HEADER_VALUE_INVALID, i.e. AUTH_INTEGRATION_FAILURE before
  dispatch.
- checkFinalInvariants: builds `new Headers(headers)` first; any value
  Fetch would reject fails as HEADER_VALUE_INVALID (new additive member of
  FinalInvariantFailure) with zero fetch calls.
- validateCredentialPatch: rejects case-only duplicate header names.

RED before fix: http-execution-v3-live-authority.test.ts "rejects a NUL /
non-Latin-1 credential value before dispatch" (got TRANSPORT_FAILURE, fetch
called), http-contract-bridge.test.ts "rejects a NUL/non-Latin-1/tab/DEL/C1
control value ..." and "fails before dispatch when Fetch would reject a
header value", auth-session-adapter.test.ts "rejects credential headers
that differ only in case instead of merging them". Pinned: "admits an
ordinary bearer token", CR/LF rejection, existing "Bearer ok"/"Bearer
token" integration paths.
Gates: check:architecture, check:types:app, check:types:test, lint,
test:component, test:integration, test:contract, test:platform,
test:reference-feature pass. test:unit passes except removal-fixture.test.ts
(needs generated artifacts/ release evidence absent in a fresh worktree;
unrelated).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 12:25:01 +09:00

136 lines
4.4 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import {
createAnonymousSessionAdapter,
createDemoSessionAdapter,
createExternalAuthSessionAdapter,
} from "../../src/adapters/auth/external-session-adapter.ts";
describe("external AuthSessionPort adapter", () => {
it("attaches opaque credentials without exposing a token-shaped session", async () => {
const adapter = createExternalAuthSessionAdapter({
readState: () => "authenticated",
subscribe: () => () => {},
beginSignIn: async () => {},
signOut: async () => {},
attachCredential: async () => ({
headers: { Authorization: "Bearer opaque" },
}),
recoverSession: async () => "restored",
notifyUnauthenticated: vi.fn(),
});
const patch = await adapter.credentialPatch({
origin: "https://api.test",
method: "GET",
operationId: "GET_RESOURCE",
});
expect(patch.headers.authorization).toBe("Bearer opaque");
expect(adapter.getState()).toBe("authenticated");
expect(adapter).not.toHaveProperty("accessToken");
expect(adapter).not.toHaveProperty("refreshToken");
});
it("fails invalid recovery states closed", async () => {
const adapter = createExternalAuthSessionAdapter({
readState: () => "authenticated",
subscribe: () => () => {},
beginSignIn: async () => {},
signOut: async () => {},
attachCredential: async () => ({ headers: {} }),
// @ts-expect-error Deliberately violates the external-owner contract.
recoverSession: async () => "unexpected",
notifyUnauthenticated: vi.fn(),
});
await expect(adapter.recover()).rejects.toThrow("invalid recovery state");
});
it("rejects credential patches that can alter transport-owned headers", async () => {
const adapter = createExternalAuthSessionAdapter({
readState: () => "authenticated",
subscribe: () => () => {},
beginSignIn: async () => {},
signOut: async () => {},
attachCredential: async () => ({
headers: { Host: "attacker.test" },
}),
recoverSession: async () => "restored",
notifyUnauthenticated: vi.fn(),
});
await expect(
adapter.credentialPatch({
origin: "https://api.test",
method: "GET",
operationId: "GET_RESOURCE",
}),
).rejects.toThrow("forbidden credential patch");
});
it("rejects credential headers that differ only in case instead of merging them", async () => {
const adapter = createExternalAuthSessionAdapter({
readState: () => "authenticated",
subscribe: () => () => {},
beginSignIn: async () => {},
signOut: async () => {},
attachCredential: async () => ({
headers: { Authorization: "Bearer a", authorization: "Bearer b" },
}),
recoverSession: async () => "restored",
notifyUnauthenticated: vi.fn(),
});
await expect(
adapter.credentialPatch({
origin: "https://api.test",
method: "GET",
operationId: "GET_RESOURCE",
}),
).rejects.toThrow("forbidden credential patch");
});
it("provides a safe anonymous adapter", async () => {
const adapter = createAnonymousSessionAdapter();
expect(adapter.getState()).toBe("unauthenticated");
await expect(adapter.recover()).resolves.toBe("no-session");
});
it("provides a reactive credential-free demo seam", async () => {
const adapter = createDemoSessionAdapter();
let notifications = 0;
const unsubscribe = adapter.subscribe(() => {
notifications += 1;
});
expect(adapter.getState()).toBe("unauthenticated");
await adapter.beginSignIn();
expect(adapter.getState()).toBe("authenticated");
await adapter.signOut();
expect(adapter.getState()).toBe("unauthenticated");
expect(notifications).toBe(2);
unsubscribe();
});
it("does not notify when the demo state is set to its current value", async () => {
const adapter = createDemoSessionAdapter("recovery-pending");
let notifications = 0;
adapter.subscribe(() => {
notifications += 1;
});
adapter.setState("recovery-pending");
expect(notifications).toBe(0);
adapter.setState("authenticated");
await adapter.beginSignIn();
expect(notifications).toBe(1);
adapter.onUnauthenticated();
adapter.onUnauthenticated();
await adapter.signOut();
expect(adapter.getState()).toBe("unauthenticated");
expect(notifications).toBe(2);
});
});