Skip to content

Commit 03010bf

Browse files
committed
Merge pull request #3 from mokemokechicken/feature/enable_to_specify_message_encoding
enable to specify message encoding
2 parents 3ff5ed7 + 6a78c06 commit 03010bf

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ validator.validate(message, function (err, message) {
2828
// Your message could not be validated.
2929
return;
3030
}
31-
31+
3232
// message has been validated and its signature checked.
3333
});
3434
```
@@ -79,7 +79,7 @@ validator.validate(message, function (err, message) {
7979
console.error(err);
8080
return;
8181
}
82-
82+
8383
if (message['Type'] === 'SubscriptionConfirmation') {
8484
https.get(message['SubscribeURL'], function (res) {
8585
// You have confirmed your endpoint subscription
@@ -88,6 +88,15 @@ validator.validate(message, function (err, message) {
8888
});
8989
```
9090

91+
If an incoming message includes multibyte characters and its encoding is utf8,
92+
set the encoding to `validator`.
93+
94+
```javascript
95+
var MessageValidator = require('sns-validator'),
96+
validator = new MessageValidator();
97+
validator.encoding = 'utf8';
98+
```
99+
91100
### Receiving a Notification
92101

93102
To receive a notification, use the same code as the preceding example, but

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ var getCertificate = function (certUrl, cb) {
8888
}).on('error', cb)
8989
};
9090

91-
var validateSignature = function (message, cb) {
91+
var validateSignature = function (message, cb, encoding) {
9292
if (message['SignatureVersion'] !== '1') {
9393
cb(new Error('The signature version '
9494
+ message['SignatureVersion'] + ' is not supported.'));
@@ -99,7 +99,7 @@ var validateSignature = function (message, cb) {
9999
for (var i = 0; i < signableKeys.length; i++) {
100100
if (signableKeys[i] in message) {
101101
verifier.update(signableKeys[i] + "\n"
102-
+ message[signableKeys[i]] + "\n");
102+
+ message[signableKeys[i]] + "\n", encoding);
103103
}
104104
}
105105

@@ -123,8 +123,9 @@ var validateSignature = function (message, cb) {
123123
* @constructor
124124
* @param {RegExp} [hostPattern=/^sns\.[a-zA-Z0-9\-]{3,}\.amazonaws\.com(\.cn)?$/] - A pattern used to validate that a message's certificate originates from a trusted domain.
125125
*/
126-
function MessageValidator(hostPattern) {
126+
function MessageValidator(hostPattern, encoding) {
127127
this.hostPattern = hostPattern || defaultHostPattern;
128+
this.encoding = encoding;
128129
}
129130

130131
/**
@@ -156,7 +157,7 @@ MessageValidator.prototype.validate = function (hash, cb) {
156157
return;
157158
}
158159

159-
validateSignature(hash, cb);
160+
validateSignature(hash, cb, this.encoding);
160161
};
161162

162163
module.exports = MessageValidator;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sns-validator",
3-
"version": "0.1.0",
3+
"version": "0.2.1",
44
"description": "A standalone validator for inbound SNS HTTP messages. No dependency on the AWS SDK for JavaScript.",
55
"repository": {
66
"type": "git",

test/validator.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ var chai = require('chai'),
2424
SubscribeURL: 'https://www.amazonaws.com',
2525
Type: 'SubscriptionConfirmation'
2626
}),
27+
utf8Message = {
28+
Type: 'Notification',
29+
MessageId: '1',
30+
TopicArn: 'arn',
31+
Message: 'A Message For you!',
32+
Timestamp: (new Date).toISOString(),
33+
SignatureVersion: '1',
34+
SigningCertURL: "https://localhost:56789/cert.pem"
35+
},
36+
utf8SubscriptionControlMessage = _.extend({}, utf8Message, {
37+
Token: 'Nonce',
38+
SubscribeURL: 'https://www.amazonaws.com',
39+
Type: 'SubscriptionConfirmation'
40+
}),
2741
validCertUrl = 'https://sns.us-east-1.amazonaws.com/cert.pem';
2842

2943
describe('Message Validator', function () {
@@ -182,4 +196,40 @@ describe('Message Validator', function () {
182196
.validate(validSubscriptionControlMessage, done);
183197
});
184198
});
199+
200+
describe('UTF8 message validation', function () {
201+
before(function (done) {
202+
pem.createCertificate({}, function (err, certHash) {
203+
if (err) throw err;
204+
205+
var crypto = require('crypto'),
206+
validMessages = [utf8Message, utf8SubscriptionControlMessage];
207+
208+
for (var i = 0; i < validMessages.length; i++) {
209+
var signer = crypto.createSign('RSA-SHA1');
210+
211+
for (var j = 0; j < signableKeys.length; j++) {
212+
if (signableKeys[j] in validMessages[i]) {
213+
signer.update(signableKeys[j] + "\n"
214+
+ validMessages[i][signableKeys[j]] + "\n", 'utf8');
215+
}
216+
}
217+
218+
validMessages[i]['Signature']
219+
= signer.sign(certHash.serviceKey, 'base64');
220+
}
221+
222+
MessageValidator.__set__('getCertificate', function (url, cb) {
223+
cb(null, certHash.certificate);
224+
});
225+
226+
done();
227+
});
228+
});
229+
230+
it('should accept a valid UTF8 message', function (done) {
231+
(new MessageValidator(/^localhost:56789$/, 'utf8'))
232+
.validate(utf8Message, done);
233+
});
234+
});
185235
});

0 commit comments

Comments
 (0)