node.js - GitHub Webhook secret 从不验证

标签 node.js git github cryptography github-api

我正在使用 GitHub webhook 将事件通过管道传输到我的一个应用程序(GitHub 的 Hubot 的一个实例),它使用 sha1 key 进行保护。

我正在使用以下代码来验证传入 webhook 上的哈希值

crypto    = require('crypto')
signature = "sha1=" + crypto.createHmac('sha1', process.env.HUBOT_GITHUB_SECRET).update( new Buffer request.body ).digest('hex')
unless request.headers['x-hub-signature'] is signature
  response.send "Signature not valid"
  return

在 webhook 中传递的 X-Hub-Signature header 如下所示

X-Hub-Signature: sha1=1cffc5d4c77a3f696ecd9c19dbc2575d22ffebd4

我按照 GitHub 的文档准确地传递了 key 和数据,但哈希值总是不同。

这是 GitHub 的文档。 https://developer.github.com/v3/repos/hooks/#example

这是我最有可能误解的部分

secret: An optional string that’s passed with the HTTP requests as an X-Hub-Signature header. The value of this header is computed as the HMAC hex digest of the body, using the secret as the key.

谁能看出我哪里出错了?

最佳答案

似乎不适用于缓冲区,但 JSON.stringify();这是我的工作代码:

var
  hmac,
  calculatedSignature,
  payload = req.body;

hmac = crypto.createHmac('sha1', config.github.secret);
hmac.update(JSON.stringify(payload));
calculatedSignature = 'sha1=' + hmac.digest('hex');

if (req.headers['x-hub-signature'] === calculatedSignature) {
  console.log('all good');
} else {
  console.log('not good');
}

关于node.js - GitHub Webhook secret 从不验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25767805/

相关文章:

javascript - 在 Angular2 应用程序中运行 Node-js 代码

git - 我如何使用 gitstats 找出一个 Git 仓库总共有多少个 SLOC 以及每个提交者?

git - 在不创建 .gitignore 文件的情况下使用 reposurgeon 将 SVN 存储库转换为 git?

git - 使用 sublimerge 在 3-way diff 中启用编辑模式

continuous-integration - GitHub、Gerrit、Hudson(Jenkins) 工作流程

javascript - Node.js HTTP GET “ECONNRESET” 读取错误

javascript - 单击 RXjs 按钮未得到服务器响应

node.js - 如何在nodeJS中将.doc文件读取/重写为xml?

python - 使用带有 key 的 github

javascript - Lint 和 Prettier 存储库没有大规模合并冲突