๐ Securing Webhooks with Nginx + njs (SHA-256 HMAC)#
A production-ready guide to validating incoming webhooks at the Nginx layer using njs (Nginx JavaScript) and HMAC SHA-256.
๐ Overview#
Only trusted webhook sources are accepted
Payload integrity is verified using HMAC
Unauthorized or tampered requests are rejected before reaching your backend
๐งฑ Architecture Flow#
Webhook Request โ Nginx (njs validation) โ Backend App
โ Reject invalid
โ
Forward valid
โ๏ธ Requirements#
Nginx compiled with ngx_http_js_module (njs)
Backend application (e.g., Laravel running on 127.0.0.1:8000)
๐ Step 1: Create njs Validation Script#
/etc/nginx/webhook_auth.js
๐ Security Best Practices#
โ
Use Constant-Time Comparison#
Use a constant-time comparison method (if available in your njs version) to prevent timing attacks instead of:
โ
Store Secrets Securely#
Avoid hardcoding secrets. Use:Secret managers (e.g., Vault, AWS Secrets Manager)
โ
Enable HTTPS#
โ
Rate Limiting#
๐งช Testing#
Generate Signature (Node.js)#
Test with cURL#
โ Common Errors#
| Error | Cause |
|---|
| Missing Security Headers | Headers not sent |
| Invalid Store ID | Unknown store |
| Empty Payload | No request body |
| Invalid Signature | Signature mismatch |
๐ Summary#
Validation is handled at the Nginx layer
Protects backend from malicious requests
Uses HMAC SHA-256 for verification
High performance and scalable
Modified atย 2026-03-28 08:38:30