fix(dotnet): validate manifest layout before capacity (#12325)

* fix(dotnet): validate manifest layout before capacity

* fix(dotnet): classify full-width unsupported flags
This commit is contained in:
morluto authored and GitHub committed 2026-11-19 12:44:32 +00:00
1 parent 5bdb7af43e
commit ff6f4f4e31
2 files changed
+155 -42

No files matched your search

+52 -41
View File
@@ -94,9 +94,9 @@ export const readDotnetBundle = async (
if (count < 0) fail(".NET bundle entry count is negative");
// Include the bundle ID length prefix and, starting at v2, the two
// configuration ranges and flags. Each row has a type and a path prefix.
const minimumHeaderTail = majorVersion >= 2 ? 41 : 1;
const minimumEntry = majorVersion === 6 ? 26 : 18;
if (minimumHeaderTail + count * minimumEntry > cursor.remainingBytes)
const afterBundleId = (majorVersion >= 2 ? 40 : 0) + count * minimumEntry;
if (1 + afterBundleId > cursor.remainingBytes)
fail(".NET bundle entry count cannot fit in the manifest");
if (count > limits.entry_count)
throw artifactReaderLimitFailure(
@@ -105,21 +105,21 @@ export const readDotnetBundle = async (
"count",
count,
);
await cursor.string(); // Bundle ID is not an authentication or content digest.
await cursor.string(afterBundleId);
const manifestStart = BigInt(headerOffset);
const configuration: { offset: number; size: number; type: number }[] = [];
if (majorVersion >= 2) {
configuration.push({
offset: await cursor.nonnegativeI64(),
size: await cursor.nonnegativeI64(),
type: 3,
});
configuration.push({
offset: await cursor.nonnegativeI64(),
size: await cursor.nonnegativeI64(),
type: 4,
});
for (const type of [3, 4]) {
const offset = await cursor.nonnegativeI64();
const size = await cursor.nonnegativeI64();
if (!fitsBeforeManifest(offset, size, manifestStart))
fail(
".NET bundle configuration location disagrees with its file table",
);
configuration.push({ offset: Number(offset), size: Number(size), type });
}
const flags = await cursor.u64();
if (flags > 1)
if (flags > 1n)
throw new ArtifactReaderFailure(
"unavailable",
`Unsupported .NET bundle flags: ${flags}`,
@@ -140,17 +140,21 @@ export const readDotnetBundle = async (
let expandedBytes = 0n;
for (let index = 0; index < count; index++) {
abortIfNeeded(signal);
const offset = await cursor.nonnegativeI64();
const fileSize = await cursor.nonnegativeI64();
const compressedSize =
majorVersion === 6 ? await cursor.nonnegativeI64() : 0;
const storedSize = compressedSize || fileSize;
if (offset > headerOffset || storedSize > headerOffset - offset)
const declaredOffset = await cursor.nonnegativeI64();
const declaredSize = await cursor.nonnegativeI64();
const declaredCompressedSize =
majorVersion === 6 ? await cursor.nonnegativeI64() : 0n;
const declaredStoredSize = declaredCompressedSize || declaredSize;
if (!fitsBeforeManifest(declaredOffset, declaredStoredSize, manifestStart))
fail(
`.NET bundle entry crosses the manifest or file boundary (entry ${index + 1}, offset ${offset}, stored size ${storedSize})`,
`.NET bundle entry crosses the manifest or file boundary (entry ${index + 1}, offset ${declaredOffset}, stored size ${declaredStoredSize})`,
);
const fileType = (await cursor.take(1))[0]!;
const path = await cursor.string();
const path = await cursor.string((count - index - 1) * minimumEntry);
const offset = Number(declaredOffset);
const storedSize = Number(declaredStoredSize);
const compressedSize = Number(declaredCompressedSize);
const fileSize = exactManifestInteger(declaredSize);
const normalized = normalizeArtifactPath(path);
paths.add(normalized, "file");
if (fileSize > limits.member_bytes || compressedSize > limits.member_bytes)
@@ -274,28 +278,15 @@ class BundleCursor {
async i32(): Promise<number> {
return (await this.take(4)).readInt32LE();
}
async nonnegativeI64(): Promise<number> {
async nonnegativeI64(): Promise<bigint> {
const value = (await this.take(8)).readBigInt64LE();
if (value < 0n) fail(".NET bundle offset or size is negative");
return this.exactInteger(value);
return value;
}
private exactInteger(value: bigint): number {
if (value > BigInt(Number.MAX_SAFE_INTEGER))
throw artifactFixedLimitFailure(
{
name: "The .NET bundle exact integer range",
kind: "parser",
limit: Number.MAX_SAFE_INTEGER,
unit: "integer units",
},
`A .NET bundle offset or size of ${String(value)} is above the exact integer range of ${String(Number.MAX_SAFE_INTEGER)}.`,
);
return Number(value);
async u64(): Promise<bigint> {
return (await this.take(8)).readBigUInt64LE();
}
async u64(): Promise<number> {
return this.exactInteger((await this.take(8)).readBigUInt64LE());
}
async string(): Promise<string> {
async string(minimumTailBytes: number): Promise<string> {
let length = 0;
for (let shift = 0; shift <= 28; shift += 7) {
const byte = (await this.take(1))[0]!;
@@ -303,7 +294,7 @@ class BundleCursor {
if (shift === 28 && byte > 7) fail("Invalid .NET bundle string length");
length += (byte & 127) * 2 ** shift;
if ((byte & 128) !== 0) continue;
this.#requireRemaining(length);
this.#requireRemaining(length + minimumTailBytes);
if (length > this.limits.string_bytes)
throw artifactReaderLimitFailure(
"artifact_limits.dotnet_bundle.string_bytes",
@@ -328,6 +319,26 @@ class BundleCursor {
}
}
const fitsBeforeManifest = (
offset: bigint,
size: bigint,
manifestStart: bigint,
): boolean => offset <= manifestStart && size <= manifestStart - offset;
const exactManifestInteger = (value: bigint): number => {
if (value > BigInt(Number.MAX_SAFE_INTEGER))
throw artifactFixedLimitFailure(
{
name: "The .NET bundle exact integer range",
kind: "parser",
limit: Number.MAX_SAFE_INTEGER,
unit: "integer units",
},
`A .NET bundle offset or size of ${String(value)} is above the exact integer range of ${String(Number.MAX_SAFE_INTEGER)}.`,
);
return Number(value);
};
const fail = (message: string): never => {
throw new ArtifactReaderFailure("format", message);
};
@@ -55,7 +55,7 @@ it.each([
},
);
it("reports a missing fixed-width manifest field before the metadata budget", async () => {
it("reports a truncated manifest before the metadata budget", async () => {
const root = await createTestTempDirectory("rea-dotnet-truncated-field-");
const path = join(root, "host.exe");
const fixture = dotnetBundleFixture(
@@ -83,6 +83,107 @@ it("reports a missing fixed-width manifest field before the metadata budget", as
}
});
it.each(["bundle ID", "entry path"] as const)(
"checks the required layout after a long %s before reporting a string limit",
async (field) => {
const root = await createTestTempDirectory("rea-dotnet-string-tail-");
const path = join(root, "host.exe");
const bytes = Buffer.from("x");
const longText = "a".repeat(100);
const fixture =
field === "bundle ID"
? dotnetBundleFixture([{ path: "one", bytes }], 6, longText)
: dotnetBundleFixture(
[
{ path: longText, bytes },
{ path: "two", bytes },
],
6,
"id",
);
const end =
field === "bundle ID"
? fixture.fields.configuration!.deps.offset
: fixture.fields.entries[1]!.offset;
for (const [content, reason] of [
[fixture.bytes, "limit"],
[fixture.bytes.subarray(0, end), "format"],
] as const) {
await writeFile(path, content);
const handle = await open(path, "r");
try {
await expect(
readDotnetBundle(
handle,
content.length,
fixture.headerOffset,
undefined,
{
...DEFAULT_DOTNET_BUNDLE_READER_LIMITS,
string_bytes: 4,
},
),
).rejects.toMatchObject({ reason });
} finally {
await handle.close();
}
}
},
);
it.each([
{ record: "member", field: "offset" },
{ record: "member", field: "size" },
{ record: "member", field: "compressedSize" },
{ record: "configuration", field: "offset" },
{ record: "configuration", field: "size" },
] as const)(
"rejects a .NET bundle with an impossible $record $field",
async ({ record, field }) => {
const root = await createTestTempDirectory("rea-dotnet-stored-extent-");
const path = join(root, "host.exe");
const fixture = dotnetBundleFixture([
{ path: "member", bytes: Buffer.from("x") },
]);
const at =
record === "member"
? fixture.fields.entries[0]![field]!
: fixture.fields.configuration!.deps[field];
fixture.bytes.writeBigInt64LE(2n ** 53n, at);
await writeFile(path, fixture.bytes);
const handle = await open(path, "r");
try {
await expect(
readDotnetBundle(handle, fixture.bytes.length, fixture.headerOffset),
).rejects.toMatchObject({ reason: "format" });
} finally {
await handle.close();
}
},
);
it.each([2n, 2n ** 63n])(
"reports unsupported .NET bundle flags %s at their full width",
async (flags) => {
const root = await createTestTempDirectory("rea-dotnet-flags-");
const path = join(root, "host.exe");
const fixture = dotnetBundleFixture();
fixture.bytes.writeBigUInt64LE(flags, fixture.fields.configuration!.flags);
await writeFile(path, fixture.bytes);
const handle = await open(path, "r");
try {
await expect(
readDotnetBundle(handle, fixture.bytes.length, fixture.headerOffset),
).rejects.toMatchObject({
reason: "unavailable",
message: `Unsupported .NET bundle flags: ${flags}`,
});
} finally {
await handle.close();
}
},
);
it.each(["close", "cancel"] as const)(
"does not complete a .NET bundle inventory after %s at the last entry",
async (interruption) => {
@@ -232,6 +333,7 @@ it("retains the exact integer representation ceiling independently of reader bud
path: "file",
bytes: Buffer.from("x"),
declaredSize: 9_007_199_254_740_992,
compressed: true,
},
]);
await writeFile(path, fixture.bytes);