fix(wasm): keep the section walk defensive on a truncated section header (#215)

This commit is contained in:
4n5rud
2026-11-14 01:29:18 +09:00
committed by GitHub
parent ff6363d374
commit f44415a04a
2 changed files with 72 additions and 5 deletions
+37 -5
View File
@@ -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;
@@ -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");
});
});