diff --git a/src/contracts/api-operations.ts b/src/contracts/api-operations.ts index 6ceebc18..692021e6 100644 --- a/src/contracts/api-operations.ts +++ b/src/contracts/api-operations.ts @@ -207,7 +207,9 @@ export function getApiOperation( operationId: string, operations: Readonly> = API_OPERATIONS, ): ApiOperation { - const selected = operations[operationId]; + const selected = Object.hasOwn(operations, operationId) + ? operations[operationId] + : undefined; if (!selected) { throw new Error(`Unregistered API operation: ${operationId}`); } diff --git a/src/contracts/boundary-mapper.ts b/src/contracts/boundary-mapper.ts index f53f91f5..5b21f7b3 100644 --- a/src/contracts/boundary-mapper.ts +++ b/src/contracts/boundary-mapper.ts @@ -75,7 +75,9 @@ export function mapWithBoundaryRegistry( input: unknown, registry: Readonly>, ): MappingResult { - const mapper = registry[mapperId]; + const mapper = Object.hasOwn(registry, mapperId) + ? registry[mapperId] + : undefined; if (!mapper) return mappingFailure("MAPPING_INVARIANT_REJECTED"); try { return mapper.map(input); diff --git a/src/contracts/offline-command.ts b/src/contracts/offline-command.ts index 64441297..a6e382ed 100644 --- a/src/contracts/offline-command.ts +++ b/src/contracts/offline-command.ts @@ -101,6 +101,8 @@ export function isAllowedOfflineTransition( from: OfflineCommandState, to: OfflineCommandState, ): boolean { + // Q-6.7-11. A state that is not listed has no allowed transition. + if (!Object.hasOwn(ALLOWED_TRANSITIONS, from)) return false; const allowed: readonly OfflineCommandState[] = ALLOWED_TRANSITIONS[from]; return allowed.includes(to); } diff --git a/src/contracts/schema-registry.ts b/src/contracts/schema-registry.ts index 4988c31e..ace442c1 100644 --- a/src/contracts/schema-registry.ts +++ b/src/contracts/schema-registry.ts @@ -79,7 +79,9 @@ export function validateWithRuntimeSchemaRegistry( value: unknown, registry: Readonly>, ): RuntimeSchemaResult { - const codec = registry[schemaId]; + const codec = Object.hasOwn(registry, schemaId) + ? registry[schemaId] + : undefined; if (!codec) { return Object.freeze({ success: false, diff --git a/src/contracts/storage-keys.ts b/src/contracts/storage-keys.ts index fb71082e..0537041c 100644 --- a/src/contracts/storage-keys.ts +++ b/src/contracts/storage-keys.ts @@ -172,7 +172,9 @@ export function buildPhysicalKey( export function getStorageDefinition(logicalName: string): StorageDefinition { const registry: Readonly> = STORAGE_REGISTRY; - const definition = registry[logicalName]; + const definition = Object.hasOwn(registry, logicalName) + ? registry[logicalName] + : undefined; if (!definition) throw new Error(`Unregistered storage key: ${logicalName}`); if (definition.classification === "sensitive-forbidden") { throw new Error(`Forbidden storage key: ${logicalName}`); diff --git a/tests/unit/contract-registry-lookup.test.ts b/tests/unit/contract-registry-lookup.test.ts new file mode 100644 index 00000000..5762be27 --- /dev/null +++ b/tests/unit/contract-registry-lookup.test.ts @@ -0,0 +1,67 @@ +import { describe, expect, it } from "vitest"; + +import { getApiOperation } from "../../src/contracts/api-operations.ts"; +import { mapWithBoundaryRegistry } from "../../src/contracts/boundary-mapper.ts"; +import { + isAllowedOfflineTransition, + type OfflineCommandState, +} from "../../src/contracts/offline-command.ts"; +import { validateWithRuntimeSchemaRegistry } from "../../src/contracts/schema-registry.ts"; +import { getStorageDefinition } from "../../src/contracts/storage-keys.ts"; + +/** + * Q-6.7-11. A registry lookup answers only for its own keys. `registry[id]` + * walks the prototype chain, so `constructor` or `__proto__` resolved to + * `Object` / `Object.prototype` instead of "not registered". + */ +const INHERITED = ["constructor", "__proto__", "toString", "hasOwnProperty"]; + +describe("registry lookups ignore inherited keys", () => { + it("getApiOperation", () => { + for (const id of INHERITED) { + expect(() => getApiOperation(id), id).toThrow("Unregistered API operation"); + } + }); + + it("getStorageDefinition", () => { + for (const id of INHERITED) { + expect(() => getStorageDefinition(id), id).toThrow( + "Unregistered storage key", + ); + } + }); + + it("validateWithRuntimeSchemaRegistry", () => { + for (const id of INHERITED) { + expect(validateWithRuntimeSchemaRegistry(id, {}, {}), id).toEqual({ + success: false, + issues: [{ path: "", code: "SCHEMA_NOT_REGISTERED" }], + }); + } + }); + + it("mapWithBoundaryRegistry", () => { + for (const id of INHERITED) { + expect(mapWithBoundaryRegistry(id, {}, {}), id).toEqual({ + ok: false, + code: "MAPPING_INVARIANT_REJECTED", + }); + } + }); +}); + +describe("offline command transitions", () => { + it("answers false for a state that is not listed", () => { + for (const from of ["UNKNOWN", ...INHERITED]) { + expect( + isAllowedOfflineTransition(from as OfflineCommandState, "PENDING"), + from, + ).toBe(false); + } + }); + + it("keeps the listed transitions", () => { + expect(isAllowedOfflineTransition("PENDING", "LEASED")).toBe(true); + expect(isAllowedOfflineTransition("ACKED", "PENDING")).toBe(false); + }); +});