fix: harden bounded state sidecars
N-05: the conditional-validator key was a colon join over components that may themselves contain colons, so two distinct valid bindings could collide and one definition's ETag could be prepared for another. The key is now a validated, byte-bounded fixed tuple encoded with JSON.stringify. N-09: capture localStorage exactly once and compare StorageEvent.storageArea against that object identity, so a pulse from sessionStorage or any other area is rejected instead of matching on key and value alone. The pulse key is registered in the storage registry as CACHE_INVALIDATION_PULSE. N-10: race loadPage against the caller signal and re-check before observing a page, so a non-cooperative loader can neither hold loadAll forever nor have a post-abort completion accumulated into a successful result. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
b893d95b36
commit
4fe924ee0f
@@ -44,6 +44,31 @@ describe("conditional validator CAS sidecar", () => {
|
||||
expect(store.acceptNotModified(binding, 7, true)).toBe(false);
|
||||
});
|
||||
|
||||
it("keeps colon-bearing validator tuples injective", () => {
|
||||
const selectedScope = scope();
|
||||
// N-05. Both bindings are individually valid and, under a delimiter join,
|
||||
// encode to the same key.
|
||||
const first = {
|
||||
definitionId: "resource:detail",
|
||||
identityToken: "identity-token-00000001",
|
||||
representationVersion: 1,
|
||||
scope: selectedScope.snapshot,
|
||||
};
|
||||
const second = {
|
||||
definitionId: "resource",
|
||||
identityToken: "detail:identity-token-00000001",
|
||||
representationVersion: 1,
|
||||
scope: selectedScope.snapshot,
|
||||
};
|
||||
const store = createConditionalValidatorStore();
|
||||
|
||||
expect(store.install(first, '"etag-a"', 7)).toBe(true);
|
||||
expect(store.install(second, '"etag-b"', 7)).toBe(true);
|
||||
|
||||
expect(store.prepare(first, 7)).toBe('"etag-a"');
|
||||
expect(store.prepare(second, 7)).toBe('"etag-b"');
|
||||
});
|
||||
|
||||
it("rejects malformed validators and bounded-capacity overflow", () => {
|
||||
const firstScope = scope();
|
||||
const store = createConditionalValidatorStore(1);
|
||||
|
||||
@@ -112,9 +112,14 @@ class FakeStorageEventTarget implements StorageEventTargetFacade {
|
||||
this.listeners.delete(listener);
|
||||
}
|
||||
|
||||
emit(key: string | null, newValue: string | null): void {
|
||||
emit(
|
||||
key: string | null,
|
||||
newValue: string | null,
|
||||
storageArea: "EXPECTED_LOCAL_STORAGE" | "OTHER_OR_UNKNOWN" =
|
||||
"EXPECTED_LOCAL_STORAGE",
|
||||
): void {
|
||||
for (const listener of [...this.listeners]) {
|
||||
listener({ key, newValue });
|
||||
listener({ key, newValue, storageArea });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -135,6 +140,9 @@ class FakeStorageBus {
|
||||
});
|
||||
}
|
||||
|
||||
/** Simulates the event arriving from a different Storage area. */
|
||||
emitAsForeignArea = false;
|
||||
|
||||
set(
|
||||
owner: FakeStorageEventTarget,
|
||||
key: string,
|
||||
@@ -142,7 +150,15 @@ class FakeStorageBus {
|
||||
): void {
|
||||
this.values.set(key, value);
|
||||
for (const target of this.targets) {
|
||||
if (target !== owner) target.emit(key, value);
|
||||
if (target !== owner) {
|
||||
target.emit(
|
||||
key,
|
||||
value,
|
||||
this.emitAsForeignArea
|
||||
? "OTHER_OR_UNKNOWN"
|
||||
: "EXPECTED_LOCAL_STORAGE",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -487,6 +503,50 @@ describe("browser cross-context invalidation transport", () => {
|
||||
second.close();
|
||||
});
|
||||
|
||||
it("rejects storage pulses from another or unknown storage area", () => {
|
||||
const storageBus = new FakeStorageBus();
|
||||
const publisherStorage = storageBus.createEndpoint();
|
||||
const receiverStorage = storageBus.createEndpoint();
|
||||
const storageOnly = {
|
||||
createBroadcastChannel: () => {
|
||||
throw new DOMException("Denied", "SecurityError");
|
||||
},
|
||||
};
|
||||
const publisher = createBrowserCrossContextInvalidation(
|
||||
dependencies("tab-a", {
|
||||
...storageOnly,
|
||||
storage: publisherStorage.storage,
|
||||
storageEvents: publisherStorage.target,
|
||||
}),
|
||||
);
|
||||
const receiver = createBrowserCrossContextInvalidation(
|
||||
dependencies("tab-b", {
|
||||
...storageOnly,
|
||||
storage: receiverStorage.storage,
|
||||
storageEvents: receiverStorage.target,
|
||||
}),
|
||||
);
|
||||
const received = vi.fn();
|
||||
receiver.subscribe(received);
|
||||
|
||||
// N-09. sessionStorage and every other Storage area raise the same event,
|
||||
// so an identical key and value from a foreign area must be ignored.
|
||||
storageBus.emitAsForeignArea = true;
|
||||
expect(
|
||||
publisher.publish({ topic: TOPIC, topicVersion: 1 }),
|
||||
).toMatchObject({ ok: true });
|
||||
expect(received).not.toHaveBeenCalled();
|
||||
|
||||
storageBus.emitAsForeignArea = false;
|
||||
expect(
|
||||
publisher.publish({ topic: TOPIC, topicVersion: 1 }),
|
||||
).toMatchObject({ ok: true });
|
||||
expect(received).toHaveBeenCalledOnce();
|
||||
|
||||
publisher.close();
|
||||
receiver.close();
|
||||
});
|
||||
|
||||
it("enters explicit local-only degradation when every transport fails", () => {
|
||||
const storageBus = new FakeStorageBus();
|
||||
const endpoint = storageBus.createEndpoint();
|
||||
|
||||
@@ -12,6 +12,45 @@ const profile = {
|
||||
} as const;
|
||||
|
||||
describe("bounded cursor pagination runtime", () => {
|
||||
it("returns PAGINATION_ABORTED when a non-cooperative page resolves after abort", async () => {
|
||||
const controller = new AbortController();
|
||||
let releasePage: ((page: unknown) => void) | undefined;
|
||||
const loadPage = vi.fn(
|
||||
() =>
|
||||
new Promise((resolve) => {
|
||||
releasePage = resolve as (page: unknown) => void;
|
||||
}),
|
||||
);
|
||||
const runtime = createCursorPaginationRuntime({
|
||||
definitionId: "bounded",
|
||||
profile,
|
||||
loadPage: loadPage as never,
|
||||
});
|
||||
|
||||
const loading = runtime.loadAll({ signal: controller.signal });
|
||||
await Promise.resolve();
|
||||
controller.abort();
|
||||
|
||||
const result = await loading;
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "REQUEST_ABORTED", code: "PAGINATION_ABORTED" },
|
||||
});
|
||||
|
||||
// The late page completion must be ignored, not accumulated.
|
||||
releasePage?.({
|
||||
ok: true,
|
||||
value: {
|
||||
items: ["late"],
|
||||
nextCursor: null,
|
||||
hasMore: false,
|
||||
snapshotToken: "snapshot-1",
|
||||
},
|
||||
});
|
||||
await Promise.resolve();
|
||||
expect(loadPage).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("loads a stable finite chain without exposing cursors in its value", async () => {
|
||||
const loadPage = vi
|
||||
.fn()
|
||||
|
||||
Reference in New Issue
Block a user