简体   繁体   English

为什么我的 Github 密码失败(每个事件都更改)?

[英]Why does my Github secret fail (change on each event)?

I am trying to verify the Github webhook secret, but each time I trigger an event in Github, the value of req.headers['x-hub-signature']) changes, which doesn't make sense.我正在尝试验证 Github webhook 密码,但每次我在 Github 中触发事件时, req.headers['x-hub-signature'])的值都会发生变化,这没有任何意义。

NodeJs: sha1=b57ad18e45f71ac069d15618f6ca547ed75bb2e9
Github: sha1=0b6ff08d557b240dbadedb2a0c1054ce69f2d93e    <----

NodeJs: sha1=b57ad18e45f71ac069d15618f6ca547ed75bb2e9
Github: sha1=15e3d5edae00951abb180e9eaea9a6278d8f8d0b    <----

Notice the secret that comes from Githit hub is different each time!请注意,来自 Githit hub 的秘密每次都不同!

I have found others that verify the secret, but I don't see how their code is different from mine.我发现其他人验证了这个秘密,但我看不出他们的代码与我的代码有何不同。

Question问题

Can anyone figure out why I get different secrets from Github on each event?谁能弄清楚为什么我在每个事件中都从 Github 得到不同的秘密? Or am I doing something wrong?还是我做错了什么?

const express = require("express");
const bodyParser = require("body-parser");
const crypto = require('crypto');
const secret = "x";

const app = express();
const PORT = 8080;

app.use(bodyParser.json());

app.post("/", (req, res) => {

let sig = "sha1=" + crypto.createHmac('sha1', secret).digest('hex');

  console.log('NodeJs: ' + sig);
  console.log('Github: ' + req.headers['x-hub-signature']);
    
  res.status(200).end();
});

app.listen(PORT, () => console.log(`Github wekhook listening on port ${PORT}`));

req.headers['x-hub-signature']) is not a hash of the secret, but req.body signed with the secret. req.headers['x-hub-signature'])不是秘密的 hash,而是用秘密签名的req.body That is why it is different on each event.这就是为什么每个事件都不同的原因。

const express = require("express");
const bodyParser = require("body-parser");
const crypto = require('crypto');
const secret = "x";

const app = express();
const PORT = 8080;

app.use(bodyParser.json());

function isSigOk(request, secret) {
    // calculate the signature
    const expectedSignature = "sha1=" +
        crypto.createHmac("sha1", secret)
            .update(JSON.stringify(request.body))
            .digest("hex");

    // compare the signature against the one in the request
    const signature = request.headers["x-hub-signature"];
    if (signature !== expectedSignature) {
        throw new Error("Invalid signature.");
    };
};

app.post("/", (req, res) => {
  // will throw an error if not ok
  isSigOk(req, secret);

  // Do stuff here

  res.status(200).end();
});

app.listen(PORT, () => console.log(`Github wekhook listening on port ${PORT}`));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM