How to Validate GitHub Webhooks Using HMAC SHA-256 in PHP and Node.js
GitHub webhooks can be exploited if endpoints blindly trust incoming payloads, making signature verification a critical security step. When GitHub dispatches a webhook, it computes an HMAC SHA-256 signature using the raw request body and a shared secret, sending the result in the X-Hub-Signature-256 header. Developers must recalculate this signature server-side from the raw body and compare it against the header value using constant-time comparison functions to prevent timing attacks. In PHP, hash_hmac() and hash_equals() handle this securely, while Node.js offers createHmac() and timingSafeEqual() from the native crypto module. A key implementation detail is that validation must use the raw request body before any JSON parsing, since even minor byte-level changes will produce a different HMAC and cause legitimate requests to fail.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in