mirror of
https://github.com/github/codeql-action.git
synced 2025-12-27 01:30:10 +08:00
66 lines
1.8 KiB
JavaScript
66 lines
1.8 KiB
JavaScript
// pkg/dist-src/node/sign.js
|
|
import { createHmac } from "node:crypto";
|
|
|
|
// pkg/dist-src/version.js
|
|
var VERSION = "5.1.1";
|
|
|
|
// pkg/dist-src/node/sign.js
|
|
async function sign(secret, payload) {
|
|
if (!secret || !payload) {
|
|
throw new TypeError(
|
|
"[@octokit/webhooks-methods] secret & payload required for sign()"
|
|
);
|
|
}
|
|
if (typeof payload !== "string") {
|
|
throw new TypeError("[@octokit/webhooks-methods] payload must be a string");
|
|
}
|
|
const algorithm = "sha256";
|
|
return `${algorithm}=${createHmac(algorithm, secret).update(payload).digest("hex")}`;
|
|
}
|
|
sign.VERSION = VERSION;
|
|
|
|
// pkg/dist-src/node/verify.js
|
|
import { timingSafeEqual } from "node:crypto";
|
|
import { Buffer } from "node:buffer";
|
|
async function verify(secret, eventPayload, signature) {
|
|
if (!secret || !eventPayload || !signature) {
|
|
throw new TypeError(
|
|
"[@octokit/webhooks-methods] secret, eventPayload & signature required"
|
|
);
|
|
}
|
|
if (typeof eventPayload !== "string") {
|
|
throw new TypeError(
|
|
"[@octokit/webhooks-methods] eventPayload must be a string"
|
|
);
|
|
}
|
|
const signatureBuffer = Buffer.from(signature);
|
|
const verificationBuffer = Buffer.from(await sign(secret, eventPayload));
|
|
if (signatureBuffer.length !== verificationBuffer.length) {
|
|
return false;
|
|
}
|
|
return timingSafeEqual(signatureBuffer, verificationBuffer);
|
|
}
|
|
verify.VERSION = VERSION;
|
|
|
|
// pkg/dist-src/index.js
|
|
async function verifyWithFallback(secret, payload, signature, additionalSecrets) {
|
|
const firstPass = await verify(secret, payload, signature);
|
|
if (firstPass) {
|
|
return true;
|
|
}
|
|
if (additionalSecrets !== void 0) {
|
|
for (const s of additionalSecrets) {
|
|
const v = await verify(s, payload, signature);
|
|
if (v) {
|
|
return v;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
export {
|
|
sign,
|
|
verify,
|
|
verifyWithFallback
|
|
};
|