[英]HMAC Signature doesn't match x-hub-signature from github
我正在处理来自github的传入Webhook,并想验证x-hub签名。 我正在使用hmac
来哈希“秘密”,然后比较两个哈希。 问题在于它们永远不匹配。 这是我的设置:
router.route("/auth")
.post((req, res) => {
var hmac = crypto.createHmac("sha1", process.env.WEBHOOK_SECRET);
var calculatedSignature = "sha1=" + hmac.update(JSON.stringify(req.body)).digest("hex");
console.log(req.headers["x-hub-signature"] === calculatedSignature); // Returns false
console.log(req.headers["x-hub-signature"]) // => sha1=blablabla
console.log(calculatedSignature) // => sha1=foofoofoo
res.end();
});
我已经尝试了所有方法,但无法使其正常工作。 想知道hmac.update()
是否应包含JSON.stringify(req.body)
以外的其他参数。 有谁知道为什么他们不匹配?
因此问题出在网络挂钩的设置上。 内容格式设置为application / x-www-form-urlencoded,出于某种原因,它对x-hub签名进行了不同的哈希处理。 我只是将其更改为application / json,然后它起作用了!
如果将webhook的Content-Type设置为application/x-www-url-encoded
则需要使用字符串检查HMAC为"payload=" + query_encoded_payload
。
例如在golang
payloadForm := r.PostFormValue("payload")
escaped := url.QueryEscape(payloadForm) # ex. http://www.url-encode-decode.com/
checkMe := "payload=" + escaped
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.