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:
DongHyeonka
2026-08-13 23:35:48 +09:00
co-authored by Claude Opus 5
parent b893d95b36
commit 4fe924ee0f
9 changed files with 261 additions and 19 deletions
+63 -3
View File
@@ -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();