Skip to content

Commit f3d2995

Browse files
authored
Merge pull request #13 from jeskew/fix/cast-lambda-messages-to-http-style
Replace Lambda-specific handling with a Lambda -> HTTP-style message converter
2 parents 981c1e4 + 900eb3e commit f3d2995

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

.travis.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ language: node_js
22
node_js:
33
- "0.10"
44
- "0.12"
5-
- "4.0"
6-
- "4.1"
75
- "4.2"
8-
- "4.3"
96
- "4.4"
10-
- "6.0"
11-
- "6.1"
7+
- "5"
8+
- "6"
9+
- "7"
10+
- "8"
1211

1312
script:
1413
- npm test

index.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,15 @@ var url = require('url'),
3939
'Token',
4040
'TopicArn',
4141
'Type'
42-
];
43-
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-
};
42+
],
43+
lambdaMessageKeys = {
44+
'SigningCertUrl': 'SigningCertURL',
45+
'UnsubscribeUrl': 'UnsubscribeURL'
46+
};
5347

5448
var hashHasKeys = function (hash, keys) {
5549
for (var i = 0; i < keys.length; i++) {
56-
if (!hashHasKey(hash, keys[i])) {
50+
if (!(keys[i] in hash)) {
5751
return false;
5852
}
5953
}
@@ -71,6 +65,20 @@ var indexOf = function (array, value) {
7165
return -1;
7266
};
7367

68+
function convertLambdaMessage(message) {
69+
for (var key in lambdaMessageKeys) {
70+
if (key in message) {
71+
message[lambdaMessageKeys[key]] = message[key];
72+
}
73+
}
74+
75+
if ('Subject' in message && message.Subject === null) {
76+
delete message.Subject;
77+
}
78+
79+
return message;
80+
}
81+
7482
var validateMessageStructure = function (message) {
7583
var valid = hashHasKeys(message, requiredKeys);
7684

@@ -182,13 +190,14 @@ function MessageValidator(hostPattern, encoding) {
182190
*/
183191
MessageValidator.prototype.validate = function (hash, cb) {
184192
var hostPattern = this.hostPattern;
193+
hash = convertLambdaMessage(hash);
185194

186195
if (!validateMessageStructure(hash)) {
187196
cb(new Error('Message missing required keys.'));
188197
return;
189198
}
190199

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

test/validator.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var chai = require('chai'),
2525
Type: 'Notification',
2626
MessageId: '1',
2727
TopicArn: 'arn',
28+
Subject: null,
2829
Message: 'A Lambda message for you!',
2930
Timestamp: (new Date).toISOString(),
3031
SignatureVersion: '1',
@@ -66,6 +67,14 @@ describe('Message Validator', function () {
6667

6768
for (var j = 0; j < signableKeysForSubscription.length; j++) {
6869
if (signableKeysForSubscription[j] in validMessages[i]) {
70+
// skip signing null Subject fields to match Lambda behavior
71+
if (
72+
signableKeysForSubscription[j] === 'Subject' &&
73+
validMessages[i][signableKeysForSubscription[j]] === null
74+
) {
75+
continue;
76+
}
77+
6978
signer.update(signableKeysForSubscription[j] + "\n"
7079
+ validMessages[i][signableKeysForSubscription[j]] + "\n", 'utf8');
7180
}

0 commit comments

Comments
 (0)