You've already forked GiteaToFeishuMsg
refactor signature verification in giteaWebhookHandler for improved accuracy and logging
This commit is contained in:
@@ -15,8 +15,31 @@ function verifySignature(req, secret) {
|
|||||||
}
|
}
|
||||||
const hmac = crypto.createHmac('sha256', secret);
|
const hmac = crypto.createHmac('sha256', secret);
|
||||||
hmac.update(JSON.stringify(req.body));
|
hmac.update(JSON.stringify(req.body));
|
||||||
const expected = `sha256=${hmac.digest('hex')}`;
|
const rawExpected = hmac.digest('hex'); // 64 hex chars
|
||||||
return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
|
// Extract raw hex from signature (strip algorithm prefix if present)
|
||||||
|
let rawSignature = signature;
|
||||||
|
if (signature.startsWith('sha256=')) {
|
||||||
|
rawSignature = signature.substring(7);
|
||||||
|
} else if (signature.startsWith('sha1=')) {
|
||||||
|
rawSignature = signature.substring(5);
|
||||||
|
}
|
||||||
|
// Ensure both are hex strings of length 64 (for SHA256) or 40 (for SHA1)
|
||||||
|
logger.debug('Signature verification', {
|
||||||
|
signature,
|
||||||
|
rawSignature,
|
||||||
|
rawExpected,
|
||||||
|
sigLen: rawSignature.length,
|
||||||
|
expLen: rawExpected.length
|
||||||
|
});
|
||||||
|
if (rawSignature.length !== rawExpected.length) {
|
||||||
|
logger.warn('Signature length mismatch', { rawSignatureLength: rawSignature.length, rawExpectedLength: rawExpected.length });
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Compare buffers (hex decoding)
|
||||||
|
return crypto.timingSafeEqual(
|
||||||
|
Buffer.from(rawSignature, 'hex'),
|
||||||
|
Buffer.from(rawExpected, 'hex')
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user