Skip to content

Commit a01adca

Browse files
authored
fix(miniflare): parse pem file with regex (#9089) (#9117)
1 parent a2a56c8 commit a01adca

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

.changeset/soft-wasps-beg.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"miniflare": patch
3+
---
4+
5+
fix: skip comment lines when parsing `NODE_EXTRA_CA_CERTS`

packages/miniflare/src/plugins/core/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ if (process.env.NODE_EXTRA_CA_CERTS !== undefined) {
8787
const extra = readFileSync(process.env.NODE_EXTRA_CA_CERTS, "utf8");
8888
// Split bundle into individual certificates and add each individually:
8989
// https://github.com/cloudflare/miniflare/pull/587/files#r1271579671
90-
const pemBegin = "-----BEGIN";
91-
for (const cert of extra.split(pemBegin)) {
92-
if (cert.trim() !== "") trustedCertificates.push(pemBegin + cert);
90+
const certs = extra.match(
91+
/-----BEGIN CERTIFICATE-----[\s\S]+?-----END CERTIFICATE-----/g
92+
);
93+
94+
if (certs !== null) {
95+
trustedCertificates.push(...certs);
9396
}
9497
} catch {}
9598
}

packages/miniflare/test/plugins/core/index.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ opensslTest("NODE_EXTRA_CA_CERTS: loads certificates", async (t) => {
4848
// (see https://github.com/cloudflare/miniflare/pull/587/files#r1271579671)
4949
const caCertsPath = path.join(tmp, "bundle.pem");
5050
const caCerts = [...tls.rootCertificates, cert];
51-
await fs.writeFile(caCertsPath, caCerts.join("\n"));
51+
await fs.writeFile(
52+
caCertsPath,
53+
["## This is a comment which should be ignored\n"].concat(
54+
caCerts.join("\n")
55+
)
56+
);
5257

5358
// Start Miniflare with NODE_EXTRA_CA_CERTS environment variable
5459
// (cannot use sync process methods here as that would block HTTPS server)

0 commit comments

Comments
 (0)