fix: handle literal \n in WX_MCH_PRIVATE_KEY env var

- Replace literal '\n' with real newlines when reading private key from env
- Add PEM header validation to catch format errors early
- File-based key (privateKeyPath) unaffected
这个提交包含在:
Guoguo
2026-05-18 05:32:22 -07:00
父节点 c21a0912c6
当前提交 2e63213e60
+11 -3
查看文件
@@ -11,9 +11,17 @@ function isConfigured() {
let _cachedPrivateKey = null
function getPrivateKey() {
if (_cachedPrivateKey) return _cachedPrivateKey
if (config.wxpay.privateKey) { _cachedPrivateKey = config.wxpay.privateKey; return _cachedPrivateKey }
if (config.wxpay.privateKeyPath) { _cachedPrivateKey = fs.readFileSync(config.wxpay.privateKeyPath, 'utf8'); return _cachedPrivateKey }
throw new Error('WeChat Pay private key not configured')
let key = ''
if (config.wxpay.privateKey) {
key = config.wxpay.privateKey.replace(/\\n/g, '\n')
} else if (config.wxpay.privateKeyPath) {
key = fs.readFileSync(config.wxpay.privateKeyPath, 'utf8')
} else {
throw new Error('WeChat Pay private key not configured')
}
if (!key.includes('-----BEGIN')) throw new Error('Invalid private key format (missing PEM header)')
_cachedPrivateKey = key
return _cachedPrivateKey
}
function generateNonce() {