Skip to content

Commit d7c45c7

Browse files
parkerramParker Ram
andauthored
Add SHA256 support (#32)
Co-authored-by: Parker Ram <rmprke@amazon.com>
1 parent dc05961 commit d7c45c7

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ var getCertificate = function (certUrl, cb) {
122122
};
123123

124124
var validateSignature = function (message, cb, encoding) {
125-
if (message['SignatureVersion'] !== '1') {
125+
var signatureVersion = message['SignatureVersion'];
126+
if (signatureVersion !== '1' && signatureVersion !== '2') {
126127
cb(new Error('The signature version '
127-
+ message['SignatureVersion'] + ' is not supported.'));
128+
+ signatureVersion + ' is not supported.'));
128129
return;
129130
}
130131

@@ -135,7 +136,7 @@ var validateSignature = function (message, cb, encoding) {
135136
signableKeys = signableKeysForNotification.slice(0);
136137
}
137138

138-
var verifier = crypto.createVerify('RSA-SHA1');
139+
var verifier = (signatureVersion === '1') ? crypto.createVerify('RSA-SHA1') : crypto.createVerify('RSA-SHA256');
139140
for (var i = 0; i < signableKeys.length; i++) {
140141
if (signableKeys[i] in message) {
141142
verifier.update(signableKeys[i] + "\n"

test/validator.js

Lines changed: 31 additions & 15 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+
validSHA256Message = {
25+
Type: 'Notification',
26+
MessageId: '1',
27+
TopicArn: 'arn',
28+
Message: 'A message for you!',
29+
Timestamp: (new Date).toISOString(),
30+
SignatureVersion: '2',
31+
SigningCertURL: "https://localhost:56789/cert.pem"
32+
},
2433
validLambdaMessage = {
2534
Type: 'Notification',
2635
MessageId: '1',
@@ -56,14 +65,16 @@ describe('Message Validator', function () {
5665
var crypto = require('crypto'),
5766
validMessages = [
5867
validMessage,
68+
validSHA256Message,
5969
validLambdaMessage,
6070
validSubscriptionControlMessage,
6171
utf8Message,
6272
utf8SubscriptionControlMessage
6373
];
6474

6575
for (var i = 0; i < validMessages.length; i++) {
66-
var signer = crypto.createSign('RSA-SHA1');
76+
var signatureVersion = validMessages[i]['SignatureVersion'];
77+
var signer = (signatureVersion === '1') ? crypto.createSign('RSA-SHA1') : crypto.createSign('RSA-SHA256');
6778

6879
for (var j = 0; j < signableKeysForSubscription.length; j++) {
6980
if (signableKeysForSubscription[j] in validMessages[i]) {
@@ -134,19 +145,19 @@ describe('Message Validator', function () {
134145

135146
it('should accept Lambda payloads with improper "Url" casing', function (done) {
136147
(new MessageValidator(/^localhost:56789$/))
137-
.validate(validLambdaMessage, function (err, message) {
138-
if (err) {
139-
return done(new Error('The validator should have accepted this message.'));
140-
}
148+
.validate(validLambdaMessage, function (err, message) {
149+
if (err) {
150+
return done(new Error('The validator should have accepted this message.'));
151+
}
141152

142-
try {
143-
expect(message.Message)
144-
.to.equal('A Lambda message for you!');
145-
done();
146-
} catch (e) {
147-
done(e);
148-
}
149-
});
153+
try {
154+
expect(message.Message)
155+
.to.equal('A Lambda message for you!');
156+
done();
157+
} catch (e) {
158+
done(e);
159+
}
160+
});
150161
});
151162

152163
it('should reject hashes residing on an invalid domain', function (done) {
@@ -169,7 +180,7 @@ describe('Message Validator', function () {
169180
it('should reject hashes with an invalid signature type', function (done) {
170181
(new MessageValidator)
171182
.validate(_.extend({}, validMessage, {
172-
SignatureVersion: '2',
183+
SignatureVersion: '3',
173184
SigningCertURL: validCertUrl
174185
}), function (err, message) {
175186
if (!err) {
@@ -178,7 +189,7 @@ describe('Message Validator', function () {
178189

179190
try {
180191
expect(err.message)
181-
.to.equal('The signature version 2 is not supported.');
192+
.to.equal('The signature version 3 is not supported.');
182193
done();
183194
} catch (e) {
184195
done(e);
@@ -211,6 +222,11 @@ describe('Message Validator', function () {
211222
.validate(validMessage, done);
212223
});
213224

225+
it('should accept a valid message', function (done) {
226+
(new MessageValidator(/^localhost:56789$/))
227+
.validate(validSHA256Message, done);
228+
});
229+
214230
it('should accept valid messages as JSON strings', function (done) {
215231
(new MessageValidator(/^localhost:56789$/))
216232
.validate(JSON.stringify(validMessage), done);

0 commit comments

Comments
 (0)