From 4ce0e218c81cb70a22564818fcfabb80cfa088fd Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 06:45:36 -0500 Subject: [PATCH] fix(deno): validate compile body extents before byte limits (#12290) --- src/artifacts/deno/DenoSectionLayout.test.ts | 51 +++++++++++++- src/artifacts/deno/DenoSectionLayout.ts | 70 +++++++------------- 2 files changed, 75 insertions(+), 46 deletions(-) diff --git a/src/artifacts/deno/DenoSectionLayout.test.ts b/src/artifacts/deno/DenoSectionLayout.test.ts index eb8a49ee63..24302daa90 100644 --- a/src/artifacts/deno/DenoSectionLayout.test.ts +++ b/src/artifacts/deno/DenoSectionLayout.test.ts @@ -1,6 +1,10 @@ import { expect, it } from "vitest"; -import { parseDenoSection } from "./DenoSectionLayout.js"; +import { + MAX_DENO_REMOTE_BODY_BYTES, + MAX_DENO_SPECIFIER_TEXT_BYTES, + parseDenoSection, +} from "./DenoSectionLayout.js"; import { buildDenoSection, defaultDenoMetadata, @@ -150,6 +154,51 @@ it("refuses a specifier table above the named record allowance", async () => { }); }); +it.each([ + { + text: "file:///wire-specifier.ts", + limit: MAX_DENO_SPECIFIER_TEXT_BYTES, + field: "specifier text", + }, + { + text: "wire-module", + limit: MAX_DENO_REMOTE_BODY_BYTES, + field: "remote module", + }, + { + text: "wire-transpiled", + limit: MAX_DENO_REMOTE_BODY_BYTES, + field: "remote transpiled", + }, +])( + "validates the $field extent before charging its byte allowance", + async ({ text, limit, field }) => { + const bytes = buildDenoSection({ + specifiers: [{ text: "file:///wire-specifier.ts", id: 0 }], + remoteModules: [ + { specifierId: 0, data: "wire-module", transpiled: "wire-transpiled" }, + ], + }); + const body = bytes.indexOf(Buffer.from(text)); + expect(body).toBeGreaterThan(4); + const claimed = limit + 1; + bytes.writeUInt32LE(claimed, body - 4); + const read = async (offset: number, length: number) => + bytes.subarray(offset, offset + length); + await expect(parseDenoSection(bytes.length, read)).rejects.toMatchObject({ + reason: "format", + message: expect.stringContaining(`${field} length overruns`), + }); + // The same body could fit at this logical section size. The selected + // allowance must stop the parser before it tries to read the body. + await expect( + parseDenoSection(body + claimed + 64, read), + ).rejects.toMatchObject({ + reason: "limit", + }); + }, +); + it("stops on unrecognized remote flags and duplicate specifier ids", async () => { await expect( parseDenoSectionBytes( diff --git a/src/artifacts/deno/DenoSectionLayout.ts b/src/artifacts/deno/DenoSectionLayout.ts index 5543cd29f4..73e9a0b2c4 100644 --- a/src/artifacts/deno/DenoSectionLayout.ts +++ b/src/artifacts/deno/DenoSectionLayout.ts @@ -160,15 +160,20 @@ export const parseDenoSection = async ( throw denoUnsupported("opening marker d3n0l4nd is absent"); let cursor = BigInt(DENO_SECTION_MAGIC.length); - const need = (bytes: bigint): void => { - if (cursor > size || bytes > size - cursor) - throw denoFormatFailure("length overruns the section"); + const takeRange = (claimed: bigint, scope?: string): DenoByteRange => { + if (cursor > size || claimed > size - cursor) + throw denoFormatFailure( + scope === undefined + ? "length overruns the section" + : `${scope} length overruns the section`, + ); + const range = { sectionOffset: Number(cursor), length: Number(claimed) }; + cursor += claimed; + return range; }; const pull = async (bytes: number): Promise => { - need(BigInt(bytes)); - const offset = Number(cursor); - cursor += BigInt(bytes); - return readExact(offset, bytes); + const range = takeRange(BigInt(bytes)); + return readExact(range.sectionOffset, range.length); }; const u8 = async (): Promise => (await pull(1))[0] ?? 0; const u32 = async (): Promise => @@ -180,25 +185,9 @@ export const parseDenoSection = async ( max: number, scope: string, ): Promise => { - if (claimed > size - cursor) - throw denoFormatFailure(`${scope} length overruns the section`); + const range = takeRange(claimed, scope); if (claimed > BigInt(max)) throw denoByteLimit(scope, claimed, max); - const offset = Number(cursor); - const length = Number(claimed); - cursor += claimed; - return readExact(offset, length); - }; - const skip = (claimed: bigint, scope: string): void => { - if (claimed > size - cursor) - throw denoFormatFailure(`${scope} length overruns the section`); - cursor += claimed; - }; - const skipBody = (length: number, scope: string): DenoByteRange => { - if (length > Number(size - cursor)) - throw denoFormatFailure(`${scope} length overruns the section`); - const sectionOffset = Number(cursor); - cursor += BigInt(length); - return { sectionOffset, length }; + return readExact(range.sectionOffset, range.length); }; const metadataBytes = await readBounded( @@ -207,11 +196,7 @@ export const parseDenoSection = async ( "metadata JSON", ); const metadata = observeMetadata(decodeJson(metadataBytes, "metadata JSON")); - const npmClaimed = await u64(); - if (npmClaimed > size - cursor) - throw denoFormatFailure("npm snapshot length overruns the section"); - const npmSnapshotByteLength = Number(npmClaimed); - skip(npmClaimed, "npm snapshot"); + const npmSnapshotByteLength = takeRange(await u64(), "npm snapshot").length; const specifierStart = cursor; const specifierCount = await u32(); @@ -223,6 +208,7 @@ export const parseDenoSection = async ( for (let index = 0; index < specifierCount; index += 1) { abortIfNeeded(signal); const length = await u32(); + const range = takeRange(BigInt(length), "specifier text"); textBytes += length; if (textBytes > MAX_DENO_SPECIFIER_TEXT_BYTES) throw denoByteLimit( @@ -230,7 +216,7 @@ export const parseDenoSection = async ( textBytes, MAX_DENO_SPECIFIER_TEXT_BYTES, ); - const bytes = await pull(length); + const bytes = await readExact(range.sectionOffset, range.length); const id = await u32(); if (specifierIds.has(id)) throw denoFormatFailure(`duplicate specifier id ${String(id)}`); @@ -276,6 +262,11 @@ export const parseDenoSection = async ( const remoteIds = new Set(); const modules: DenoRemoteModule[] = []; let remoteBodies = 0n; + const takeRemoteBody = (length: number, scope: string): DenoByteRange => { + const range = takeRange(BigInt(length), scope); + remoteBodies = addRemoteBody(remoteBodies, length); + return range; + }; let unresolvedRemote = namedSpecifiers === null; for (let index = 0; index < remoteCount; index += 1) { abortIfNeeded(signal); @@ -287,9 +278,7 @@ export const parseDenoSection = async ( remoteIds.add(specifierId); if (namedSpecifiers?.has(specifierId) !== true) unresolvedRemote = true; const mediaType = await u8(); - const dataLength = await u32(); - remoteBodies = addRemoteBody(remoteBodies, dataLength); - const data = skipBody(dataLength, "remote module"); + const data = takeRemoteBody(await u32(), "remote module"); const flags = await u8(); if ((flags & ~KNOWN_REMOTE_FLAGS) !== 0) throw denoUnsupported( @@ -300,9 +289,7 @@ export const parseDenoSection = async ( scope: string, ): Promise => { if ((flags & flag) === 0) return null; - const length = await u32(); - remoteBodies = addRemoteBody(remoteBodies, length); - return skipBody(length, scope); + return takeRemoteBody(await u32(), scope); }; const transpiled = await optionalRegion( FLAG_TRANSPILED, @@ -340,14 +327,7 @@ export const parseDenoSection = async ( "vfs header JSON", ); const vfsJson = decodeJson(vfsHeader, "vfs header JSON"); - const vfsFileClaimed = await u64(); - if (vfsFileClaimed > size - cursor) - throw denoFormatFailure("vfs file data length overruns the section"); - const vfsFileData = { - sectionOffset: Number(cursor), - length: Number(vfsFileClaimed), - }; - cursor += vfsFileClaimed; + const vfsFileData = takeRange(await u64(), "vfs file data"); if (size - cursor !== BigInt(DENO_SECTION_MAGIC.length)) throw denoFormatFailure( "closing marker is missing or trailing bytes follow it",