Skip to content

Commit

Permalink
Update signature verification method to in line with the latest webho…
Browse files Browse the repository at this point in the history
…ok update from Ghost
  • Loading branch information
MFYDev authored Jul 6, 2024
1 parent c320dab commit 19d941c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ async function verifySignature(request) {
return false;
}

const [sigHash, timestamp] = signatureHeader.split(", ");
// Extract the hash and timestamp from the X-Ghost-Signature header
const [sigHash, timeStamp] = signatureHeader.split(", ");
const [, hash] = sigHash.split("=");
const [, ts] = timestamp.split("=");
const [, ts] = timeStamp.split("=");

const currentTime = Date.now();
// Check if the timestamp is within 5 minutes of the current time
if (Math.abs(currentTime - parseInt(ts)) > 5 * 60 * 1000) {
return false;
}
Expand All @@ -124,6 +126,9 @@ async function verifySignature(request) {

const body = await request.text();

// Create the message by concatenating the body and timestamp
const message = `${body}${ts}`;

const key = await crypto.subtle.importKey(
"raw",
new TextEncoder().encode(secret),
Expand All @@ -134,7 +139,7 @@ async function verifySignature(request) {
const signature = await crypto.subtle.sign(
"HMAC",
key,
new TextEncoder().encode(body)
new TextEncoder().encode(message)
);
const computedHash = Array.from(new Uint8Array(signature))
.map((b) => b.toString(16).padStart(2, "0"))
Expand Down

0 comments on commit 19d941c

Please sign in to comment.