|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import debugLib from 'debug'; |
| 4 | +import { BitGoTlsPolicy, createBitGoTlsPolicy, getCaExpiries } from 'bitgo'; |
| 5 | +import { Config } from './config'; |
| 6 | +import { TlsConfigurationError } from './errors'; |
| 7 | + |
| 8 | +const debug = debugLib('bitgo:express'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Official BitGo domain patterns covered by the default CA bundle restriction. |
| 12 | + * Covers apex and all subdomains (`app.*`, `admin.*`, `cdn.*`, etc.) across prod, test, staging, and dev. |
| 13 | + */ |
| 14 | +export const BITGO_DEFAULT_TLS_HOSTS: string[] = [ |
| 15 | + 'bitgo.com', |
| 16 | + '.bitgo.com', |
| 17 | + 'bitgo-test.com', |
| 18 | + '.bitgo-test.com', |
| 19 | + 'bitgo-staging.com', |
| 20 | + '.bitgo-staging.com', |
| 21 | + 'bitgo-dev.com', |
| 22 | + '.bitgo-dev.com', |
| 23 | +]; |
| 24 | + |
| 25 | +function resolveBundledCaPath(): string { |
| 26 | + const candidates = [path.resolve(__dirname, '../bitgo-ca.pem'), path.resolve(__dirname, '../../bitgo-ca.pem')]; |
| 27 | + const found = candidates.find((p) => fs.existsSync(p)); |
| 28 | + if (!found) { |
| 29 | + throw new TlsConfigurationError(`Bundled BitGo CA file not found (searched: ${candidates.join(', ')})`); |
| 30 | + } |
| 31 | + return found; |
| 32 | +} |
| 33 | + |
| 34 | +// Read the baked-in bundle once at module load so we don't re-read from disk on every config |
| 35 | +// or collide with unit tests that stub fs.readFileSync for signer config files. |
| 36 | +const BUNDLED_CA_PEM = fs.readFileSync(resolveBundledCaPath(), 'utf8'); |
| 37 | + |
| 38 | +const policies = new WeakMap<Config, BitGoTlsPolicy | undefined>(); |
| 39 | + |
| 40 | +/** |
| 41 | + * Logs the policy summary to debug, plus a console.warn per bundle CA expiring within 30 days, so an operator |
| 42 | + * can update Express before a bundled root CA expiry breaks connections. |
| 43 | + */ |
| 44 | +function logPolicyStatus(hosts: string[], ca: string | Buffer | Array<string | Buffer>): void { |
| 45 | + const expiries = getCaExpiries(ca); |
| 46 | + debug('BitGo TLS policy enabled for %d host pattern(s), %d CA(s)', hosts.length, expiries.length); |
| 47 | + for (const expiry of expiries) { |
| 48 | + if (expiry.daysRemaining < 30) { |
| 49 | + const when = |
| 50 | + expiry.daysRemaining < 0 |
| 51 | + ? `expired ${-expiry.daysRemaining} day(s) ago` |
| 52 | + : `expires in ${expiry.daysRemaining} day(s)`; |
| 53 | + console.warn( |
| 54 | + `BitGo TLS policy CA '${ |
| 55 | + expiry.subject |
| 56 | + }' ${when} (${expiry.validTo.toISOString()}); update @bitgo/express to refresh the bundled CA roots` |
| 57 | + ); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Builds (once per config) the default outbound TLS policy for BitGo hostnames using the bundled multi-provider |
| 64 | + * CA store (`bitgo-ca.pem`), or returns undefined when disabled via `disablePinCerts` (`--disablepincerts` / |
| 65 | + * `BITGO_DISABLE_PIN_CERTS`) or when `BITGO_USE_PROXY` is set. |
| 66 | + */ |
| 67 | +export function getBitGoTlsPolicy(config: Config, caBundlePath?: string): BitGoTlsPolicy | undefined { |
| 68 | + if (!caBundlePath && policies.has(config)) { |
| 69 | + return policies.get(config); |
| 70 | + } |
| 71 | + |
| 72 | + if (config.disablePinCerts) { |
| 73 | + policies.set(config, undefined); |
| 74 | + return undefined; |
| 75 | + } |
| 76 | + |
| 77 | + if (process.env.BITGO_USE_PROXY) { |
| 78 | + console.warn( |
| 79 | + 'warning: BitGo TLS CA pinning is skipped because BITGO_USE_PROXY is set; the proxy agent owns the TLS connection.' |
| 80 | + ); |
| 81 | + policies.set(config, undefined); |
| 82 | + return undefined; |
| 83 | + } |
| 84 | + |
| 85 | + let policy: BitGoTlsPolicy; |
| 86 | + try { |
| 87 | + const ca = caBundlePath ? fs.readFileSync(caBundlePath) : BUNDLED_CA_PEM; |
| 88 | + policy = createBitGoTlsPolicy({ ca, hosts: BITGO_DEFAULT_TLS_HOSTS }); |
| 89 | + logPolicyStatus(BITGO_DEFAULT_TLS_HOSTS, ca); |
| 90 | + } catch (err) { |
| 91 | + throw err instanceof TlsConfigurationError ? err : new TlsConfigurationError((err as Error).message); |
| 92 | + } |
| 93 | + |
| 94 | + if (!caBundlePath) { |
| 95 | + policies.set(config, policy); |
| 96 | + } |
| 97 | + return policy; |
| 98 | +} |
0 commit comments