Skip to content

Commit c5bc961

Browse files
committed
Validate Lambda SNS trigger payload with Url suffixes
1 parent d0c0a1a commit c5bc961

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,19 @@ var url = require('url'),
4141
'Type'
4242
];
4343

44+
var hashHasKey = function (hash, key) {
45+
if (!(key in hash)) {
46+
if (/URL$/.test(key) && key.replace(/URL$/, 'Url') in hash) {
47+
return true;
48+
}
49+
return false;
50+
}
51+
return true;
52+
};
53+
4454
var hashHasKeys = function (hash, keys) {
4555
for (var i = 0; i < keys.length; i++) {
46-
if (!(keys[i] in hash)) {
56+
if (!hashHasKey(hash, keys[i])) {
4757
return false;
4858
}
4959
}
@@ -178,7 +188,7 @@ MessageValidator.prototype.validate = function (hash, cb) {
178188
return;
179189
}
180190

181-
if (!validateUrl(hash['SigningCertURL'], hostPattern)) {
191+
if (!validateUrl(hash['SigningCertURL'] || hash['SigningCertUrl'], hostPattern)) {
182192
cb(new Error('The certificate is located on an invalid domain.'));
183193
return;
184194
}

test/validator.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ var chai = require('chai'),
2121
SignatureVersion: '1',
2222
SigningCertURL: "https://localhost:56789/cert.pem"
2323
},
24+
validLambdaMessage = {
25+
Type: 'Notification',
26+
MessageId: '1',
27+
TopicArn: 'arn',
28+
Message: 'A Lambda message for you!',
29+
Timestamp: (new Date).toISOString(),
30+
SignatureVersion: '1',
31+
SigningCertUrl: "https://localhost:56789/cert.pem"
32+
},
2433
validSubscriptionControlMessage = _.extend({}, validMessage, {
2534
Token: 'Nonce',
2635
SubscribeURL: 'https://www.amazonaws.com',
@@ -46,6 +55,7 @@ describe('Message Validator', function () {
4655
var crypto = require('crypto'),
4756
validMessages = [
4857
validMessage,
58+
validLambdaMessage,
4959
validSubscriptionControlMessage,
5060
utf8Message,
5161
utf8SubscriptionControlMessage
@@ -113,6 +123,23 @@ describe('Message Validator', function () {
113123
});
114124
});
115125

126+
it('should accept Lambda payloads with improper "Url" casing', function (done) {
127+
(new MessageValidator(/^localhost:56789$/))
128+
.validate(validLambdaMessage, function (err, message) {
129+
if (err) {
130+
return done(new Error('The validator should have accepted this message.'));
131+
}
132+
133+
try {
134+
expect(message.Message)
135+
.to.equal('A Lambda message for you!');
136+
done();
137+
} catch (e) {
138+
done(e);
139+
}
140+
});
141+
});
142+
116143
it('should reject hashes residing on an invalid domain', function (done) {
117144
(new MessageValidator)
118145
.validate(validMessage, function (err, message) {

0 commit comments

Comments
 (0)