Postmark Webhook 签名安全

Postmark Webhook Signature Security

提问人:haumich 提问时间:9/13/2022 最后编辑:haumich 更新时间:9/16/2022 访问量:323

问:

https://postmarkapp.com/developer/webhooks/webhooks-overview

https://<username>:<password>@example.com/webhook

查看 Postmark API,似乎没有任何关于 webhook 签名或令牌或 hmac 安全的信息。唯一提到的安全性是基本身份验证或防火墙。

您如何设置基本身份验证或防火墙以使用 Postmark webhook?在nginx或apache上有什么需要做的吗?

像这样的基本身份验证?

requests.post('url',headers=headers,auth=('username','password'), json=json_data)

username:[email protected]

对我来说,这似乎不如签名验证安全。其他 API(如 Mailgun)具有签名和令牌,以及用于验证请求标头中的 Webhook 的 hmac。

Mailgun 示例:

https://documentation.mailgun.com/en/latest/user_manual.html#webhooks-1

import hashlib, hmac, json
def verify(signing_key, token, timestamp, signature):
    hmac_digest = hmac.new(key=signing_key.encode(),
                           msg=('{}{}'.format(timestamp, token)).encode(),
                           digestmod=hashlib.sha256).hexdigest()
    return hmac.compare_digest(str(signature), str(hmac_digest))


@csrf_exempt
def mailgun_webhook(request):
  body_unicode = request.body.decode('utf-8')
  body = json.loads(body_unicode)
  signature = body['signature']['signature']
  token = body['signature']['token']
  timestamp = body['signature']['timestamp']
  webhook_signing_key = 'KEY'
  if verify(webhook_signing_key, token, timestamp, signature) is True:
    print('do something')
  return HttpResponse(status=200)
python django api webhooks 邮戳

评论


答:

1赞 dxtime 9/16/2022 #1

你是对的,邮戳仅支持基本身份验证。他们进一步建议使用防火墙规则,因为它们可以为您提供静态 IP 以添加到允许列表中。

让我们探讨如何使用防火墙规则和 Nginx 的基本身份验证来做到这一点。

基本身份验证

您可以在应用程序服务器或 Web 服务器中实现基本身份验证。

对于 nginx,您可以在此处找到有关该主题的文档 https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-http-basic-authentication/

您的 nginx 配置可能如下所示:

server {
    ...

    location /webhooks {
        auth_basic           "Webhook Area";
        auth_basic_user_file /etc/apache2/.htpasswd; 
        ...
    }
}

在 Postmark 中,您可以按照其建议的格式创建一个 Webhook 端点:其中用户名和密码与文件中的内容匹配。https://<username>:<password>@yoursite.com/webhooks.htpasswd

防火墙

此选项之所以可行,是因为 Postmark 保证其 webhook 将从一组静态 IP 发送。

在实施方面,防火墙因您运行的基础架构类型而有很大差异。是否有专门的应用程序主要负责处理 Webhook?你有 Web 服务器 (nginx) 代理吗?您是否在使用 WAF?

要再次以 Nginx 为例,按照上一节中提供的相同链接,您可以找到讨论“IP 地址访问限制”的部分。

它可能看起来像:

server {
    ...

    location /webhooks {
        #...
        allow 3.134.147.250;
        allow 50.31.156.6;
        allow 50.31.156.77;
        allow 18.217.206.57;
        deny  all;
        ...
    }
}

您可以查阅邮戳文档以获取最新的 IP https://postmarkapp.com/support/article/800-ips-for-firewalls#webhooks 列表。

如果您使用的是 WAF,它们还应该支持基于 IP 的规则,例如 Cloudflare WAF https://developers.cloudflare.com/waf/tools/ip-access-rules/