Skip to content

Commit

Permalink
Merge pull request #10 from mostcowbell/lambda-url-case
Browse files Browse the repository at this point in the history
Validate Lambda SNS trigger payload with `Url` suffixes
  • Loading branch information
jeskew authored Mar 10, 2017
2 parents d0c0a1a + c5bc961 commit 9126c83
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,19 @@ var url = require('url'),
'Type'
];

var hashHasKey = function (hash, key) {
if (!(key in hash)) {
if (/URL$/.test(key) && key.replace(/URL$/, 'Url') in hash) {
return true;
}
return false;
}
return true;
};

var hashHasKeys = function (hash, keys) {
for (var i = 0; i < keys.length; i++) {
if (!(keys[i] in hash)) {
if (!hashHasKey(hash, keys[i])) {
return false;
}
}
Expand Down Expand Up @@ -178,7 +188,7 @@ MessageValidator.prototype.validate = function (hash, cb) {
return;
}

if (!validateUrl(hash['SigningCertURL'], hostPattern)) {
if (!validateUrl(hash['SigningCertURL'] || hash['SigningCertUrl'], hostPattern)) {
cb(new Error('The certificate is located on an invalid domain.'));
return;
}
Expand Down
27 changes: 27 additions & 0 deletions test/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ var chai = require('chai'),
SignatureVersion: '1',
SigningCertURL: "https://localhost:56789/cert.pem"
},
validLambdaMessage = {
Type: 'Notification',
MessageId: '1',
TopicArn: 'arn',
Message: 'A Lambda message for you!',
Timestamp: (new Date).toISOString(),
SignatureVersion: '1',
SigningCertUrl: "https://localhost:56789/cert.pem"
},
validSubscriptionControlMessage = _.extend({}, validMessage, {
Token: 'Nonce',
SubscribeURL: 'https://www.amazonaws.com',
Expand All @@ -46,6 +55,7 @@ describe('Message Validator', function () {
var crypto = require('crypto'),
validMessages = [
validMessage,
validLambdaMessage,
validSubscriptionControlMessage,
utf8Message,
utf8SubscriptionControlMessage
Expand Down Expand Up @@ -113,6 +123,23 @@ describe('Message Validator', function () {
});
});

it('should accept Lambda payloads with improper "Url" casing', function (done) {
(new MessageValidator(/^localhost:56789$/))
.validate(validLambdaMessage, function (err, message) {
if (err) {
return done(new Error('The validator should have accepted this message.'));
}

try {
expect(message.Message)
.to.equal('A Lambda message for you!');
done();
} catch (e) {
done(e);
}
});
});

it('should reject hashes residing on an invalid domain', function (done) {
(new MessageValidator)
.validate(validMessage, function (err, message) {
Expand Down

0 comments on commit 9126c83

Please sign in to comment.