cultcache-ts: the reader judges the stamp from the header before it decodes the body; the header-form pins return
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
19f3a5edbb
commit
d1dfd8f027
4 changed files with 133 additions and 3 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import { decode } from "@msgpack/msgpack";
|
||||
|
||||
import { checkPayloadDepth } from "./payload-depth";
|
||||
import { type StoreFormat, isStoreSnapshot, requireStoreSlots, requireV1RecordSlots } from "./store-format";
|
||||
import { type StoreFormat, isStoreSnapshot, refuseUnreadStoreBytes, requireStoreSlots, requireV1RecordSlots } from "./store-format";
|
||||
|
||||
export interface CultCacheInspection {
|
||||
filePath: string;
|
||||
|
|
@ -47,6 +47,7 @@ export function inspectCultCacheBytes(
|
|||
bytes: Uint8Array,
|
||||
fileSizeBytes = bytes.length,
|
||||
): CultCacheInspection {
|
||||
refuseUnreadStoreBytes(bytes);
|
||||
const decoded = decode(bytes);
|
||||
|
||||
if (isStoreSnapshot(decoded)) {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import { z } from "zod";
|
|||
|
||||
import { documentPayloadBytes, documentPayloadFromBytes } from "./document";
|
||||
import { commitStoreFile, withStoreWriteLock } from "./store-file";
|
||||
import { STORE_FORMAT_VERSION, SchemaConflictError, type StoreFormat, StoreUnreadableError, isStoreSnapshot, requireStoreSlots, requireV1RecordSlots } from "./store-format";
|
||||
import { STORE_FORMAT_VERSION, SchemaConflictError, type StoreFormat, StoreUnreadableError, isStoreSnapshot, refuseUnreadStoreBytes, requireStoreSlots, requireV1RecordSlots } from "./store-format";
|
||||
import type {
|
||||
CacheBackingStore,
|
||||
CultCacheEnvelope,
|
||||
|
|
@ -400,6 +400,8 @@ export type SingleFileStoreRead = {
|
|||
};
|
||||
|
||||
function decodeStoreFile(data: Uint8Array): Omit<SingleFileStoreRead, "absent"> {
|
||||
// The stamp is judged before the body is decoded, as Rust's store_header does: a newer store is refused by name.
|
||||
refuseUnreadStoreBytes(data);
|
||||
const decoded = decode(data);
|
||||
const snapshot = decodeSnapshot(decoded);
|
||||
if (snapshot) {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import { decode } from "@msgpack/msgpack";
|
||||
|
||||
export const STORE_FORMAT_VERSION = "cultcache.store.v1";
|
||||
|
||||
/**
|
||||
|
|
@ -26,6 +28,44 @@ export function isStoreSnapshot(decoded: unknown): decoded is [StoreFormat, ...u
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuses a store whose header names a format this runtime does not read, judging the header alone: the leading array's first
|
||||
* element, when it is a complete string. The reader calls it before it decodes the body, so a newer store is refused by name
|
||||
* whether or not its body is whole. Bytes with no such header (a legacy array, an empty, truncated or garbled file) are not
|
||||
* refused here.
|
||||
*/
|
||||
export function refuseUnreadStoreBytes(bytes: Uint8Array): void {
|
||||
const header = peekHeader(bytes);
|
||||
if (header !== undefined && header.startsWith(STORE_FORMAT_PREFIX)) {
|
||||
refuseUnreadFormat(header);
|
||||
}
|
||||
}
|
||||
|
||||
// The first element of a leading non-empty MessagePack array when it is a complete string; anything else is undefined. The
|
||||
// string is decoded by the decoder a read uses, so a stamp gets one verdict from the peek and from the whole decode.
|
||||
function peekHeader(bytes: Uint8Array): string | undefined {
|
||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
const lead = bytes[0];
|
||||
let offset: number;
|
||||
if (lead === undefined) return undefined;
|
||||
if (lead >= 0x91 && lead <= 0x9f) offset = 1;
|
||||
else if (lead === 0xdc && bytes.length >= 3 && view.getUint16(1) > 0) offset = 3;
|
||||
else if (lead === 0xdd && bytes.length >= 5 && view.getUint32(1) > 0) offset = 5;
|
||||
else return undefined;
|
||||
const start = offset;
|
||||
const tag = bytes[offset];
|
||||
let length: number;
|
||||
if (tag === undefined) return undefined;
|
||||
if (tag >= 0xa0 && tag <= 0xbf) { length = tag & 0x1f; offset += 1; }
|
||||
else if (tag === 0xd9 && bytes.length >= offset + 2) { length = bytes[offset + 1]!; offset += 2; }
|
||||
else if (tag === 0xda && bytes.length >= offset + 3) { length = view.getUint16(offset + 1); offset += 3; }
|
||||
else if (tag === 0xdb && bytes.length >= offset + 5) { length = view.getUint32(offset + 1); offset += 5; }
|
||||
else return undefined;
|
||||
if (bytes.length < offset + length) return undefined;
|
||||
const header = decode(bytes.subarray(start, offset + length));
|
||||
return typeof header === "string" ? header : undefined;
|
||||
}
|
||||
|
||||
/** The one verdict on a stamp: this runtime reads v1 and v3, and any other `cultcache.store.*` header is refused by name. */
|
||||
function refuseUnreadFormat(header: string): void {
|
||||
if (header !== STORE_FORMAT_VERSION && header !== STORE_FORMAT_ELEMENT_IDS) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue