NAME

Twilio::Security::RequestValidator - Check that a webhook really came from Twilio

SYNOPSIS

use Twilio::Security::RequestValidator;

my $validator = Twilio::Security::RequestValidator->new($auth_token);

# A form post: pass its parameters
my $is_from_twilio = $validator->validate($signature, $url, \%params);

# A JSON post: pass its raw body
my $is_from_twilio = $validator->validate($signature, $url, $raw_body);

# A webhook signed with a SharedKey
my $algorithm = Twilio::Security::RequestValidator->get_signature_algorithm(
  $settings_header
);
my $validator = Twilio::Security::RequestValidator->new($shared_key_secret);
my $is_from_twilio = $validator->validate($signature, $url, \%params, $algorithm);

DESCRIPTION

Anyone who finds the URL of a webhook can send it a request, so an application should check each one before trusting it. Twilio signs its requests, and puts the signature in the X-Twilio-Signature header.

$url must be the full URL Twilio called, as Twilio knows it, including the query string. Behind a proxy, that is not always the URL your server sees.

Keys and values may be given as characters or as UTF-8 bytes. A value can be an array reference, for a parameter that came more than once.

METHODS

new

my $validator = Twilio::Security::RequestValidator->new($secret);

$secret is the auth token of the account, or the SharedKey secret when the request has an X-Twilio-Webhooks-Signature-Settings header.

validate

my $valid = $validator->validate($signature, $url, $data, $algorithm);

Returns 1 when the signature is right, and 0 otherwise. $data is a hash reference of parameters, or the raw body as a string. A body must match the bodySHA256 parameter in the URL. The signature is accepted with or without the port in the URL, because Twilio is not consistent about it.

$algorithm is HMAC_SHA1, which is the default, or HMAC_SHA256. One that this library does not know gives a warning and returns 0.

compute_signature

my $signature = $validator->compute_signature($url, \%params, $algorithm);

Returns the signature of a request. It throws Twilio::Exceptions::TwilioException when it does not know the algorithm.

compute_body_hash

my $hash = Twilio::Security::RequestValidator->compute_body_hash($body);

Returns the SHA-256 hash of a body, in hexadecimal.

compare

my $same = Twilio::Security::RequestValidator->compare($one, $other);

Returns 1 when two non-empty strings are the same. It takes the same time wherever they differ, so that the time cannot be used to guess a signature.

get_signature_algorithm

my $algorithm = Twilio::Security::RequestValidator->get_signature_algorithm($header);

Returns the algorithm named in the X-Twilio-Webhooks-Signature-Settings header, or HMAC_SHA1 when there is no such header, or it does not name one.

SIGNATURE_HEADER

SIGNATURE_SETTINGS_HEADER

HMAC_SHA1

HMAC_SHA256

Return the names of the two headers and of the two algorithms.

LICENSE

Copyright (c) 2026 Francisco Zarabozo. Released under the MIT License.