Files
clean-architecture-frontend…/tests/unit/api-operation-v2.test.ts
T
DongHyeonkaandClaude Opus 5.5 f26fd18fb0 fix(contracts): bind REST v2 COMMAND operations to unsafe methods (R-7.4)
validateRestOperation only required QUERY to use GET/HEAD and never
checked the semantics/replayPolicy enums. A COMMAND declared as GET
passed, and rest-profiles decides whether CSRF protection applies from the
HTTP method alone, so such a command with a cookie auth profile and csrf
NONE would be sent without CSRF protection. COMMAND now requires
POST/PUT/PATCH/DELETE, and unknown semantics/replayPolicy values are
rejected (same binding browser-rpc and the V3 composition enforce).

High-risk (CSRF) change: a new test re-validates every installed REST v2
operation from installed-feature-contracts (the reference feature's two
GET queries and one POST keyed command) and all pass; the rest-profile,
realtime/bounded-polling, legacy credential and reference-feature suites
that define REST v2 operations also pass.

RED: "REST operation v2 command method and enum rules > rejects a COMMAND
sent as GET/HEAD/OPTIONS/post" and "rejects an unknown semantics/replayPolicy
value" failed with "expected function to throw an error, but it didn't".
Gates: check:architecture, check:types:app/test/node, lint, test:component,
test:runtime-schema, test:contract pass; test:unit passes except the
environment-dependent removal-fixture.test.ts (needs generated
artifacts/release files; unrelated). tests/integration also passes.

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

114 lines
3.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
composeApiOperations,
defineRestOperation,
type RestOperationV2,
} from "../../src/contracts/api-operations.ts";
import { API_OPERATIONS } from "../../src/features/installed-feature-contracts.ts";
function operation(operationId = "GET_RESOURCE") {
return defineRestOperation({
contractVersion: 2,
protocol: "REST",
semantics: "QUERY",
method: "GET",
path: "/resources/{resourceId}",
operationId,
auth: "external-session",
timeoutMs: 5_000,
idempotency: "safe",
retry: "runtime",
replayPolicy: "SAFE",
idempotencyKeyPolicy: "NONE",
requestSource: "none",
requestSchema: "NoRequest",
responseSchema: "ResourcePayload",
mapperId: "ResourceMapper",
successStatuses: [200],
responseMediaTypes: ["application/json"],
maxResponseBytes: 32_768,
providerId: "PRIMARY_API",
authProfileId: "REFERENCE_EXTERNAL_BEARER",
csrfProfileId: "NO_CSRF_BEARER",
pathSchema: "ResourceParams",
pathParameterNames: ["resourceId"],
maxEncodedSearchBytes: 0,
owner: "test",
});
}
describe("REST operation v2 registry", () => {
it("rejects duplicate contributions before an object spread can overwrite them", () => {
const selected = operation();
expect(() =>
composeApiOperations([
{ GET_RESOURCE: selected },
{ GET_RESOURCE: selected },
]),
).toThrow("Duplicate API operation");
});
it("rejects an unsafe query/replay policy combination", () => {
expect(() =>
defineRestOperation({
...operation(),
method: "POST",
}),
).toThrow("Incoherent REST replay contract");
});
});
function command(): RestOperationV2 {
return {
...operation(),
semantics: "COMMAND",
method: "POST",
path: "/resources",
operationId: "CREATE_RESOURCE",
idempotency: "keyed",
replayPolicy: "KEYED_COMMAND",
idempotencyKeyPolicy: "REQUIRED",
requestSource: "body",
pathSchema: "NoRequest",
pathParameterNames: [],
};
}
/**
* R-7.4. CSRF protection is chosen from the HTTP method, so a COMMAND sent
* as GET/HEAD would skip it. browser-rpc and the V3 composition already bind
* commands to unsafe methods; REST V2 must too.
*/
describe("REST operation v2 command method and enum rules", () => {
it.each(["POST", "PUT", "PATCH", "DELETE"])("accepts a COMMAND sent as %s", (method) => {
expect(defineRestOperation({ ...command(), method }).method).toBe(method);
});
it.each(["GET", "HEAD", "OPTIONS", "post"])("rejects a COMMAND sent as %s", (method) => {
expect(() => defineRestOperation({ ...command(), method })).toThrow(TypeError);
expect(() =>
composeApiOperations([{ CREATE_RESOURCE: { ...command(), method } }]),
).toThrow(TypeError);
});
it.each<[string, Record<string, unknown>]>([
["semantics", { semantics: "SUBSCRIBE" }],
["replayPolicy", { replayPolicy: "ALWAYS" }],
])("rejects an unknown %s value", (_field, patch) => {
void _field;
const candidate = { ...command(), ...patch } as unknown as RestOperationV2;
expect(() => defineRestOperation(candidate)).toThrow(TypeError);
});
it("keeps every installed REST v2 operation valid", () => {
for (const installed of Object.values(API_OPERATIONS)) {
if (installed.contractVersion !== 2) continue;
expect(
() => defineRestOperation(installed as RestOperationV2),
installed.operationId,
).not.toThrow();
}
});
});