fix(wxapkg): admit member extents under one index layout (#12192)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 04:59:56 +00:00
1 parent c61b4789ad
commit 2ca44e56ac
5 files changed
+89 -71

No files matched your search

+3 -3
View File
@@ -23,9 +23,9 @@ plain header. They keep their format identity but report `encrypted`
directory status with unknown coverage: member identities and bytes remain
unknown and no decryption is attempted, so extraction refuses with a typed
error rather than producing ciphertext. Declared tables or data regions
that exceed the file, member extents outside the declared data region,
truncated rows, invalid UTF-8 names, and counts that cannot fit their table
are malformed-input errors.
that exceed the file, truncated rows, invalid UTF-8 names, and counts that
cannot fit their table are malformed-input errors. All member extents must
fit the data region declared by one index-length convention.
## Resource bounds
@@ -4,7 +4,6 @@
"src/application/MipsGnuAttributes.ts": 2,
"src/artifacts/DesktopFileInspection.ts": 4,
"src/artifacts/UnityFsStructures.ts": 8,
"src/artifacts/WxapkgStructures.ts": 3,
"src/artifacts/apple/NibArchive.ts": 8,
"src/artifacts/bun/BunModuleGraphLayout.ts": 22,
"src/artifacts/dotnet/DotnetBundle.ts": 5,
@@ -68,7 +67,6 @@
"src/domain/windows/ntfsMft.ts": 19,
"src/domain/windows/ntfsMftAttributes.ts": 38,
"src/domain/windows/ntfsUsnJournal.ts": 23,
"src/domain/wxapkgFormat.ts": 4,
"src/dotnet/ManagedConstantValue.ts": 12,
"src/dotnet/ManagedFieldConstants.ts": 1,
"src/dotnet/ManagedFieldDefinitionRows.ts": 1,
@@ -276,6 +276,25 @@ describe("WxapkgArtifactReader data region", () => {
);
});
it("rejects member extents that require different index-length conventions", async () => {
// The rows end at 44. Counting the member count in the index length places
// data at [44, 48). Counting only rows places it at [48, 52).
const bytes = buildWxapkg(
[
{ path: "a", data: "AAAA", offset: 44 },
{ path: "b", data: "BBBB", offset: 48 },
],
{ indexLengthIncludesFileCount: true, declaredDataLength: 4 },
);
await withReader(bytes, async (reader) => {
await expect(list(reader)).rejects.toMatchObject({
reason: "format",
message:
"wxapkg is malformed: no single declared data region contains all member extents",
});
});
});
it("rejects a data region declared past the file", async () => {
await withReader(
buildWxapkg([{ path: "x", data: "ok" }], {
+21 -23
View File
@@ -1,3 +1,4 @@
import { BinaryReader } from "../domain/binaryContainer.js";
import { Budget } from "../domain/budget.js";
import type { FileHandle } from "node:fs/promises";
import {
@@ -8,10 +9,10 @@ import {
import { readArtifactFileBytes } from "./ArtifactFileRange.js";
import { setImmediate } from "node:timers/promises";
import {
admitWxapkgMemberExtents,
hasWxapkgEncryptedMagic,
readWxapkgHeaderFacts,
resolveWxapkgIndexLayouts,
selectWxapkgIndexLayout,
WXAPKG_MIN_ROW_BYTES,
type WxapkgIndexLayout,
type WxapkgIndexLayoutFailure,
@@ -153,6 +154,7 @@ export const readWxapkgFileTable = async (
// Loose lower bound already checked in facts; precise rows are checked here.
if (table.length < header.fileCount * WXAPKG_MIN_ROW_BYTES)
throw wxapkgFormatFailure("member table is truncated");
const reader = new BinaryReader(table, false, "wxapkg");
const members: WxapkgMember[] = [];
let cursor = 0;
for (let index = 0; index < header.fileCount; index++) {
@@ -160,7 +162,7 @@ export const readWxapkgFileTable = async (
assertArtifactReadActive(signal);
if (cursor + 4 > table.length)
throw wxapkgFormatFailure("member row is truncated");
const nameLength = table.readUInt32BE(cursor);
const nameLength = reader.u32(cursor);
cursor += 4;
if (
nameLength === 0 ||
@@ -183,8 +185,8 @@ export const readWxapkgFileTable = async (
);
}
cursor += nameLength;
const offset = table.readUInt32BE(cursor);
const sizeMember = table.readUInt32BE(cursor + 4);
const offset = reader.u32(cursor);
const sizeMember = reader.u32(cursor + 4);
cursor += 8;
// Producer names are root-prefixed; translate to package-relative once.
const path = declaredPath.startsWith("/")
@@ -194,25 +196,21 @@ export const readWxapkgFileTable = async (
throw wxapkgFormatFailure("member name has no package-relative path");
members.push({ offset, size: sizeMember, path, declaredPath });
}
const candidates = indexLayouts.filter((index) => index.tableBytes >= cursor);
if (candidates.length === 0)
throw wxapkgFormatFailure("member table is truncated");
// Rows and member ranges choose the convention. EOF is not a signal: four
// trailing bytes can make the other convention end on the carrier.
const index = selectWxapkgIndexLayout(candidates, cursor, members);
if (index === undefined) {
const outside = members.find((member) => {
const extentEnd = member.offset + member.size;
return candidates.every(
(candidate) =>
member.offset < candidate.dataStart ||
extentEnd > candidate.dataEnd ||
extentEnd < member.offset,
);
});
throw wxapkgFormatFailure(
`member exceeds the data region: ${outside?.path ?? ""}`,
);
// Rows and member ranges choose the convention. EOF can fit either.
const admitted = admitWxapkgMemberExtents(indexLayouts, cursor, members);
if (!admitted.ok) {
switch (admitted.error._tag) {
case "TruncatedRows":
throw wxapkgFormatFailure("member table is truncated");
case "MemberOutsideDataRegion":
throw wxapkgFormatFailure(
`member exceeds the data region: ${admitted.error.member.path}`,
);
case "ConflictingMemberExtents":
throw wxapkgFormatFailure(
"no single declared data region contains all member extents",
);
}
}
return { members };
};
+46 -43
View File
@@ -1,3 +1,4 @@
import { BinaryReader } from "./binaryContainer.js";
import { err, ok, type Result } from "./result.js";
/**
@@ -33,6 +34,16 @@ export const hasWxapkgEncryptedMagic = (bytes: Uint8Array): boolean =>
WXAPKG_ENCRYPTED_MAGIC.length,
).equals(WXAPKG_ENCRYPTED_MAGIC);
/**
* Whether bytes carry the plain wxapkg marks. Recognition is intentionally
* separate from admission: a damaged package keeps its format identity and the
* reader rejects impossible table accounting as a typed format failure.
*/
export const hasWxapkgHeader = (bytes: Uint8Array): boolean =>
bytes.length >= WXAPKG_HEADER_BYTES &&
bytes[0] === WXAPKG_FIRST_MARK &&
bytes[13] === WXAPKG_LAST_MARK;
/** Declared header fields; support and extent admission belong to callers. */
export interface WxapkgHeaderFacts {
/** Unknown header field after the first mark; producers write zero. */
@@ -55,17 +66,16 @@ export interface WxapkgHeaderFacts {
export const readWxapkgHeaderFacts = (
bytes: Uint8Array,
): WxapkgHeaderFacts | undefined => {
if (
bytes.length < WXAPKG_HEADER_BYTES ||
bytes[0] !== WXAPKG_FIRST_MARK ||
bytes[13] !== WXAPKG_LAST_MARK
)
return undefined;
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const info = view.getUint32(1);
const indexLength = view.getUint32(5);
const dataLength = view.getUint32(9);
const fileCount = view.getUint32(14);
if (!hasWxapkgHeader(bytes)) return undefined;
const reader = new BinaryReader(
Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength),
false,
"wxapkg",
);
const info = reader.u32(1);
const indexLength = reader.u32(5);
const dataLength = reader.u32(9);
const fileCount = reader.u32(14);
// Every row carries at least its fixed fields; a table too small for the
// declared count cannot be a wxapkg index.
if (fileCount === 0 || indexLength < fileCount * WXAPKG_MIN_ROW_BYTES)
@@ -73,16 +83,6 @@ export const readWxapkgHeaderFacts = (
return { info, indexLength, dataLength, fileCount };
};
/**
* Whether bytes carry the plain wxapkg marks. Recognition is intentionally
* separate from admission: a damaged package keeps its format identity and the
* reader rejects impossible table accounting as a typed format failure.
*/
export const hasWxapkgHeader = (bytes: Uint8Array): boolean =>
bytes.length >= WXAPKG_HEADER_BYTES &&
bytes[0] === WXAPKG_FIRST_MARK &&
bytes[13] === WXAPKG_LAST_MARK;
/** Row table and declared data region for one index-length convention. */
export interface WxapkgIndexLayout {
/** Absolute offset of the first name/offset/size row. */
@@ -169,30 +169,33 @@ const memberFitsIndex = (
);
};
/**
* Choose the admitted index that contains the parsed rows and every member
* range. An index that ends on the last parsed row wins a tie. Neither the
* carrier end nor the shorter table is enough on its own: four trailing bytes
* can make the other convention land on EOF.
*/
export const selectWxapkgIndexLayout = (
type WxapkgMemberExtentFailure<Member> =
| { readonly _tag: "TruncatedRows" }
| { readonly _tag: "MemberOutsideDataRegion"; readonly member: Member }
| { readonly _tag: "ConflictingMemberExtents" };
/** Require one declared layout to contain the parsed rows and every member. */
export const admitWxapkgMemberExtents = <
Member extends { readonly offset: number; readonly size: number },
>(
layouts: readonly WxapkgIndexLayout[],
consumedRowBytes: number,
members: readonly { readonly offset: number; readonly size: number }[],
): WxapkgIndexLayout | undefined => {
const fitting = layouts.filter(
(layout) =>
layout.tableBytes >= consumedRowBytes &&
members: readonly Member[],
): Result<void, WxapkgMemberExtentFailure<Member>> => {
const candidates = layouts.filter(
(layout) => layout.tableBytes >= consumedRowBytes,
);
if (candidates.length === 0) return err({ _tag: "TruncatedRows" });
if (
candidates.some((layout) =>
members.every((member) => memberFitsIndex(layout, member)),
)
)
return ok(undefined);
const outside = members.find((member) =>
candidates.every((layout) => !memberFitsIndex(layout, member)),
);
const exactRows = fitting.filter(
(layout) => layout.tableBytes === consumedRowBytes,
);
const pool = exactRows.length > 0 ? exactRows : fitting;
let selected: WxapkgIndexLayout | undefined;
for (const layout of pool) {
if (selected === undefined || layout.tableBytes < selected.tableBytes)
selected = layout;
}
return selected;
return outside !== undefined
? err({ _tag: "MemberOutsideDataRegion", member: outside })
: err({ _tag: "ConflictingMemberExtents" });
};