fix(contracts): look registries up by own key only (Q-6.7-11)
`registry[id]` walks the prototype chain: getApiOperation("constructor")
returned the Object function, getStorageDefinition("__proto__")
returned Object.prototype, validateWithRuntimeSchemaRegistry reported
an inherited id as SCHEMA_EXECUTION_FAILED instead of
SCHEMA_NOT_REGISTERED, and isAllowedOfflineTransition threw a TypeError
for a state that is not listed.
getApiOperation, getStorageDefinition, validateWithRuntimeSchemaRegistry
and mapWithBoundaryRegistry now check Object.hasOwn before reading, and
isAllowedOfflineTransition returns false for an unlisted state.
Verified: new tests/unit/contract-registry-lookup.test.ts (4 of 6 RED
before; the boundary-mapper case already failed closed and is kept as
characterization); gates green except the known removal-fixture test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
375b563961
commit
3cc5039686
@@ -207,7 +207,9 @@ export function getApiOperation(
|
||||
operationId: string,
|
||||
operations: Readonly<Record<string, ApiOperation>> = 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}`);
|
||||
}
|
||||
|
||||
@@ -75,7 +75,9 @@ export function mapWithBoundaryRegistry(
|
||||
input: unknown,
|
||||
registry: Readonly<Record<string, InstalledBoundaryMapper>>,
|
||||
): MappingResult<unknown> {
|
||||
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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -79,7 +79,9 @@ export function validateWithRuntimeSchemaRegistry(
|
||||
value: unknown,
|
||||
registry: Readonly<Record<string, RuntimeSchemaCodec>>,
|
||||
): RuntimeSchemaResult {
|
||||
const codec = registry[schemaId];
|
||||
const codec = Object.hasOwn(registry, schemaId)
|
||||
? registry[schemaId]
|
||||
: undefined;
|
||||
if (!codec) {
|
||||
return Object.freeze({
|
||||
success: false,
|
||||
|
||||
@@ -172,7 +172,9 @@ export function buildPhysicalKey(
|
||||
|
||||
export function getStorageDefinition(logicalName: string): StorageDefinition {
|
||||
const registry: Readonly<Record<string, StorageDefinition>> = 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}`);
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user