安全验证 Webhook 签名
通过验证 Stripe Webhook 签名并遵循安全的端点实践,保护支付后端免受伪造事件的攻击。
安全验证 Webhook 签名 是 CoddyKit 上的免费 Stripe Payments & SaaS Billing Systems 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Stripe Payments & SaaS Billing Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Stripe Payments & SaaS Billing Systems 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Verify Webhooks?
Your webhook endpoint is public. Without verification, an attacker could POST a fake payment_succeeded event and unlock paid features for free.
The Signing Secret
Each webhook endpoint has a signing secret (starts with whsec_). Stripe uses it to sign every event it sends you.
The Stripe-Signature Header
Every webhook request carries a Stripe-Signature header containing a timestamp and an HMAC signature of the payload.
// Stripe-Signature: t=1700000000,v1=5257a8...Use the Raw Body
Signature verification needs the exact raw request body. If a framework parses JSON first, the bytes change and verification fails.
app.post('/webhook',
express.raw({ type: 'application/json' }),
handler
);Verifying with the SDK
The Stripe SDK does the HMAC math for you via constructEvent.
function verify(rawBody, sig, secret, stripe) {
return stripe.webhooks.constructEvent(rawBody, sig, secret);
}Handling Verification Failure
If verification throws, reject the request with a 400. Never process an unverified event.
function process(ok) {
if (!ok) return { status: 400, body: 'invalid signature' };
return { status: 200, body: 'received' };
}
console.log(process(false));Timestamp Tolerance
The signature includes a timestamp. Stripe rejects events older than a tolerance window to block replay attacks with captured payloads.
Constant-Time Comparison
Under the hood, signatures are compared in constant time to avoid timing attacks. The SDK handles this; never hand-roll a simple equality check.
Respond Fast, Process Later
Acknowledge with 200 quickly, then do heavy work asynchronously. Slow responses make Stripe retry and can cause duplicates.
Keep the Secret Safe
Store the signing secret in environment variables, never in source control. Rotate it if it leaks, using the dashboard.
const secret = process.env.STRIPE_WEBHOOK_SECRET;
console.log(Boolean(secret));Per-Endpoint Secrets
Each registered endpoint has its own secret. Use the correct one for the URL receiving the event, especially across test and live modes.
Quick Check
Why must you use the raw request body for verification?
Recap
You secured your webhook endpoint:
- Verify the Stripe-Signature with the signing secret
- Use the raw body and the SDK constructEvent
- Reject failures and rely on timestamp tolerance against replays
- Keep secrets in env vars and respond fast
常见问题解答
「安全验证 Webhook 签名」课时是免费的吗?
是的 — 「安全验证 Webhook 签名」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Stripe Payments & SaaS Billing Systems 课程的其余内容,请升级到 CoddyKit PRO。 Stripe Payments & SaaS Billing Systems 课程共包含 4 节课。
「安全验证 Webhook 签名」这节课中我会学到什么?
通过验证 Stripe Webhook 签名并遵循安全的端点实践,保护支付后端免受伪造事件的攻击。 你通过在浏览器中直接运行的动手代码来练习 Stripe Payments & SaaS Billing Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Stripe Payments & SaaS Billing Systems 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Stripe Payments & SaaS Billing Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「安全验证 Webhook 签名」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Stripe Payments & SaaS Billing Systems 课中编写并运行代码吗?
能。每节 Stripe Payments & SaaS Billing Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 安全存储支付方式(令牌)
- 实现强客户身份验证(SCA)
- 面向开发者的 PCI 合规最佳实践
- 安全验证 Webhook 签名