@@ -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 ( / ^ l o c a l h o s t : 5 6 7 8 9 $ / ) )
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 ( / ^ l o c a l h o s t : 5 6 7 8 9 $ / ) )
227+ . validate ( validSHA256Message , done ) ;
228+ } ) ;
229+
214230 it ( 'should accept valid messages as JSON strings' , function ( done ) {
215231 ( new MessageValidator ( / ^ l o c a l h o s t : 5 6 7 8 9 $ / ) )
216232 . validate ( JSON . stringify ( validMessage ) , done ) ;
0 commit comments