|
| 1 | +// Flags: --use-system-ca |
| 2 | + |
| 3 | +import * as common from '../common/index.mjs'; |
| 4 | +import assert from 'node:assert/strict'; |
| 5 | +import * as fixtures from '../common/fixtures.mjs'; |
| 6 | +import { it, describe } from 'node:test'; |
| 7 | +import { includesCert, extractMetadata } from '../common/tls.js'; |
| 8 | +import { execFileSync } from 'node:child_process'; |
| 9 | + |
| 10 | +if (!common.hasCrypto) { |
| 11 | + common.skip('requires crypto'); |
| 12 | +} |
| 13 | + |
| 14 | +if (process.platform !== 'darwin') { |
| 15 | + common.skip('macOS-specific test'); |
| 16 | +} |
| 17 | + |
| 18 | +function isCertInKeychain(cn) { |
| 19 | + try { |
| 20 | + execFileSync('security', ['find-certificate', '-c', cn], { stdio: 'pipe' }); |
| 21 | + return true; |
| 22 | + } catch { |
| 23 | + return false; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function isDupKeychainPresent() { |
| 28 | + try { |
| 29 | + const out = execFileSync( |
| 30 | + 'security', ['list-keychains', '-d', 'user'], |
| 31 | + { encoding: 'utf8' }, |
| 32 | + ); |
| 33 | + return out.includes('node-test-dup.keychain'); |
| 34 | + } catch { |
| 35 | + return false; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +const { default: tls } = await import('node:tls'); |
| 40 | + |
| 41 | +const systemCerts = tls.getCACertificates('system'); |
| 42 | +const fakeStartcomCert = fixtures.readKey('fake-startcom-root-cert.pem'); |
| 43 | +if (!includesCert(systemCerts, fakeStartcomCert)) { |
| 44 | + common.skip( |
| 45 | + 'fake-startcom-root-cert.pem not found in system CA store. ' + |
| 46 | + 'Please follow setup instructions in test/system-ca/README.md', |
| 47 | + ); |
| 48 | +} |
| 49 | +if (!isDupKeychainPresent()) { |
| 50 | + common.skip( |
| 51 | + 'Duplicate keychain not set up. ' + |
| 52 | + 'Please follow setup instructions in test/system-ca/README.md', |
| 53 | + ); |
| 54 | +} |
| 55 | +if (!isCertInKeychain('NodeJS-Test-Expired-Root')) { |
| 56 | + common.skip( |
| 57 | + 'Expired cert not installed. ' + |
| 58 | + 'Please follow setup instructions in test/system-ca/README.md', |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +describe('macOS certificate filtering', () => { |
| 63 | + it('includes self-signed cert with absent kSecTrustSettingsResult', () => { |
| 64 | + const noResultCert = fixtures.readKey('selfsigned-no-result-root-cert.pem'); |
| 65 | + assert.ok( |
| 66 | + includesCert(systemCerts, noResultCert), |
| 67 | + 'Self-signed cert with absent kSecTrustSettingsResult ' + |
| 68 | + '(defaulting to TrustRoot) should be in system CA list', |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it('deduplicates certificates from multiple keychains', () => { |
| 73 | + const target = extractMetadata(fakeStartcomCert); |
| 74 | + const matches = systemCerts.filter((c) => { |
| 75 | + const m = extractMetadata(c); |
| 76 | + return m.serialNumber === target.serialNumber && |
| 77 | + m.issuer === target.issuer && |
| 78 | + m.subject === target.subject; |
| 79 | + }); |
| 80 | + assert.strictEqual( |
| 81 | + matches.length, 1, |
| 82 | + `Expected exactly 1 copy of fake-startcom-root-cert, found ${matches.length}`, |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it('filters out expired certificates', () => { |
| 87 | + const expiredCert = fixtures.readKey('expired-root-cert.pem'); |
| 88 | + assert.ok( |
| 89 | + !includesCert(systemCerts, expiredCert), |
| 90 | + 'Expired certificate should not be in system CA list', |
| 91 | + ); |
| 92 | + }); |
| 93 | +}); |
0 commit comments