尝试模拟github webhook请求,得到:" X-Hub-Signature与blob签名不匹配"

时间:2017-06-30 16:09:29

标签: node.js github webhooks hmac cryptojs

这是一个处理github webhooks的小代理服务器设置:

require('dotenv').config();
var http = require('http');
var createHandler = require('github-webhook-handler');
var handler = createHandler({
  path: '/webhook',
  secret: process.env.GIT_WEBHOOK_SECRET
});

http
  .createServer(function(req, res) {
    handler(req, res, function(err) {
      res.statusCode = 404;
      res.end('no such location');
    });
  })
  .listen(8080);

handler.on('error', function(err) {
  console.error('Error:', err.message);
});

handler.on('push', function(event) {
  console.log(
    'Received a push event for %s to %s',
    event.payload.repository.name,
    event.payload.ref
  );
});

handler.on('issues', function(event) {
  console.log(
    'Received an issue event for %s action=%s: #%d %s',
    event.payload.repository.name,
    event.payload.action,
    event.payload.issue.number,
    event.payload.issue.title
  );
});

在邮差中,我设置了这些标题:

Headers

原始身体在这里:https://developer.github.com/v3/activity/events/types/#pullrequestreviewevent

这是我的预请求脚本:

var payload = request.data;
console.log("Using payload as " + payload)
var hash = CryptoJS.HmacSHA1(payload, environment.secret).toString(CryptoJS.enc.Hex)
postman.setGlobalVariable("signature", hash);

我可以确认.env中的GIT_WEBHOOK_SECRET与Postman环境设置中secret中设置的内容相同。

1 个答案:

答案 0 :(得分:2)

您需要将X-Hub-Signature的内容设置为sha1字段的参数:

var payload = request.data;
console.log("Using payload as " + payload)
var hash = CryptoJS.HmacSHA1(payload, environment.secret).toString(CryptoJS.enc.Hex)
postman.setGlobalVariable("signature", "sha1=" + hash);

来自validating payloads from Github

  

无论您使用哪种实现,哈希签名都以   sha1 =,使用您的秘密令牌和您的有效负载体的密钥。