双面方案:发送侧管签名、退避重试、多端点、per-host 熔断;接收侧管多端点验签、防重放、防时序攻击、队列处理。零第三方依赖,用原生 curl 实现。
安装
composer require wiseadmin/wise-webhook
php think migrate:runextra.think.services 注册 WebhookServiceProvider;配置发布为 config/wise-webhook.php;迁移建 webhook_sends、webhook_receives 两表。环境变量:GITHUB_WEBHOOK_SECRET、STRIPE_WEBHOOK_SECRET。
配置三段
| 段 | 关键项 |
|---|---|
server(发送侧) | dispatch.default_mode = 'sync'、signer、queue = 'webhooks'、retry.max_attempts = 3、retry.backoff_method = 'linear'、http.timeout = 15、verify_ssl = true、signature_header = 'X-Signature'、callbacks |
client(接收侧) | endpoints = []、defaults.signature_validator、processor_class = LogOnlyProcessor、queue = 'webhooks'、timeout = 30、tolerance_seconds = 300 |
circuit | failure_threshold = 5、timeout = 120、key_prefix = 'wise_webhook:cb' |
发送侧
use wise\webhook\server\WebhookServer;
WebhookServer::create()
->url('https://hooks.slack.com/services/T000/B000/XXXX')
->payload(['text' => 'Order #123 paid!'])
->useSecret('my-signing-secret')
->dispatch();
// ['success'=>true,'response'=>'ok','code'=>200,'duration_ms'=>128]WebhookCall 的链式方法:url()、payload()、useSecret()、signer()、method()、timeout()、maxRetries()、backoffStrategy()、withHeaders()、queue()、dispatch()、dispatchSync()、dispatchToQueue()、dispatchIf()、dispatchUnless()。门面入口 wise\webhook\facade\Webhook::serverCreate()。
dispatch() 与 dispatchToQueue() 的区别是"现在发"还是"排队发"。对外回调建议排队:对方站点慢的时候,同步发送会拖住你自己的请求。
接收侧
use wise\webhook\client\WebhookClient;
$result = WebhookClient::receiver()->receive($request, 'stripe');
// ['success' => bool, 'id' => ..., 'message' => ...]路由挂中间件:
Route::post('webhooks/:endpoint', 'webhook/Webhook@receive')
->middleware(\wise\webhook\client\middleware\VerifyWebhookSignature::class);内置实现:DefaultSigner(HMAC-SHA256 + X-Signature)、NoSigner、DefaultSignatureValidator(纯 hex / Stripe / GitHub 三种格式)、LogOnlyProcessor。签名比对用 hash_equals 防时序攻击,tolerance_seconds = 300 防重放。门面入口 wise\webhook\facade\Webhook::clientReceiver()。
receive() 内部做了四件事:二次验签、幂等(同端点同签名只处理一次)、落 webhook_receives(status = pending)、投队列。所以你的 Processor 不需要自己判重。
SSRF 防护:强制开启,没有开关
这是本篇最该记住的一条:SSRF 防护在代码里始终强制,不是配置项。
- 仅允许 http / https;
- 拒绝内网与保留 IP;
- 用
CURLOPT_RESOLVE固定解析结果,防 DNS 重绑定; - 重定向逐跳重新校验。
verifySsl(false) 只关 SSL 证书校验,不关 SSRF 防护。所以"内网测试环境要关掉 SSRF"这个需求无法通过配置满足——要么用可路由的测试地址,要么改代码(不推荐)。
三个容易搞错的认知
一、"notify 的 webhook 通道和这个包是一回事"。 不是:签名位置(query vs header)、HTTP 客户端(Guzzle vs curl)、落库表都不同,签名不能互验。对比见实战:订单通知与外部回调。
二、"忘了配 secret 也能收到回调"。 端点未配 signing_secret 会直接拒绝,属 fail-closed。
三、"失败会一直重试"。 max_attempts = 3 后停止,WebhookFailed 事件的 willRetry = false 才是你应该告警的时机。
自查:config/wise-webhook.php 存在;用一个本地接收端看签名头是否正确;用错误签名 POST 回调端点应被 VerifyWebhookSignature 拒绝。
框架侧概览见消息通知与 Webhook。