【问题标题】:Github push event signature don't matchGithub 推送事件签名不匹配
【发布时间】: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


【解决方案1】:

default encoding of Crypto hash.update() is binary 的详细信息请参见Node JS crypto, cannot create hmac on chars with accents 的答案。这会导致您的推送事件有效负载出现问题,其中包含字符 U+00E1 LATIN SMALL LETTER A WITH ACUTEHernández 中四次,并且 GitHub 服务将有效负载散列为 utf-8 编码.请注意,您的 Gist 在 ISO-8859-1 中显示了这些错误编码,因此还要确保您正确处理传入的请求字符编码(但这应该默认发生)。

要解决此问题,您需要使用Buffer

hmac = crypto.createHmac('sha1', tok).update(new Buffer(blob, 'utf-8')).digest('hex');

...或者直接把编码传给update:

hmac = crypto.createHmac('sha1', tok).update(blob, 'utf-8').digest('hex');

然后将计算7f9e6014b7bddf5533494eff6a2c71c4ec7c042d 的正确哈希。

【讨论】:

  • 好的。要去尝试 :) 现在很有意义!
  • 标志应该是utf8 而不是utf-8
  • @JosephLust 谢谢,也许,但两者都应该可以正常工作 - console.log(Buffer.isEncoding('utf-8'));真正的 console.log(Buffer.isEncoding('utf8'));真的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 2020-10-08
  • 2014-02-09
相关资源
最近更新 更多