From ce9c28fa2c886ff720ef76df9329136f72c77ee1 Mon Sep 17 00:00:00 2001 From: Iakhub Seitasanov Date: Sat, 14 Sep 2024 00:00:21 +0300 Subject: [PATCH] skip generating cert each time (#67122) --------- Co-authored-by: JJ Kasper --- packages/next/src/lib/mkcert.ts | 26 ++++++++++++++++--- .../https-server.generated-key.test.ts | 9 +++++++ test/lib/next-modes/base.ts | 2 -- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/packages/next/src/lib/mkcert.ts b/packages/next/src/lib/mkcert.ts index 5a2a7dc89ec7e..06eb46a369052 100644 --- a/packages/next/src/lib/mkcert.ts +++ b/packages/next/src/lib/mkcert.ts @@ -1,8 +1,9 @@ -import fs from 'fs' -import path from 'path' +import fs from 'node:fs' +import path from 'node:path' +import { X509Certificate, createPrivateKey } from 'node:crypto' import { getCacheDirectory } from './helpers/get-cache-directory' import * as Log from '../build/output/log' -import { execSync } from 'child_process' +import { execSync } from 'node:child_process' const { WritableStream } = require('node:stream/web') as { WritableStream: typeof global.WritableStream } @@ -112,6 +113,25 @@ export async function createSelfSignedCertificate( const keyPath = path.resolve(resolvedCertDir, 'localhost-key.pem') const certPath = path.resolve(resolvedCertDir, 'localhost.pem') + if (fs.existsSync(keyPath) && fs.existsSync(certPath)) { + const cert = new X509Certificate(fs.readFileSync(certPath)) + const key = fs.readFileSync(keyPath) + + if ( + cert.checkHost(host ?? 'localhost') && + cert.checkPrivateKey(createPrivateKey(key)) + ) { + Log.info('Using already generated self signed certificate') + const caLocation = execSync(`"${binaryPath}" -CAROOT`).toString().trim() + + return { + key: keyPath, + cert: certPath, + rootCA: `${caLocation}/rootCA.pem`, + } + } + } + Log.info( 'Attempting to generate self signed certificate. This may prompt for your password' ) diff --git a/test/development/experimental-https-server/https-server.generated-key.test.ts b/test/development/experimental-https-server/https-server.generated-key.test.ts index 82dc95332fe9c..c20fa2ff7864e 100644 --- a/test/development/experimental-https-server/https-server.generated-key.test.ts +++ b/test/development/experimental-https-server/https-server.generated-key.test.ts @@ -33,4 +33,13 @@ describe('experimental-https-server (generated certificate)', () => { const html = await renderViaHTTP(next.url, '/2', undefined, { agent }) expect(html).toContain('Hello from Pages') }) + + it('should successfully reuse generated certificates', async () => { + await next.stop() + await next.start() + expect(next.url).toInclude('https://') + expect(next.cliOutput).toContain( + 'Using already generated self signed certificate' + ) + }) }) diff --git a/test/lib/next-modes/base.ts b/test/lib/next-modes/base.ts index bfff29d05d280..ed2ff87feab2d 100644 --- a/test/lib/next-modes/base.ts +++ b/test/lib/next-modes/base.ts @@ -79,8 +79,6 @@ export class NextInstance { this.env = {} Object.assign(this, opts) - require('console').log('packageJson??', this.packageJson) - if (!isNextDeploy) { this.env = { ...this.env,