diff --git a/src/artifacts/dotnet/DotnetBundle.ts b/src/artifacts/dotnet/DotnetBundle.ts index 8d1d3834df..4ac457ef32 100644 --- a/src/artifacts/dotnet/DotnetBundle.ts +++ b/src/artifacts/dotnet/DotnetBundle.ts @@ -274,9 +274,12 @@ class BundleCursor { let length = 0; for (let shift = 0; shift <= 28; shift += 7) { const byte = (await this.take(1))[0]!; - if (shift === 28 && byte > 15) fail("Invalid .NET bundle string length"); + // BinaryReader.ReadString uses a nonnegative signed Int32 length. + 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"); if (length > this.limits.string_bytes) throw artifactReaderLimitFailure( "artifact_limits.dotnet_bundle.string_bytes", diff --git a/tests/boundary/filesystem/dotnetBundleArtifacts.test.ts b/tests/boundary/filesystem/dotnetBundleArtifacts.test.ts index 80d18d7d55..12c4d3ec75 100644 --- a/tests/boundary/filesystem/dotnetBundleArtifacts.test.ts +++ b/tests/boundary/filesystem/dotnetBundleArtifacts.test.ts @@ -24,6 +24,37 @@ import { import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { writeMinimalElfHeader } from "../../fixtures/nativeHeaders.js"; +it.each([ + { + length: [0x80, 0x80, 0x80, 0x80, 0x08], + message: "Invalid .NET bundle string length", + }, + { + length: [0xff, 0xff, 0xff, 0xff, 0x07], + message: "Truncated .NET bundle manifest", + }, +])( + "reports malformed bundle ID length $length as a format failure", + async ({ length, message }) => { + const root = await createTestTempDirectory("rea-dotnet-invalid-string-"); + const path = join(root, "host.exe"); + const fixture = dotnetBundleFixture([ + { path: "member", bytes: Buffer.from("x") }, + ]); + // The fixed manifest prefix contains two version fields and a file count. + fixture.bytes.set(length, fixture.headerOffset + 12); + 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", message }); + } 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) => {