From 3b6734bfaebccb717d655f93ef1d361a772c8ebc Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 14:21:25 -0500 Subject: [PATCH] test(pe): consolidate certificate parsing and public error proofs (#13565) * test(pe): consolidate certificate parsing and public error proofs * test(pe): keep lifecycle coverage linked to the BER proof --- src/native/pe/PeCertificateParser.test.ts | 15 +- tests/acceptance/peCertificatesCli.test.ts | 217 +++++---------------- tests/support/providerLifecycleCoverage.ts | 2 +- 3 files changed, 63 insertions(+), 171 deletions(-) diff --git a/src/native/pe/PeCertificateParser.test.ts b/src/native/pe/PeCertificateParser.test.ts index 879b10bfb0..e80f3c4387 100644 --- a/src/native/pe/PeCertificateParser.test.ts +++ b/src/native/pe/PeCertificateParser.test.ts @@ -1148,6 +1148,7 @@ it("accepts only RFC 5280 UTC validity instants", () => { // UTCTime 00 is 2000, a leap year; GeneralizedTime carries 2050 onward. for (const [tag, time] of [ [0x17, "000229235959Z"], + [0x17, "240229000000Z"], [0x18, "20500101000000Z"], ] as const) expect( @@ -1259,11 +1260,15 @@ it("decodes UniversalString names as UTF-32BE", () => { ); }); -it.each([0xd800, 0xdfff])( - "rejects BMPString surrogate code unit %i in certificate names", - (codeUnit) => { - const content = Buffer.alloc(2); - content.writeUInt16BE(codeUnit); +it.each([ + { units: [0xd800] }, + { units: [0xdfff] }, + { units: [0xd800, 0xdc00] }, +])( + "rejects BMPString surrogate code units $units in certificate names", + ({ units }) => { + const content = Buffer.alloc(units.length * 2); + units.forEach((unit, index) => content.writeUInt16BE(unit, index * 2)); const certificate = readAsn1( namedCertificate( directoryName(der(0x1e, content)), diff --git a/tests/acceptance/peCertificatesCli.test.ts b/tests/acceptance/peCertificatesCli.test.ts index ff05a75cf7..e0936e6804 100644 --- a/tests/acceptance/peCertificatesCli.test.ts +++ b/tests/acceptance/peCertificatesCli.test.ts @@ -140,7 +140,7 @@ cliTest.for([false, true])( }, ); -cliTest.for([0x00, 0x01, 0x7f, 0xfe, 0xff, null])( +cliTest.for([0x00, 0xff, null])( "requires canonical extension critical encoding %s through CLI and MCP", async (critical, { cli }) => { const root = await createTestTempDirectory("rea-pe-cert-critical-"); @@ -207,50 +207,51 @@ cliTest.for([0x00, 0x01, 0x7f, 0xfe, 0xff, null])( }, ); -cliTest.for(["extra-field", "empty-rdn"] as const)( - "rejects a malformed certificate name %s through CLI and MCP", - async (kind, { cli }) => { +cliTest( + "refuses an empty certificate RDN through both adapters", + async ({ cli }) => { const root = await createTestTempDirectory("rea-pe-name-structure-"); const path = join(root, "malformed.exe"); - const subject = - kind === "empty-rdn" - ? Buffer.from([0x30, 0x02, 0x31, 0]) - : Buffer.from([ - 0x30, 14, 0x31, 12, 0x30, 10, 0x06, 3, 0x55, 0x04, 3, 0x0c, 1, 0x41, - 0x05, 0, - ]); const payload = pkcs7SignedDataFixture( undefined, undefined, undefined, undefined, - { subject }, + { + subject: Buffer.from([0x30, 0x02, 0x31, 0]), + }, ); const bytes = peCertificateFixture(false, payload); await writeFile(path, bytes); const result = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); - expect.soft(result.exitCode, result.stdout).toBe(1); - expect.soft(result.json).toMatchObject({ + expect(result.exitCode, result.stdout).toBe(1); + expect(result.json).toMatchObject({ code: "invalid_request", details: { operation: "inspect_pe_certificates", issues: [ - expect.objectContaining({ path: ["path"], reason: "invalid_format" }), + expect.objectContaining({ + path: ["path"], + reason: "invalid_format", + message: expect.stringContaining("Name RDN"), + }), ], }, }); const call = await cli.sharedMcpCall!(); const response = await call("inspect_pe_certificates", { path }); - expect.soft(response.isError, JSON.stringify(response)).toBe(true); + expect(parseMcpToolError(response).error).toEqual(result.json); + expect(response.structuredContent).toBeUndefined(); expect((await readFile(path)).equals(bytes)).toBe(true); }, ); -cliTest.for(["pubkey", "signature"])( - "rejects nonzero BIT STRING padding in %s through the public CLI", - async (field, { cli }) => { +cliTest( + "rejects nonzero certificate signature padding through the public CLI", + async ({ cli }) => { + const field = "signature"; const root = await createTestTempDirectory("rea-pe-cert-bit-padding-"); const path = join(root, "padding.exe"); const payload = pkcs7SignedDataFixture(); @@ -295,11 +296,13 @@ cliTest.for(["pubkey", "signature"])( ); cliTest( - "reports PKCS#7 signer identity and RDN indices through CLI and MCP", + "reports PKCS#7 signer identity and BER lengths through both adapters", async ({ cli }) => { const root = await createTestTempDirectory("rea-pe-cert-cli-"); const path = join(root, "signed.exe"); - await writeFile(path, peCertificateFixture()); + const payload = pkcs7SignedDataIndefiniteEnvelopeFixture(); + const bytes = peCertificateFixture(false, payload); + await writeFile(path, bytes); const result = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); @@ -307,19 +310,34 @@ cliTest( const call = await cli.sharedMcpCall!(); const response = await call("inspect_pe_certificates", { path }); expect(response.isError, JSON.stringify(response.content)).not.toBe(true); - for (const value of [result.json, response.structuredContent]) { - const report = contract.outputSchema.parse(value).normalized_result; - const pkcs7 = report.entries[0]?.pkcs7; - expect(pkcs7?.certificates[0]?.serial_hex).toBe("11223344"); - for (const name of [ - pkcs7?.certificates[0]?.issuer, - pkcs7?.certificates[0]?.subject, - pkcs7?.signers[0]?.issuer, - ]) - expect(name?.attributes.map(({ rdn_index }) => rdn_index)).toEqual([ - 0, 1, - ]); - } + const evidence = contract.outputSchema.parse(result.json); + const mcp = contract.outputSchema.parse(response.structuredContent); + const report = evidence.normalized_result; + expect(evidence.caller_input_path).toBe(path); + expect(mcp.evidence_id).toBe(evidence.evidence_id); + expect(mcp.normalized_result).toEqual(report); + expect(report.artifact).toEqual({ + path, + bytes: bytes.length, + sha256: createHash("sha256").update(bytes).digest("hex"), + }); + const entry = report.entries[0]; + expect(entry?.payload).toMatchObject({ + bytes: payload.length, + sha256: createHash("sha256").update(payload).digest("hex"), + }); + const pkcs7 = entry?.pkcs7; + expect(pkcs7?.certificates[0]?.serial_hex).toBe("11223344"); + expect(pkcs7?.certificates[0]?.location.bytes).toBeGreaterThan(0); + for (const name of [ + pkcs7?.certificates[0]?.issuer, + pkcs7?.certificates[0]?.subject, + pkcs7?.signers[0]?.issuer, + ]) + expect(name?.attributes.map(({ rdn_index }) => rdn_index)).toEqual([ + 0, 1, + ]); + expect((await readFile(path)).equals(bytes)).toBe(true); }, ); @@ -349,29 +367,6 @@ cliTest("reports unsigned attributes through CLI and MCP", async ({ cli }) => { } }); -cliTest( - "inspects SignedData with an indefinite constructed ContentInfo envelope", - async ({ cli }) => { - const root = await createTestTempDirectory("rea-pe-cert-ber-cli-"); - const path = join(root, "ber-signed.exe"); - const payload = pkcs7SignedDataIndefiniteEnvelopeFixture(); - await writeFile(path, peCertificateFixture(false, payload)); - - const result = await cli.run({ - arguments: ["inspect-pe-certificates", path, "--json"], - }); - - expect(result.exitCode, result.stderr).toBe(0); - const evidence = contract.outputSchema.parse(result.json); - const entry = evidence.normalized_result.entries[0]; - expect(entry?.pkcs7?.certificates[0]).toMatchObject({ - serial_hex: "11223344", - }); - expect(entry?.payload.bytes).toBe(payload.length); - expect(entry?.pkcs7?.certificates[0]?.location.bytes).toBeGreaterThan(0); - }, -); - cliTest( "resolves a relative PE path against the working directory", async ({ cli }) => { @@ -474,73 +469,6 @@ cliTest( }, ); -cliTest( - "rejects surrogate pairs in BMPString certificate names and accepts valid neighboring code units", - async ({ cli }) => { - const root = await createTestTempDirectory("rea-pe-cert-cli-bmp-"); - const path = join(root, "bmp.exe"); - const payload = pkcs7SignedDataFixture("ABCD"); - const nameAt = payload.indexOf( - Buffer.from([0x0c, 0x04, 0x41, 0x42, 0x43, 0x44]), - ); - expect(nameAt).toBeGreaterThanOrEqual(0); - // Equal-size replacement keeps the DER lengths and all unrelated fields intact. - Buffer.from([0x1e, 0x04, 0xd8, 0, 0xdc, 0]).copy(payload, nameAt); - await writeFile(path, peCertificateFixture(false, payload)); - const invalid = await cli.run({ - arguments: ["inspect-pe-certificates", path, "--json"], - }); - expect(invalid.exitCode).toBe(1); - expect(invalid.json).toMatchObject({ - code: "invalid_request", - details: { operation: "inspect_pe_certificates" }, - }); - Buffer.from([0x1e, 0x04, 0xd7, 0xff, 0xe0, 0]).copy(payload, nameAt); - await writeFile(path, peCertificateFixture(false, payload)); - const valid = await cli.run({ - arguments: ["inspect-pe-certificates", path, "--json"], - }); - expect(valid.exitCode, valid.stderr).toBe(0); - const evidence = contract.outputSchema.parse(valid.json); - expect( - evidence.normalized_result.entries[0]?.pkcs7?.certificates[0]?.issuer - .text, - ).toBe("commonName=\ud7ff\ue000, organizationName=REA Tests"); - }, -); - -cliTest( - "rejects a certificate validity date that is not a calendar day through the public CLI", - async ({ cli }) => { - const root = await createTestTempDirectory("rea-pe-cert-cli-validity-"); - const path = join(root, "validity.exe"); - const payload = pkcs7SignedDataFixture(); - const timeAt = payload.indexOf(Buffer.from("250101000000Z")); - expect(timeAt).toBeGreaterThanOrEqual(0); - // 2025 has no 29 February; the equal-length edit keeps every DER length. - payload.write("250229000000Z", timeAt, "ascii"); - await writeFile(path, peCertificateFixture(false, payload)); - const invalid = await cli.run({ - arguments: ["inspect-pe-certificates", path, "--json"], - }); - expect(invalid.exitCode).toBe(1); - expect(invalid.json).toMatchObject({ - code: "invalid_request", - details: { operation: "inspect_pe_certificates" }, - }); - payload.write("240229000000Z", timeAt, "ascii"); - await writeFile(path, peCertificateFixture(false, payload)); - const valid = await cli.run({ - arguments: ["inspect-pe-certificates", path, "--json"], - }); - expect(valid.exitCode, valid.stderr).toBe(0); - const evidence = contract.outputSchema.parse(valid.json); - expect( - evidence.normalized_result.entries[0]?.pkcs7?.certificates[0]?.not_before, - ).toBe("240229000000Z"); - }, -); - cliTest( "inventories repeated certificates within both decoder allowances", async ({ cli }) => { @@ -614,44 +542,3 @@ cliTest.for([ expect(remediation.action).toContain(String(Object.values(limits)[0])); }, ); - -cliTest.for([ - [0x13, "REA@Fixture"], - [0x16, "REAéFixture"], -] as const)( - "rejects an invalid certificate name alphabet for tag %i through the public CLI", - async ([tag, name], { cli }) => { - const root = await createTestTempDirectory("rea-pe-cert-name-alphabet-"); - const path = join(root, "name.exe"); - const payload = pkcs7SignedDataFixture(name); - const content = Buffer.from(name, "utf8"); - const marker = Buffer.concat([ - Buffer.from([0x0c, content.length]), - content, - ]); - const at = payload.indexOf(marker); - expect(at).toBeGreaterThanOrEqual(0); - payload[at] = tag; - await writeFile(path, peCertificateFixture(false, payload)); - const invalid = await cli.run({ - arguments: ["inspect-pe-certificates", path, "--json"], - }); - expect(invalid.exitCode).toBe(1); - expect(invalid.json).toMatchObject({ - code: "invalid_request", - details: { operation: "inspect_pe_certificates" }, - }); - // Only restore the string tag: the same bytes are valid UTF8String content. - payload[at] = 0x0c; - await writeFile(path, peCertificateFixture(false, payload)); - const valid = await cli.run({ - arguments: ["inspect-pe-certificates", path, "--json"], - }); - expect(valid.exitCode, valid.stderr).toBe(0); - const evidence = contract.outputSchema.parse(valid.json); - expect( - evidence.normalized_result.entries[0]?.pkcs7?.certificates[0]?.issuer - .attributes[0]?.value, - ).toBe(name); - }, -); diff --git a/tests/support/providerLifecycleCoverage.ts b/tests/support/providerLifecycleCoverage.ts index 69ee855236..f3d9f3ea53 100644 --- a/tests/support/providerLifecycleCoverage.ts +++ b/tests/support/providerLifecycleCoverage.ts @@ -1504,7 +1504,7 @@ export const PROVIDER_LIFECYCLE_COVERAGE: Readonly< covered( test( "tests/acceptance/peCertificatesCli.test.ts", - "reports PKCS#7 signer identity and RDN indices through CLI and MCP", + "reports PKCS#7 signer identity and BER lengths through both adapters", ), ), typedFailures("tests/boundary/filesystem/peCertificates.test.ts"),