diff --git a/src/shields/formats/wasm.ts b/src/shields/formats/wasm.ts index dd39fb9..1377b4b 100644 --- a/src/shields/formats/wasm.ts +++ b/src/shields/formats/wasm.ts @@ -48,6 +48,24 @@ function decodeUnsignedLeb128(buf: Buffer, startOffset: number): { value: number throw new Error("Truncated LEB128 integer in WASM binary"); } +/** + * Non-throwing variant of {@link decodeUnsignedLeb128} for defensive section + * walks. Returns null on a truncated / invalid LEB128 (e.g. a section-id byte + * with no following size field) so callers documented as defensive — which + * guard a truncated section *body* but decoded the section *size* header + * outside that guard — never throw on hostile / corrupt input. + */ +function tryDecodeUnsignedLeb128( + buf: Buffer, + startOffset: number, +): { value: number; nextOffset: number } | null { + try { + return decodeUnsignedLeb128(buf, startOffset); + } catch { + return null; + } +} + function encodeWasmName(str: string): Buffer { const utf8 = Buffer.from(str, "utf8"); return Buffer.concat([encodeUnsignedLeb128(utf8.length), utf8]); @@ -304,7 +322,9 @@ function collectExportedFuncIndices(bytes: Buffer): number[] { let exportEntries: WasmExportEntry[] | null = null; while (offset < bytes.length) { const sectionId = bytes[offset++]!; - const { value: sectionSize, nextOffset } = decodeUnsignedLeb128(bytes, offset); + const header = tryDecodeUnsignedLeb128(bytes, offset); + if (header === null) break; // truncated section-size header → stop defensively + const { value: sectionSize, nextOffset } = header; const sectionEnd = nextOffset + sectionSize; if (sectionEnd > bytes.length) break; const payload = bytes.subarray(nextOffset, sectionEnd); @@ -426,7 +446,9 @@ export function inspectWasmBinary(bytes: Buffer): WasmInspectionSummary { while (offset < bytes.length) { const sectionId = bytes[offset++]!; - const { value: sectionSize, nextOffset } = decodeUnsignedLeb128(bytes, offset); + const header = tryDecodeUnsignedLeb128(bytes, offset); + if (header === null) break; // truncated section-size header → stop defensively + const { value: sectionSize, nextOffset } = header; const sectionEnd = nextOffset + sectionSize; if (sectionEnd > bytes.length) break; sectionCount++; @@ -507,7 +529,9 @@ export function readWasmCustomSectionContent( let offset = 8; while (offset < bytes.length) { const sectionId = bytes[offset++]!; - const { value: sectionSize, nextOffset } = decodeUnsignedLeb128(bytes, offset); + const header = tryDecodeUnsignedLeb128(bytes, offset); + if (header === null) break; // truncated section-size header → stop defensively + const { value: sectionSize, nextOffset } = header; const sectionEnd = nextOffset + sectionSize; if (sectionEnd > bytes.length) break; if (sectionId === 0) { @@ -549,7 +573,13 @@ export function stripWasmCustomSectionByName( while (offset < bytes.length) { const sectionStart = offset; const sectionId = bytes[offset++]!; - const { value: sectionSize, nextOffset } = decodeUnsignedLeb128(bytes, offset); + const header = tryDecodeUnsignedLeb128(bytes, offset); + if (header === null) { + // Truncated section-size header → keep the malformed tail verbatim. + out.push(Buffer.from(bytes.subarray(sectionStart))); + break; + } + const { value: sectionSize, nextOffset } = header; const sectionEnd = nextOffset + sectionSize; if (sectionEnd > bytes.length) { out.push(Buffer.from(bytes.subarray(sectionStart))); @@ -687,7 +717,9 @@ export function protectWasmBinary( while (offset < bytes.length) { const sectionStart = offset; const sectionId = bytes[offset++]!; - const { value: sectionSize, nextOffset } = decodeUnsignedLeb128(bytes, offset); + const header = tryDecodeUnsignedLeb128(bytes, offset); + if (header === null) break; // truncated section-size header → stop defensively + const { value: sectionSize, nextOffset } = header; const sectionEnd = nextOffset + sectionSize; if (sectionEnd > bytes.length) break; diff --git a/tests/wasm-truncated-section-header.test.ts b/tests/wasm-truncated-section-header.test.ts new file mode 100644 index 0000000..6a494d5 --- /dev/null +++ b/tests/wasm-truncated-section-header.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from "vitest"; +import { + appendWasmCustomSection, + inspectWasmBinary, + readWasmCustomSectionContent, + stripWasmCustomSectionByName, +} from "../src/shields/formats/wasm.js"; + +// Valid magic + version, then a lone section-id byte with no size LEB128 → +// the section-size header is truncated. The walk helpers must stay defensive. +const TRUNCATED = Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x07]); + +describe("WASM section walk is defensive on a truncated section header", () => { + it("inspectWasmBinary returns a summary instead of throwing", () => { + const info = inspectWasmBinary(TRUNCATED); + expect(info.validMagic).toBe(true); + expect(info.sectionCount).toBe(0); + expect(info.exportNames).toEqual([]); + }); + + it("readWasmCustomSectionContent returns null instead of throwing", () => { + expect(readWasmCustomSectionContent(TRUNCATED, "name")).toBeNull(); + }); + + it("stripWasmCustomSectionByName preserves the bytes instead of throwing", () => { + const out = stripWasmCustomSectionByName(TRUNCATED, "name"); + expect(out.equals(TRUNCATED)).toBe(true); + }); + + it("appendWasmCustomSection still appends instead of throwing", () => { + const out = appendWasmCustomSection(TRUNCATED, "bla.sidecar.v1", Buffer.from("x")); + expect(out.subarray(0, TRUNCATED.length).equals(TRUNCATED)).toBe(true); + expect(out.toString("latin1")).toContain("bla.sidecar.v1"); + }); +});