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
这个提交包含在:
+10
-2
@@ -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 }
|
||||
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() {
|
||||
|
||||
在新工单中引用
屏蔽一个用户