Skip to content

Commit 9058c31

Browse files
committed
Merge pull request #144 from marco-c/improve_sanity_checks
Improve sanity checks
2 parents 10af831 + 2f8d98c commit 9058c31

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

index.js

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

151+
if (userPublicKey) {
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+
}
157+
}
158+
159+
if (userAuth) {
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+
}
165+
}
166+
151167
const isGCM = endpoint.indexOf('https://android.googleapis.com/gcm/send') === 0;
152168

153169
var urlParts = url.parse(endpoint);

test/testSelenium.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,10 @@ suite('selenium', function() {
159159

160160
try {
161161
console.log('Using Firefox: ' + firefoxStableBinaryPath);
162-
console.log('Version: ' + childProcess.execSync(firefoxStableBinaryPath + ' --version').replace('\n', ''));
163-
console.log('Beta Version: ' + childProcess.execSync(firefoxBetaBinaryPath + ' --version').replace('\n', ''));
164-
console.log('Aurora Version: ' + childProcess.execSync(firefoxAuroraBinaryPath + ' --version').replace('\n', ''));
162+
console.log('Version: ' + childProcess.execSync(firefoxStableBinaryPath + ' --version').toString().replace('\n', ''));
163+
console.log('Beta Version: ' + childProcess.execSync(firefoxBetaBinaryPath + ' --version').toString().replace('\n', ''));
164+
//console.log('Aurora Version: ' + childProcess.execSync(firefoxAuroraBinaryPath + ' --version').toString().replace('\n', ''));
165+
console.log('Nightly Version: ' + childProcess.execSync(firefoxNightlyBinaryPath + ' --version').toString().replace('\n', ''));
165166
} catch (e) {}
166167

167168
if (process.env.GCM_API_KEY && !fs.existsSync(chromeBinaryPath)) {
@@ -170,7 +171,7 @@ suite('selenium', function() {
170171

171172
try {
172173
console.log('Using Chromium: ' + chromeBinaryPath);
173-
console.log('Version: ' + childProcess.execSync(chromeBinaryPath + ' --version').replace('\n', ''));
174+
console.log('Version: ' + childProcess.execSync(chromeBinaryPath + ' --version').toString().replace('\n', ''));
174175
} catch (e) {}
175176
});
176177
});

test/testSendNotification.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,71 @@ suite('sendNotification', function() {
438438
});
439439
});
440440

441+
test('userPublicKey argument isn\'t a string', function() {
442+
return webPush.sendNotification('https://127.0.0.1:' + serverPort, {
443+
userPublicKey: userPublicKey,
444+
userAuth: urlBase64.encode(userAuth),
445+
payload: 'hello',
446+
})
447+
.then(function(body) {
448+
assert(false, 'sendNotification promise resolved');
449+
}, function() {
450+
assert(true, 'sendNotification promise rejected');
451+
});
452+
});
453+
454+
test('userAuth argument isn\'t a string', function() {
455+
return webPush.sendNotification('https://127.0.0.1:' + serverPort, {
456+
userPublicKey: urlBase64.encode(userPublicKey),
457+
userAuth: userAuth,
458+
payload: 'hello',
459+
})
460+
.then(function(body) {
461+
assert(false, 'sendNotification promise resolved');
462+
}, function() {
463+
assert(true, 'sendNotification promise rejected');
464+
});
465+
});
466+
467+
test('userPublicKey argument is too long', function() {
468+
return webPush.sendNotification('https://127.0.0.1:' + serverPort, {
469+
userPublicKey: urlBase64.encode(Buffer.concat([ userPublicKey, new Buffer(1) ])),
470+
userAuth: urlBase64.encode(userAuth),
471+
payload: 'hello',
472+
})
473+
.then(function(body) {
474+
assert(false, 'sendNotification promise resolved');
475+
}, function() {
476+
assert(true, 'sendNotification promise rejected');
477+
});
478+
});
479+
480+
test('userPublicKey argument is too short', function() {
481+
return webPush.sendNotification('https://127.0.0.1:' + serverPort, {
482+
userPublicKey: urlBase64.encode(userPublicKey.slice(1)),
483+
userAuth: urlBase64.encode(userAuth),
484+
payload: 'hello',
485+
})
486+
.then(function(body) {
487+
assert(false, 'sendNotification promise resolved');
488+
}, function() {
489+
assert(true, 'sendNotification promise rejected');
490+
});
491+
});
492+
493+
test('userAuth argument is too short', function() {
494+
return webPush.sendNotification('https://127.0.0.1:' + serverPort, {
495+
userPublicKey: urlBase64.encode(userPublicKey),
496+
userAuth: urlBase64.encode(userAuth.slice(1)),
497+
payload: 'hello',
498+
})
499+
.then(function(body) {
500+
assert(false, 'sendNotification promise resolved');
501+
}, function() {
502+
assert(true, 'sendNotification promise rejected');
503+
});
504+
});
505+
441506
test('TTL with old interface', function() {
442507
return startServer(undefined, 5)
443508
.then(function() {

0 commit comments

Comments
 (0)