fix(pe): reject malformed certificate validity times (#8832)

This commit is contained in:
RRFZ authored and GitHub committed 2026-11-04 19:34:31 +08:00
1 parent 39172ca2e1
commit a97f827469
3 files changed
+112 -1

No files matched your search

+40 -1
View File
@@ -319,12 +319,51 @@ const decodeName = (
return { text, attributes };
};
// RFC 5280 4.1.2.5 and X.690 11.7-11.8: UTC with seconds, a final "Z", and
// no fractional seconds. UTCTime years 50-99 are 1950-1999, 00-49 are 2000-2049.
const VALIDITY_TIMES = {
0x17: {
label: "UTCTime",
layout: "YYMMDDHHMMSSZ",
form: /^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/u,
},
0x18: {
label: "GeneralizedTime",
layout: "YYYYMMDDHHMMSSZ",
form: /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/u,
},
} as const;
const isCalendarInstant = (
year: number,
[month = 0, day = 0, hour = 24, minute = 60, second = 60]: number[],
): boolean => {
const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
const days = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
return (
day >= 1 &&
day <= (days[month - 1] ?? 0) &&
hour <= 23 &&
minute <= 59 &&
second <= 59
);
};
const validityTime = (node: Asn1BerNode, budget: DecodedNameBudget): string => {
if (node.tag !== 0x17 && node.tag !== 0x18)
return fail("Validity instant is not UTCTime or GeneralizedTime.");
// A zero byte becomes "\u0000" (six characters) in the hashed evidence JSON.
chargeDecodedName(budget, node.content.length * 6);
return node.content.toString("utf8");
const { label, layout, form } = VALIDITY_TIMES[node.tag];
// Latin-1 maps each byte to one character, so no invalid byte can match.
const text = node.content.toString("latin1");
const [year = 0, ...rest] = form.exec(text)?.slice(1).map(Number) ?? [];
if (rest.length === 0)
return fail(`${label} validity instant is not ${layout}.`);
const fullYear = node.tag === 0x17 ? year + (year < 50 ? 2000 : 1900) : year;
if (!isCalendarInstant(fullYear, rest))
return fail(`${label} validity instant is not a calendar time.`);
return text;
};
export interface ParsedCertificate {
+38
View File
@@ -786,6 +786,44 @@ it("rejects a UTF8String name that is not valid UTF-8", () => {
).toBe("commonName=é");
});
it("accepts only RFC 5280 UTC validity instants", () => {
const name = directoryName(der(0x0c, Buffer.from("Fixture")));
const certificate = (notBefore: Buffer) =>
readAsn1(
derSeq(
derSeq(
derInt(1),
SHA256_ALG,
name,
derSeq(notBefore, der(0x17, Buffer.from("260101000000Z"))),
name,
spki,
),
SHA256_ALG,
der(0x03, Buffer.from([0])),
),
);
for (const [tag, content] of [
[0x17, Buffer.from([0xff, 0xfe])],
[0x17, Buffer.from("2501010000Z")],
[0x17, Buffer.from("250101000000+0800")],
[0x17, Buffer.from("250229000000Z")],
[0x18, Buffer.from("20250101000000.5Z")],
[0x18, Buffer.from("20250101240000Z")],
] as const)
expect(() => parseCertificate(certificate(der(tag, content)))).toThrow(
PeCertificateAsn1Failure,
);
// UTCTime 00 is 2000, a leap year; GeneralizedTime carries 2050 onward.
for (const [tag, time] of [
[0x17, "000229235959Z"],
[0x18, "20500101000000Z"],
] as const)
expect(
parseCertificate(certificate(der(tag, Buffer.from(time)))).not_before,
).toBe(time);
});
it.each([
["GeneralString", 0x1b, Buffer.from("name")],
["OCTET STRING", 0x04, Buffer.from("name")],
@@ -272,6 +272,40 @@ cliTest(
},
);
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 = toolContract("inspect_pe_certificates").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 }) => {