Verifying Amazon SNS Messages with PHP
Messages sent by Amazon Simple Notification Service are signed, and checking that any received message is indeed from AWS and not from some douche trying to outsmart you is not very hard (nor should it be optional, for that matter): sns-verify.php Theverify_sns()
function expects the message in JSON format, plus region (e.g. "eu-west-1"), numerical account ID without dashes and an array containing the topics you're interested in. The code will verify both SubscriptionConfirmation
and Notification
messages. It loads the certificate from the address in SigningCertURL
field to check against for each message separately because the certificate changes over time, as described here. It is also checked that the host where the certificate is loaded from is in the amazonaws.com domain.
Example usage where subscriptions are automatically confirmed:
require_once('sns-verify.php'); if($_SERVER['REQUEST_METHOD'] != 'POST') { logger('Not POST request, quitting'); exit(); } $post = file_get_contents('php://input'); if(!verify_sns($post, 'REGION', 'ACCOUNT', array('TOPIC 1', 'TOPIC 2'))) { exit; } $msg = json_decode($post); if($msg->Type == 'SubscriptionConfirmation') { logger('SNS SubscriptionConfirmation received); file_get_contents($msg->SubscribeURL); } elseif($msg->Type == 'Notification') { logger('SNS Notification received); process_message($msg); }
Categorised as: snippet