cultnet-ts: validateManifest owns the whole-number size rule

A manifest or chunk size that is a fraction, NaN or Infinity is refused in the one
validator. C# sizes are long, so the wire cannot carry one; the rule guards hand-built
manifests. The reader's private copy is deleted in the next commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-19 18:23:47 +02:00
commit c2bb187843
2 changed files with 23 additions and 1 deletions

View file

@ -97,13 +97,18 @@ export async function artifactRecordKey(artifactId: string | null, version: stri
// ---- validation ------------------------------------------------------------------------------------------------------
/** `ValidateManifestShape`, in its order and with its messages, then the metadata keys a dictionary could not hold twice. */
/**
* `ValidateManifestShape`, in its order and with its messages, then the metadata keys a dictionary could not hold twice.
* C# sizes are `long`, so a fraction, NaN or Infinity cannot exist on the wire; a hand-built manifest is refused here, once.
*/
export function validateManifest(manifest: CultMeshCdnArtifactManifest): void {
if (manifest.sizeBytes < 0) throw new Error("CDN artifact manifest has a negative size.");
if (!Number.isInteger(manifest.sizeBytes)) throw new Error("CDN artifact manifest size is not a whole number.");
let expectedOffset = 0;
for (const reference of [...manifest.chunks].sort((a, b) => a.offset - b.offset)) {
if (reference.offset !== expectedOffset) throw new Error("CDN artifact chunks are not contiguous.");
if (reference.sizeBytes < 0) throw new Error("CDN artifact chunk has a negative size.");
if (!Number.isInteger(reference.sizeBytes)) throw new Error("CDN artifact chunk size is not a whole number.");
expectedOffset += reference.sizeBytes;
}
if (expectedOffset !== manifest.sizeBytes) throw new Error("CDN artifact chunk sizes do not sum to the manifest size.");

View file

@ -231,6 +231,23 @@ test("validateManifest refuses negative size, a gap, an overlap and a size-sum m
validateManifest(manifestOf({ sizeBytes: 0, chunks: [] }));
});
test("validateManifest refuses a size that is not a whole number: a fraction, NaN or Infinity, in the manifest or in a chunk", () => {
const chunk = (offset: number, sizeBytes: number) => ({ chunkHash: HASH, offset, sizeBytes, recordKey: "k" });
const refuses = (over: Partial<CultMeshCdnArtifactManifest>, message: string) =>
assert.throws(() => validateManifest(manifestOf(over)), new Error(message));
const manifestMessage = "CDN artifact manifest size is not a whole number.";
const chunkMessage = "CDN artifact chunk size is not a whole number.";
for (const size of [1.5, Number.NaN, Number.POSITIVE_INFINITY]) refuses({ sizeBytes: size, chunks: [] }, manifestMessage);
// fractional chunk sizes that still tile the manifest are contiguous, so only the whole-number rule refuses them
refuses({ sizeBytes: 6, chunks: [chunk(0, 4), chunk(4, 1.5), chunk(5.5, 0.5)] }, chunkMessage);
refuses({ sizeBytes: 1.5, chunks: [chunk(0, 1.5)] }, manifestMessage);
refuses({ sizeBytes: 4, chunks: [chunk(0, Number.NaN)] }, chunkMessage);
refuses({ sizeBytes: Number.POSITIVE_INFINITY, chunks: [chunk(0, Number.POSITIVE_INFINITY)] }, manifestMessage);
refuses({ sizeBytes: 4, chunks: [chunk(0, 2), chunk(2, Number.POSITIVE_INFINITY)] }, chunkMessage);
refuses({ sizeBytes: 0, chunks: [chunk(0, -1)] }, "CDN artifact chunk has a negative size.");
validateManifest(manifestOf({ sizeBytes: 2 ** 53 - 1, chunks: [chunk(0, 2 ** 53 - 1)] }));
});
test("normalizeHash: 'SHA256:ABC ' -> 'abc', ' sha256:abc' -> 'sha256:abc', blank throws", () => {
assert.equal(normalizeHash("SHA256:ABC "), "abc");
assert.equal(normalizeHash("sHa256:AbC"), "abc");