ハウツーガイド - Webhookイベントの保護
Webhook署名の検証は、悪意のあるアクターによるイベントの偽装を防ぎ、改ざんされたWebhookをコネクターが拒否できるようにするための推奨プラクティスです。 既知の署名検証方法は多数あります。通常は、Webhookイベントから署名を作成し、それをWebhookのヘッダー内の署名と照合します。 このガイドでは、HubSpotのWebhook V1署名を使用して、それを行う簡単な例を1つ示します。
TIP
このガイドは、静的または動的なWebhookトリガーの作成に関する基本を理解していることを前提としています。 まだ確認していない場合は、これらのガイドを確認してください。
サンプルコネクター - HubSpot Webhooks
webhook_payload_type: "raw", # Workato does a JSON.parse on incoming webhooks but we need to calculate the signature based on the raw payload
webhook_notification: lambda do |input, payload, extended_input_schema, extended_output_schema, headers, params, connection, webhook_subscribe_output|
original_payload = payload
client_secret = connection['client_secret']
if client_secret.present?
source_string = client_secret + original_payload # Build the string to SHA256 which is a concatenation of client secret + payload
v1_signature = source_string.encode_sha256.encode_hex
end
# If condition below verifies that the signature we calculated is the same as the X-Hubspot-Signature we got in the webhook event
if (client_secret.present? && v1_signature == headers['X-Hubspot-Signature'])
# Don't forget to parse the payload into JSON as we dictated that the payload would be `raw`
{
events: workato.parse_json(payload),
headers: headers,
webhook_validated: client_secret.present? ? true : false
}
end
end,ステップ1 - 必要に応じたwebhook_payload_typeのrawへの設定
WorkatoのWebhookゲートウェイは、受信ペイロードを常にJSONとして解析しようとします。 場合によっては、これによりペイロードの詳細の一部が失われ、不正な署名が作成される可能性があります。 これを回避するには、キーwebhook_payload_typeを使用して、Workatoが未加工のペイロードをwebhook_notificationラムダに提供するように強制できます。
ステップ2 - Webhook署名の計算
Webhookの真正性を検証するうえで、もう1つ重要な点は、受信したWebhookイベントから独自のWebhook署名を計算することです。 これは多くの場合、ペイロードと、自分およびWebhookプロバイダーのみが知っているシークレットを使用して、SHA256やHMACアルゴリズムなどの暗号化アルゴリズムによって行われます。
original_payload = payload
client_secret = connection['client_secret']
if client_secret.present?
source_string = client_secret + original_payload # Build the string to SHA256 which is a concatenation of client secret + payload
v1_signature = source_string.encode_sha256.encode_hex
endHubSpotの場合、ペイロードとクライアントシークレットから暗号化するキーを作成してから、SHA256で暗号化します。
ステップ3 - 生成された署名とWebhookイベントで提供された署名の比較
次のステップでは、ステップ2で生成した署名を、Webhookイベントに存在する署名と比較します。 通常、これはWebhookイベントのヘッダーに含まれており、webhook_notificationラムダでアクセスできます。
# If condition below verifies that the signature we calculated is the same as the X-Hubspot-Signature we got in the webhook event
if (client_secret.present? && v1_signature == headers['X-Hubspot-Signature'])
# Don't forget to parse the payload into JSON as we dictated that the payload would be `raw`
{
events: workato.parse_json(payload),
headers: headers,
webhook_validated: client_secret.present? ? true : false
}
endLast updated: