minibear2021 / wechatpayv3

微信支付 API v3 Python SDK
MIT License
829 stars 130 forks source link

【BUG】在本地证书需要更新时,程序会因无法通过证书验证而陷入死循环 #9

Closed worship-dog closed 2 years ago

worship-dog commented 2 years ago

方法路径:wechatpayv3 > core.py > _verify_signature(self, headers, body)

方法源码:

    def _verify_signature(self, headers, body):
        signature = headers.get('Wechatpay-Signature')
        timestamp = headers.get('Wechatpay-Timestamp')
        nonce = headers.get('Wechatpay-Nonce')
        serial_no = headers.get('Wechatpay-Serial')
        cert_found = False
        for cert in self._certificates:
            if int('0x' + serial_no, 16) == cert.serial_number:
                cert_found = True
                certificate = cert
                break
        if not cert_found:
            self._update_certificates()
            for cert in self._certificates:
                if int('0x' + serial_no, 16) == cert.serial_number:
                    cert_found = True
                    certificate = cert
                    break
            if not cert_found:
                return False
        if not rsa_verify(timestamp, nonce, body, signature, certificate):
            return False
        return True

cert_foundFalse 时,程序将会执行 _update_certificates() 方法,在此更新证书的方法中,self._certificates 中的信息仍然错误,从而导致接下来的验证依旧不能通过,进而再次执行 _update_certificates() 方法,最终产生死循环

minibear2021 commented 2 years ago

已确认,请更新到1.0.9版。此修复有安全性bug,存在被精心设计的中间人攻击的可能,后续版本中拟加以完善。

worship-dog commented 2 years ago

好的,感谢您的修改与回复,期待更完善的sdk版本