mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +00:00
fix(dotnet): check manifest bounds before metadata budget (#12287)
* fix(dotnet): check manifest bounds before metadata budget * docs(dotnet): describe bounded manifest read windows * fix(dotnet): validate stored member ranges before path budgets
This commit is contained in:
1 parent
66e0bd717f
commit
fce336236e
4 files changed
+107
-14
No files matched your search
@@ -50,7 +50,8 @@ explicitly with the managed tools to establish PE/CLI facts.
|
||||
|
||||
## Admission and containment
|
||||
|
||||
Metadata is range-read with a cache of at most 64 KiB, bounded by the remaining
|
||||
Metadata is range-read in 64 KiB windows, or a larger window when an admitted
|
||||
field requires it. Each read is bounded by the remaining file bytes and the
|
||||
selected metadata allowance. Default policy budgets are 16 MiB of
|
||||
manifest metadata, 100,000 files, 64 KiB per UTF-8 string, 512 MiB of stored or
|
||||
expanded bytes per file, 256 path parts per member, 250,000 distinct in-memory
|
||||
|
||||
@@ -108,7 +108,7 @@ export class DotnetBundleArtifactReader implements ArtifactReader {
|
||||
declaredSize: file.size,
|
||||
compressedSize: file.compressedSize || null,
|
||||
byteOffset: file.offset,
|
||||
byteLength: file.compressedSize || file.size,
|
||||
byteLength: file.storedSize,
|
||||
executable: false,
|
||||
encrypted: false,
|
||||
declaredIntegrity: null,
|
||||
@@ -252,7 +252,7 @@ export class DotnetBundleArtifactReader implements ArtifactReader {
|
||||
},
|
||||
signal?: AbortSignal,
|
||||
): AsyncGenerator<Buffer> {
|
||||
const storedBytes = file.compressedSize || file.size;
|
||||
const storedBytes = file.storedSize;
|
||||
const input =
|
||||
storedBytes === 0
|
||||
? Readable.from([])
|
||||
|
||||
@@ -26,6 +26,7 @@ export interface DotnetBundleFile {
|
||||
readonly offset: number;
|
||||
readonly size: number;
|
||||
readonly compressedSize: number;
|
||||
readonly storedSize: number;
|
||||
readonly fileType: number;
|
||||
}
|
||||
|
||||
@@ -143,13 +144,15 @@ export const readDotnetBundle = async (
|
||||
const fileSize = await cursor.nonnegativeI64();
|
||||
const compressedSize =
|
||||
majorVersion === 6 ? await cursor.nonnegativeI64() : 0;
|
||||
const storedSize = compressedSize || fileSize;
|
||||
if (offset > headerOffset || storedSize > headerOffset - offset)
|
||||
fail(
|
||||
`.NET bundle entry crosses the manifest or file boundary (entry ${index + 1}, offset ${offset}, stored size ${storedSize})`,
|
||||
);
|
||||
const fileType = (await cursor.take(1))[0]!;
|
||||
const path = await cursor.string();
|
||||
const normalized = normalizeArtifactPath(path);
|
||||
paths.add(normalized, "file");
|
||||
const storedSize = compressedSize || fileSize;
|
||||
if (offset > headerOffset || storedSize > headerOffset - offset)
|
||||
fail(`.NET bundle entry crosses the manifest or file boundary: ${path}`);
|
||||
if (fileSize > limits.member_bytes || compressedSize > limits.member_bytes)
|
||||
throw artifactReaderLimitFailure(
|
||||
"artifact_limits.dotnet_bundle.member_bytes",
|
||||
@@ -165,16 +168,23 @@ export const readDotnetBundle = async (
|
||||
"bytes",
|
||||
expandedBytes,
|
||||
);
|
||||
files.push({ path, offset, size: fileSize, compressedSize, fileType });
|
||||
files.push({
|
||||
path,
|
||||
offset,
|
||||
size: fileSize,
|
||||
compressedSize,
|
||||
storedSize,
|
||||
fileType,
|
||||
});
|
||||
}
|
||||
const ranges = files
|
||||
.filter((file) => (file.compressedSize || file.size) > 0)
|
||||
.filter((file) => file.storedSize > 0)
|
||||
.toSorted((a, b) => a.offset - b.offset);
|
||||
let end = 0;
|
||||
for (const file of ranges) {
|
||||
if (file.offset < end)
|
||||
fail(`Overlapping .NET bundle entries: ${file.path}`);
|
||||
end = file.offset + (file.compressedSize || file.size);
|
||||
end = file.offset + file.storedSize;
|
||||
}
|
||||
for (const item of configuration) {
|
||||
if (item.offset === 0 && item.size === 0) continue;
|
||||
@@ -212,8 +222,15 @@ class BundleCursor {
|
||||
return this.size - this.#position;
|
||||
}
|
||||
|
||||
#requireRemaining(length: number): number {
|
||||
const remaining = this.remainingBytes;
|
||||
if (length > remaining) fail("Truncated .NET bundle manifest");
|
||||
return remaining;
|
||||
}
|
||||
|
||||
async take(length: number): Promise<Buffer> {
|
||||
abortIfNeeded(this.signal);
|
||||
const remaining = this.#requireRemaining(length);
|
||||
const consumed = this.#position - this.start;
|
||||
if (consumed + length > this.limits.metadata_bytes)
|
||||
throw artifactReaderLimitFailure(
|
||||
@@ -222,15 +239,13 @@ class BundleCursor {
|
||||
"bytes",
|
||||
consumed + length,
|
||||
);
|
||||
if (length > this.size - this.#position)
|
||||
fail("Truncated .NET bundle manifest");
|
||||
const cached = this.#position - this.#cacheOffset;
|
||||
if (cached < 0 || cached + length > this.#cache.length) {
|
||||
this.#cacheOffset = this.#position;
|
||||
this.#cache = Buffer.alloc(
|
||||
Math.min(
|
||||
Math.max(length, 64 * 1024),
|
||||
this.size - this.#position,
|
||||
remaining,
|
||||
this.limits.metadata_bytes - consumed,
|
||||
),
|
||||
);
|
||||
@@ -288,8 +303,7 @@ class BundleCursor {
|
||||
if (shift === 28 && byte > 7) fail("Invalid .NET bundle string length");
|
||||
length += (byte & 127) * 2 ** shift;
|
||||
if ((byte & 128) !== 0) continue;
|
||||
if (length > this.size - this.#position)
|
||||
fail("Truncated .NET bundle manifest");
|
||||
this.#requireRemaining(length);
|
||||
if (length > this.limits.string_bytes)
|
||||
throw artifactReaderLimitFailure(
|
||||
"artifact_limits.dotnet_bundle.string_bytes",
|
||||
|
||||
@@ -55,6 +55,34 @@ it.each([
|
||||
},
|
||||
);
|
||||
|
||||
it("reports a missing fixed-width manifest field before the metadata budget", async () => {
|
||||
const root = await createTestTempDirectory("rea-dotnet-truncated-field-");
|
||||
const path = join(root, "host.exe");
|
||||
const fixture = dotnetBundleFixture(
|
||||
[{ path: "member", bytes: Buffer.from("x") }],
|
||||
1,
|
||||
"owner-layout-id",
|
||||
);
|
||||
const rowStart = fixture.fields.entries[0]!.offset;
|
||||
// The valid bundle ID leaves only three bytes of the first eight-byte field.
|
||||
const bytes = fixture.bytes.subarray(0, rowStart + 3);
|
||||
await writeFile(path, bytes);
|
||||
const handle = await open(path, "r");
|
||||
try {
|
||||
await expect(
|
||||
readDotnetBundle(handle, bytes.length, fixture.headerOffset, undefined, {
|
||||
...DEFAULT_DOTNET_BUNDLE_READER_LIMITS,
|
||||
metadata_bytes: rowStart - fixture.headerOffset,
|
||||
}),
|
||||
).rejects.toMatchObject({
|
||||
reason: "format",
|
||||
message: "Truncated .NET bundle manifest",
|
||||
});
|
||||
} 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) => {
|
||||
@@ -146,6 +174,56 @@ it.each([
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
{ field: "string_bytes", maximum: 4, compressed: false },
|
||||
{ field: "path_parts", maximum: 1, compressed: true },
|
||||
{ field: "path_nodes", maximum: 1, compressed: false },
|
||||
] as const)(
|
||||
"reports an impossible stored member extent before the $field allowance",
|
||||
async ({ field, maximum, compressed }) => {
|
||||
const root = await createTestTempDirectory("rea-dotnet-invalid-extent-");
|
||||
const path = join(root, "host.exe");
|
||||
const fixture = dotnetBundleFixture(
|
||||
[{ path: "dir/member", bytes: Buffer.from("x"), compressed }],
|
||||
6,
|
||||
"id",
|
||||
);
|
||||
const limits = { ...DEFAULT_DOTNET_BUNDLE_READER_LIMITS, [field]: maximum };
|
||||
const read = async () => {
|
||||
await writeFile(path, fixture.bytes);
|
||||
const handle = await open(path, "r");
|
||||
try {
|
||||
return await readDotnetBundle(
|
||||
handle,
|
||||
fixture.bytes.length,
|
||||
fixture.headerOffset,
|
||||
undefined,
|
||||
limits,
|
||||
);
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
};
|
||||
await expect(read()).rejects.toMatchObject({
|
||||
reason: "limit",
|
||||
readerLimit: { parameter: `artifact_limits.dotnet_bundle.${field}` },
|
||||
});
|
||||
|
||||
const member = fixture.fields.entries[0]!;
|
||||
if (compressed)
|
||||
fixture.bytes.writeBigInt64LE(
|
||||
BigInt(fixture.headerOffset - fixture.offsets[0]! + 1),
|
||||
member.compressedSize!,
|
||||
);
|
||||
else
|
||||
fixture.bytes.writeBigInt64LE(
|
||||
BigInt(fixture.headerOffset + 1),
|
||||
member.offset,
|
||||
);
|
||||
await expect(read()).rejects.toMatchObject({ reason: "format" });
|
||||
},
|
||||
);
|
||||
|
||||
it("retains the exact integer representation ceiling independently of reader budgets", async () => {
|
||||
const root = await createTestTempDirectory("rea-dotnet-integer-allowance-");
|
||||
const path = join(root, "host.exe");
|
||||
|
||||
Reference in new issue
Block a user