문제 V2에서 옮긴 두 조립 검사가 한 사례씩만 테스트된다. 쿠키 쓰기 CSRF 검사는 POST와 x-xsrf-token 한 쌍만 본다. 그래서 `!isRead`를 `method === "POST"`로 좁히거나, CSRF 헤더 이름 대신 "필수 헤더가 하나라도 있으면"으로 바꾸거나, x-csrf-token 허용을 지워도 테스트가 통과한다. realtime snapshot 바인딩(GET + SAFE·IDEMPOTENT)은 거절 사례가 없어 method 조건이나 retrySemantics 조건을 지워도 통과한다. 고른 방법 - rest-profile-contract: POST·PUT·PATCH·DELETE마다 CSRF 없는 쿠키 프로필과 x-tenant-context만 요구하는 쿠키 프로필을 거절하고, x-csrf-token과 x-xsrf-token을 각각 받는지 본다. GET·HEAD 쿠키 읽기는 CSRF 없이 된다. - realtime-stream-registry: 6 method × 4 retrySemantics 표를 CURSOR와 SNAPSHOT_ONLY 두 복구 모드로 돈다. 받는 조합은 GET×SAFE, GET×IDEMPOTENT 뿐이다. 행은 조립한 descriptor를 projectApiOperationRows로 뽑은 것에서 method와 retrySemantics만 바꾼다. 투영 칸 이름이 바뀌어도 이 표가 같이 깨진다. FE-REG-API 행이 없는 id도 거절한다. 버린 대안 - 손으로 쓴 행(fixture.ts)을 그대로 쓰기: 투영과 realtime 검사가 따로 바뀌는 것을 못 잡는다. 위험·한계 - 검사 코드는 바꾸지 않았다. 테스트만 더했다. - resumable upload 제어 요청의 쿠키 POST는 descriptor 밖이라 이 검사 범위가 아니다(영역 01). 검증 - 변이 7종을 하나씩 넣고 두 테스트 파일을 돌렸다. 모두 실패한다: M2(POST만), M3(아무 필수 헤더), H19(x-csrf-token 허용 삭제), M9(method 조건 삭제), M9b(method·retry 조건 삭제), S2(retry 조건 삭제), H21-PUT(GET 또는 PUT 허용). - 변이 없이 두 파일 47건 통과. check:types, eslint 통과. Refs: docs/reviews/2026-11-03-maturity-review/07-contracts.md 07-02 Refs: docs/reviews/2026-11-03-maturity-review/10-tests-fast-layers.md 10-03, 10-04 Refs: docs/reviews/2026-11-03-maturity-review/04-adapters-realtime.md 04-03
197 lines
6.7 KiB
TypeScript
197 lines
6.7 KiB
TypeScript
import { readdir, readFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
composeContractContributions,
|
|
ContractContributionError,
|
|
} from "../../src/contracts/external-contract-runtime.ts";
|
|
import {
|
|
INSTALLED_REST_AUTH_PROFILES,
|
|
installRestAuthProfileRegistry,
|
|
REST_AUTH_PROFILES,
|
|
} from "../../src/contracts/rest-profiles.ts";
|
|
import { TEST_CONTRACT_CONTRIBUTION } from "../helpers/external-contract-fixture.ts";
|
|
|
|
describe("one installed REST auth profile registry", () => {
|
|
it("refuses a raw profile record that is incoherent", () => {
|
|
expect(() =>
|
|
installRestAuthProfileRegistry({
|
|
EXTERNAL_BEARER: {
|
|
...REST_AUTH_PROFILES.EXTERNAL_BEARER,
|
|
credentials: "same-origin" as const,
|
|
},
|
|
}),
|
|
).toThrow(TypeError);
|
|
});
|
|
|
|
it("does not answer for an inherited profile id", () => {
|
|
expect(INSTALLED_REST_AUTH_PROFILES.has("toString")).toBe(false);
|
|
expect(INSTALLED_REST_AUTH_PROFILES.get("toString")).toBeUndefined();
|
|
});
|
|
|
|
it("composes contributions against the registry it is given", () => {
|
|
const registry = installRestAuthProfileRegistry({
|
|
...REST_AUTH_PROFILES,
|
|
CUSTOM_BEARER: {
|
|
...REST_AUTH_PROFILES.EXTERNAL_BEARER,
|
|
authProfileId: "CUSTOM_BEARER",
|
|
},
|
|
});
|
|
const row = TEST_CONTRACT_CONTRIBUTION.http[0]!;
|
|
const contribution = {
|
|
...TEST_CONTRACT_CONTRIBUTION,
|
|
http: [
|
|
{ ...row, frontend: { ...row.frontend, authProfileId: "CUSTOM_BEARER" } },
|
|
],
|
|
};
|
|
expect(() => composeContractContributions([contribution])).toThrow(
|
|
ContractContributionError,
|
|
);
|
|
expect(
|
|
composeContractContributions([contribution], registry).httpByOperationId
|
|
.size,
|
|
).toBe(1);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* A cookie-authenticated write must carry a CSRF proof header: the browser
|
|
* attaches the cookie to any site's request, so the header is what tells the
|
|
* server the page itself sent it. The V2 client refused such a binding; the
|
|
* installed HTTP contract has to refuse it too.
|
|
*/
|
|
describe("cookie profiles on writes", () => {
|
|
const cookie = (
|
|
authProfileId: string,
|
|
headers: readonly ("x-csrf-token" | "x-xsrf-token" | "x-tenant-context")[],
|
|
) => ({
|
|
authProfileId,
|
|
transport: "SAME_ORIGIN_COOKIE" as const,
|
|
credentials: "include" as const,
|
|
allowedCredentialHeaders: [...headers],
|
|
requiredCredentialHeaders: [...headers],
|
|
});
|
|
const registry = installRestAuthProfileRegistry({
|
|
...REST_AUTH_PROFILES,
|
|
COOKIE_READ: cookie("COOKIE_READ", []),
|
|
COOKIE_WRITE: cookie("COOKIE_WRITE", ["x-xsrf-token"]),
|
|
COOKIE_CSRF: cookie("COOKIE_CSRF", ["x-csrf-token"]),
|
|
COOKIE_TENANT: cookie("COOKIE_TENANT", ["x-tenant-context"]),
|
|
});
|
|
const withProfile = (
|
|
index: number,
|
|
authProfileId: string,
|
|
method?: "GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE",
|
|
) => {
|
|
const row = TEST_CONTRACT_CONTRIBUTION.http[index]!;
|
|
return {
|
|
...TEST_CONTRACT_CONTRIBUTION,
|
|
http: [
|
|
{
|
|
contract: method ? { ...row.contract, method } : row.contract,
|
|
frontend: { ...row.frontend, authProfileId },
|
|
},
|
|
],
|
|
};
|
|
};
|
|
// TEST_CONTRACT_CONTRIBUTION.http: [list (GET), detail (GET), create (POST)]
|
|
const compose = (
|
|
index: number,
|
|
authProfileId: string,
|
|
method?: "GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE",
|
|
) =>
|
|
composeContractContributions(
|
|
[withProfile(index, authProfileId, method)],
|
|
registry,
|
|
).httpByOperationId.size;
|
|
|
|
// 1103/07-02, 10-03. Every write method, not POST alone, and only the two
|
|
// CSRF header names count: another required header is not a CSRF proof.
|
|
it.each(["POST", "PUT", "PATCH", "DELETE"] as const)(
|
|
"refuses a %s whose cookie profile requires no CSRF header",
|
|
(method) => {
|
|
for (const authProfileId of ["COOKIE_READ", "COOKIE_TENANT"]) {
|
|
expect(() => compose(2, authProfileId, method)).toThrow(
|
|
expect.objectContaining({
|
|
reason: `test-feature/TEST_CREATE_ENTITY: ${method} with cookie profile ${authProfileId} must require x-csrf-token or x-xsrf-token`,
|
|
}),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
|
|
it.each(["POST", "PUT", "PATCH", "DELETE"] as const)(
|
|
"accepts a %s that requires x-csrf-token or x-xsrf-token",
|
|
(method) => {
|
|
expect(compose(2, "COOKIE_CSRF", method)).toBe(1);
|
|
expect(compose(2, "COOKIE_WRITE", method)).toBe(1);
|
|
},
|
|
);
|
|
|
|
it.each(["GET", "HEAD"] as const)(
|
|
"accepts a cookie %s without a CSRF header",
|
|
(method) => {
|
|
expect(compose(0, "COOKIE_READ", method)).toBe(1);
|
|
},
|
|
);
|
|
|
|
it("refuses a write whose cookie profile requires no CSRF header", () => {
|
|
expect(() =>
|
|
composeContractContributions([withProfile(2, "COOKIE_READ")], registry),
|
|
).toThrow(
|
|
expect.objectContaining({
|
|
reason:
|
|
"test-feature/TEST_CREATE_ENTITY: POST with cookie profile COOKIE_READ must require x-csrf-token or x-xsrf-token",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("accepts a write that requires a CSRF header, and a cookie read without one", () => {
|
|
expect(
|
|
composeContractContributions([withProfile(2, "COOKIE_WRITE")], registry)
|
|
.httpByOperationId.size,
|
|
).toBe(1);
|
|
expect(
|
|
composeContractContributions([withProfile(0, "COOKIE_READ")], registry)
|
|
.httpByOperationId.size,
|
|
).toBe(1);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 1101/16-03. The only bearer profile is the platform's, not the sample's:
|
|
* `EXTERNAL_BEARER`. The old name stays for one release as a deprecated alias
|
|
* that resolves to the same transport and headers.
|
|
*/
|
|
describe("EXTERNAL_BEARER and its deprecated alias (16-03)", () => {
|
|
it("resolves the alias exactly like EXTERNAL_BEARER but for its id", () => {
|
|
expect(INSTALLED_REST_AUTH_PROFILES.get("EXTERNAL_BEARER")).toMatchObject({
|
|
transport: "BEARER_HEADER",
|
|
requiredCredentialHeaders: ["authorization"],
|
|
});
|
|
expect({
|
|
...INSTALLED_REST_AUTH_PROFILES.get("REFERENCE_EXTERNAL_BEARER"),
|
|
authProfileId: "EXTERNAL_BEARER",
|
|
}).toEqual(INSTALLED_REST_AUTH_PROFILES.get("EXTERNAL_BEARER"));
|
|
});
|
|
|
|
it("keeps the old name in src only on the alias line", async () => {
|
|
const files = (await readdir("src", { recursive: true }))
|
|
.filter((file) => /\.tsx?$/u.test(file))
|
|
.map((file) => path.join("src", file));
|
|
const lines: string[] = [];
|
|
for (const file of files) {
|
|
for (const line of (await readFile(file, "utf8")).split("\n")) {
|
|
if (line.includes("REFERENCE_EXTERNAL_BEARER")) {
|
|
lines.push(`${file}: ${line.trim()}`);
|
|
}
|
|
}
|
|
}
|
|
expect(lines).toEqual([
|
|
'src/contracts/rest-profiles.ts: REFERENCE_EXTERNAL_BEARER: Object.freeze({ ...EXTERNAL_BEARER, authProfileId: "REFERENCE_EXTERNAL_BEARER" }),',
|
|
]);
|
|
});
|
|
});
|