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,10 +11,18 @@ function isConfigured() {
|
|||||||
let _cachedPrivateKey = null
|
let _cachedPrivateKey = null
|
||||||
function getPrivateKey() {
|
function getPrivateKey() {
|
||||||
if (_cachedPrivateKey) return _cachedPrivateKey
|
if (_cachedPrivateKey) return _cachedPrivateKey
|
||||||
if (config.wxpay.privateKey) { _cachedPrivateKey = config.wxpay.privateKey; return _cachedPrivateKey }
|
let key = ''
|
||||||
if (config.wxpay.privateKeyPath) { _cachedPrivateKey = fs.readFileSync(config.wxpay.privateKeyPath, 'utf8'); return _cachedPrivateKey }
|
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')
|
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() {
|
function generateNonce() {
|
||||||
return crypto.randomBytes(16).toString('hex')
|
return crypto.randomBytes(16).toString('hex')
|
||||||
|
|||||||
在新工单中引用
屏蔽一个用户