Skip to content

Commit c9ccc16

Browse files
committed
Check public key and auth secret length
1 parent 84ba55c commit c9ccc16

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,20 @@ function sendNotification(endpoint, params) {
148148
console.warn('You are using the old, deprecated, interface of the `sendNotification` function.'.bold.red);
149149
}
150150

151-
if (typeof userPublicKey !== 'undefined' && typeof userPublicKey !== 'string') {
152-
throw new Error('userPublicKey should be a base64-encoded string.');
151+
if (typeof userPublicKey !== 'undefined') {
152+
if (typeof userPublicKey !== 'string') {
153+
throw new Error('userPublicKey should be a base64-encoded string.');
154+
} else if (urlBase64.decode(userPublicKey).length !== 65) {
155+
throw new Error('userPublicKey should be 65 bytes long.');
156+
}
153157
}
154158

155-
if (typeof userAuth !== 'undefined' && typeof userAuth !== 'string') {
156-
throw new Error('userAuth should be a base64-encoded string.');
159+
if (typeof userAuth !== 'undefined') {
160+
if (typeof userAuth !== 'string') {
161+
throw new Error('userAuth should be a base64-encoded string.');
162+
} else if (urlBase64.decode(userAuth).length < 12) {
163+
throw new Error('userAuth should be at least 12 bytes long');
164+
}
157165
}
158166

159167
const isGCM = endpoint.indexOf('https://android.googleapis.com/gcm/send') === 0;

test/testSendNotification.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ suite('sendNotification', function() {
421421
});
422422
});
423423

424-
test('invalid userPublicKey arguments', function() {
424+
test('userPublicKey argument isn\'t a string', function() {
425425
return webPush.sendNotification('https://127.0.0.1:' + serverPort, {
426426
userPublicKey: userPublicKey,
427427
userAuth: urlBase64.encode(userAuth),
@@ -434,7 +434,7 @@ suite('sendNotification', function() {
434434
});
435435
});
436436

437-
test('invalid userAuth arguments', function() {
437+
test('userAuth argument isn\'t a string', function() {
438438
return webPush.sendNotification('https://127.0.0.1:' + serverPort, {
439439
userPublicKey: urlBase64.encode(userPublicKey),
440440
userAuth: userAuth,
@@ -447,6 +447,45 @@ suite('sendNotification', function() {
447447
});
448448
});
449449

450+
test('userPublicKey argument is too long', function() {
451+
return webPush.sendNotification('https://127.0.0.1:' + serverPort, {
452+
userPublicKey: urlBase64.encode(Buffer.concat([ userPublicKey, new Buffer(1) ])),
453+
userAuth: urlBase64.encode(userAuth),
454+
payload: 'hello',
455+
})
456+
.then(function(body) {
457+
assert(false, 'sendNotification promise resolved');
458+
}, function() {
459+
assert(true, 'sendNotification promise rejected');
460+
});
461+
});
462+
463+
test('userPublicKey argument is too short', function() {
464+
return webPush.sendNotification('https://127.0.0.1:' + serverPort, {
465+
userPublicKey: urlBase64.encode(userPublicKey.slice(1)),
466+
userAuth: urlBase64.encode(userAuth),
467+
payload: 'hello',
468+
})
469+
.then(function(body) {
470+
assert(false, 'sendNotification promise resolved');
471+
}, function() {
472+
assert(true, 'sendNotification promise rejected');
473+
});
474+
});
475+
476+
test('userAuth argument is too short', function() {
477+
return webPush.sendNotification('https://127.0.0.1:' + serverPort, {
478+
userPublicKey: urlBase64.encode(userPublicKey),
479+
userAuth: urlBase64.encode(userAuth.slice(1)),
480+
payload: 'hello',
481+
})
482+
.then(function(body) {
483+
assert(false, 'sendNotification promise resolved');
484+
}, function() {
485+
assert(true, 'sendNotification promise rejected');
486+
});
487+
});
488+
450489
test('TTL with old interface', function() {
451490
return startServer(undefined, 5)
452491
.then(function() {

0 commit comments

Comments
 (0)