【发布时间】:2015-07-27 22:34:21
【问题描述】:
我正在编写Webhook for GitHub,并在KOA.js 中实现了安全验证:
function sign(tok, blob) {
var hmac;
hmac = crypto
.createHmac('sha1', tok)
.update(blob)
.digest('hex');
return 'sha1=' + hmac;
}
...
key = this.request.headers['x-hub-signature'];
blob = JSON.stringify(this.request.body);
if (!key || !blob) {
this.status = 400;
this.body = 'Bad Request';
}
lock = sign(settings.api_secret, blob);
if (lock !== key) {
console.log(symbols.warning, 'Unauthorized');
this.status = 403;
this.body = 'Unauthorized';
return;
}
...
对于 pull_requests 和 create events,这可以正常工作,即使推送新分支也可以,但是对于推送提交事件,x-hub-signature 和来自有效负载的计算哈希不匹配,因此它总是得到 403 未授权。
更新
我注意到,对于这种推送负载,提交和 head_commit 被添加到负载中。我已尝试从正文中删除提交和 head_commit,但没有奏效。
更新
有关更多信息,请查看这些示例有效负载。我还包含了测试 repo 和令牌信息的 url:https://gist.github.com/marcoslhc/ec581f1a5ccdd80f8b33
【问题讨论】:
-
“惨遭失败”是什么意思?你得到什么错误?
-
“x-hub-signature”不等于计算的哈希值。
-
在黑暗中彻底刺伤,但你确定
this.request.body还不是一个字符串吗?如果是,它将被双重编码(例如,三个字符串foo将被 JSON 编码为五个字符串"foo")。看看typeof this.request.body产生了什么。 -
您能否提供一个有问题的事件有效负载示例(来自事件 API)、
x-hub-signature的值、您计算的 HMAC 和密钥?显然,这需要来自测试存储库。 -
您可以在此处查看示例数据以及令牌详细信息:gist.github.com/marcoslhc/ec581f1a5ccdd80f8b33
标签: javascript github webhooks github-api