1+ import {
2+ KeyUsagesExtension ,
3+ X509Certificate as X509CertificatePeculiar ,
4+ } from "@peculiar/x509" ;
15import axios from "axios" ;
6+ import { X509Certificate as X509CertificateNode } from "node:crypto" ;
27import * as fs from "node:fs/promises" ;
38import https from "node:https" ;
49import { afterAll , beforeAll , describe , expect , it , vi } from "vitest" ;
@@ -15,9 +20,7 @@ describe("Certificate errors", () => {
1520 // These tests run in Electron (BoringSSL) for accurate certificate validation testing.
1621
1722 it ( "should run in Electron environment" , ( ) => {
18- const isElectron =
19- process . versions . electron || process . env . ELECTRON_RUN_AS_NODE ;
20- expect ( isElectron ) . toBeTruthy ( ) ;
23+ expect ( process . versions . electron ) . toBeTruthy ( ) ;
2124 } ) ;
2225
2326 beforeAll ( ( ) => {
@@ -113,8 +116,7 @@ describe("Certificate errors", () => {
113116 } ) ;
114117
115118 // In Electron a self-issued certificate without the signing capability fails
116- // (again with the same "unable to verify" error) but in Node self-issued
117- // certificates are not required to have the signing capability.
119+ // (again with the same "unable to verify" error)
118120 it ( "detects self-signed certificates without signing capability" , async ( ) => {
119121 const address = await startServer ( "no-signing" ) ;
120122 const request = axios . get ( address , {
@@ -146,6 +148,24 @@ describe("Certificate errors", () => {
146148 await expect ( request ) . resolves . toHaveProperty ( "data" , "foobar" ) ;
147149 } ) ;
148150
151+ // Node's X509Certificate.keyUsage is unreliable, so use a third-party parser
152+ it ( "parses no-signing cert keyUsage with third-party library" , async ( ) => {
153+ const certPem = await fs . readFile (
154+ getFixturePath ( "tls" , "no-signing.crt" ) ,
155+ "utf-8" ,
156+ ) ;
157+
158+ // Node's implementation seems to always return `undefined`
159+ const nodeCert = new X509CertificateNode ( certPem ) ;
160+ expect ( nodeCert . keyUsage ) . toBeUndefined ( ) ;
161+
162+ // Here we can correctly get the KeyUsages
163+ const peculiarCert = new X509CertificatePeculiar ( certPem ) ;
164+ const extension = peculiarCert . getExtension ( KeyUsagesExtension ) ;
165+ expect ( extension ) . toBeDefined ( ) ;
166+ expect ( extension ?. usages ) . toBeTruthy ( ) ;
167+ } ) ;
168+
149169 // Both environments give the same error code when a self-issued certificate is
150170 // untrusted.
151171 it ( "detects self-signed certificates" , async ( ) => {
0 commit comments