Skip to content

Commit 2d3f7fb

Browse files
committed
Limit generated certs to 200 days to match new CAB rules
This doesn't matter really for usage as a private CA, where generally these don't apply, but if you're injecting Mockttp certs as a standard system CA anywhere the resulting certificates will generally have to be 200 days max.
1 parent 50f46a1 commit 2d3f7fb

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

src/util/certificates.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ class CA {
378378
const notBefore = new Date();
379379
notBefore.setDate(notBefore.getDate() - 1); // Valid from 24 hours ago
380380

381-
const notAfter = new Date();
382-
notAfter.setFullYear(notAfter.getFullYear() + 1); // Valid for 1 year
381+
// As of March 2026, public certs are limited to 200 days
382+
const notAfter = new Date(notBefore.getTime() + 200 * 24 * 60 * 60 * 1000);
383383

384384
const extensions: x509.Extension[] = [];
385385
extensions.push(new x509.BasicConstraintsExtension(false, undefined, true));

test/certificates.spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,21 @@ nodeOnly(() => {
176176
const caCertificate = await caCertificatePromise;
177177
const ca = await getCA({ key: caCertificate.key, cert: caCertificate.cert, keyLength: 1024 });
178178

179-
const { cert, key } = await ca.generateCertificate('localhost');
179+
const { cert, key, expiresAt } = await ca.generateCertificate('localhost');
180180

181181
expect(cert.length).to.be.greaterThan(1000);
182182
expect(cert.split('\n')[0]).to.equal('-----BEGIN CERTIFICATE-----');
183-
expect(key.length).to.be.greaterThan(1000);
183+
expect(key.length).to.be.greaterThan(500);
184184
expect(key.split('\n')[0]).to.equal('-----BEGIN PRIVATE KEY-----');
185+
186+
// Cert validity must be <= 200 days (notBefore is 1 day ago, notAfter is 199 days from now)
187+
const certData = new x509.X509Certificate(cert);
188+
const validityDays = (certData.notAfter.getTime() - certData.notBefore.getTime()) / (1000 * 60 * 60 * 24);
189+
expect(validityDays).to.be.at.most(200);
190+
expect(validityDays).to.be.at.least(199);
191+
192+
// expiresAt should match the cert's notAfter (within 1s - cert times have second precision)
193+
expect(Math.abs(expiresAt.getTime() - certData.notAfter.getTime())).to.be.at.most(1000);
185194
});
186195

187196
it("should be able to generate a CA certificate that passes lintcert checks", async function () {

0 commit comments

Comments
 (0)