Are you implementing an HTTP/HTTPS endpoint for SNS? If so, you should definetly verify the incoming messages. Otherwise, anyone on the Internet can deliver messages to your HTTP/HTTPS endpoint. Which is a security risk.
How do you verify incoming messages? The SNS documentation answers this question:
You should verify the authenticity of a notification, subscription confirmation, or unsubscribe confirmation message sent by Amazon SNS.
In a nutshell, each SNS message contains a signature that we have to verify.
The npm module sns-validator does the job. Unfortunately, the module is old and lacks support for save caching and certificate download retries. Therefore, I decided to implement this on my own, which wasn’t as hard as expected. Let’s get started.
First, you need to install a few dependencies:
requestandrequestretryto perform HTTP(S) requests with retrieslru-cacheto safely cache certificates without running out of memory
Install the modules with:
npm i request requestretry lru-cache |
Create a new JavaScript file (e.g., index.js) and import the dependencies we need:
const crypto = require('crypto'); |
According to the SNS documentation, we have to use different fields of the message based on the Type of the message delivered by SNS.
function fieldsForSignature(type) { |
We also have to come up with a way to download the certificate that we need to verify the signature. The certificate is attached to the message in the form of a URL. We have to download the certificate before we can verify the signature. Downloading things can fail for many reasons. Therefore, we retry failed download requests. To optimize for performance, we also want to cache downloaded certificates. Let’s look at the code.
const CERT_CACHE = new LRU({max: 5000, maxAge: 1000 * 60}); |
The cache stores a maximum of 5000 certificates and the certificates expire after 1 minute from the cache.
Last but not least, we do some input validation:
- the fields
SignatureVersion,SigningCertURL,Type, andSignaturemust be available SignatureVersionmust be1- the
SigningCertURLmust start withhttps://and we only want to download certificates from AWS
const CERT_URL_PATTERN = /^https://sns.[a-zA-Z0-9-]{3,}.amazonaws.com(.cn)?/SimpleNotificationService-[a-zA-Z0-9]{32}.pem$/; |
Finally, the signature is verified.
const verify = crypto.createVerify('sha1WithRSAEncryption'); |
You can test the code with a message like this:
validate({ |
Summary
You should verify the authenticity of a message sent by Amazon SNS. The SNS documentation provides an in-depth description of the needed steps which can be implemented in Node.js as shown in this blog post.
- Master Your Career Path: Achieve Professional Google Workspace Administrator Certification for Unmatched Opportunities in 2025
- AWS Monthly Review – April 2016
- A neglected serverless data store: Cloud Directory
- Guest authors wanted!
- Unlock Your Career Potential with MB-320: The Essential 2025 Guide to Mastering Microsoft Dynamics 365 for Supply Chain













