mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Member Settings / Notification Settings / "Browser notifications on this device": the browser asks for permission and gives a push subscription, and from then on every notification the member gets in WeKan - the same recipients, watching and muted kinds as the tray and e-mail - also pops up from that browser even with WeKan closed; a click opens the card. Turn it on per browser and device, up to ten per account. web-push 3.6.7 (pinned) sends it with this WeKan's own VAPID key pair, made once and kept in a server-only collection; only the public key leaves the server. A subscription's endpoint is chosen by the browser and POSTed to by the server, so it is accepted only over https and only at the push services browsers use - an internal address or another site is refused when saved and again before every send. Gone subscriptions are forgotten. The service worker opens only paths of this WeKan. Not offered on Sandstorm. models/lib/webPush.js decides; tests/webPush.test.cjs covers the push services, internal and look-alike hosts, foreign click targets and the bound; server/lib/tests/webPush.tests.js runs in Meteor (passing): one key pair, only the public key out, an internal endpoint refused. With the webhook activity picker (c3321926ab) and the all-boards activity feed (656af74b2c), this completes both issues. The new strings are English placeholders until the next translation pass. Thanks to saurabharch, hingerl and xet7 ! Fixes #2026, Fixes #1377.
51 lines
3.4 KiB
JavaScript
51 lines
3.4 KiB
JavaScript
'use strict';
|
|
|
|
// #2026 / #1377: browser push notifications.
|
|
// Run: node tests/webPush.test.cjs
|
|
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const w = require('../models/lib/webPush.js');
|
|
|
|
const ROOT = path.join(__dirname, '..');
|
|
const read = f => fs.readFileSync(path.join(ROOT, f), 'utf8');
|
|
const keys = { p256dh: 'BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM', auth: 'tBHItJI5svbpez7KI4CCXg' };
|
|
|
|
// Positive: the push services browsers use.
|
|
for (const endpoint of ['https://fcm.googleapis.com/fcm/send/abc', 'https://updates.push.services.mozilla.com/wpush/v2/x',
|
|
'https://web.push.apple.com/QGuQ', 'https://db5p.notify.windows.com/w/?token=x']) {
|
|
assert.ok(w.cleanSubscription({ endpoint, keys }), endpoint);
|
|
}
|
|
assert.deepEqual(JSON.parse(w.pushPayload({ title: 'T', body: 'B', url: '/b/x/y' })), { title: 'T', body: 'B', url: '/b/x/y' });
|
|
const after = w.subscriptionsAfterAdd([{ endpoint: 'https://fcm.googleapis.com/a' }], { endpoint: 'https://fcm.googleapis.com/a', keys });
|
|
assert.equal(after.length, 1, 'once each');
|
|
console.log(' ok - browser push services accepted');
|
|
|
|
// Negative: the server never POSTs where a member likes.
|
|
for (const endpoint of ['http://fcm.googleapis.com/x', 'https://127.0.0.1/x', 'https://localhost/x', 'https://evil.example/x',
|
|
'https://fcm.googleapis.com.evil.example/x', 'https://user:pw@fcm.googleapis.com/x', 'https://fcm.googleapis.com:8443/x',
|
|
'javascript:alert(1)', '']) {
|
|
assert.equal(w.isPushServiceEndpoint(endpoint), false, endpoint);
|
|
assert.equal(w.cleanSubscription({ endpoint, keys }), null, endpoint);
|
|
}
|
|
assert.equal(w.cleanSubscription({ endpoint: 'https://fcm.googleapis.com/x', keys: { p256dh: 'x', auth: '<script>' } }), null);
|
|
assert.equal(JSON.parse(w.pushPayload({ url: 'https://evil.example/' })).url, '/', 'a click only opens this WeKan');
|
|
assert.equal(JSON.parse(w.pushPayload({ url: '//evil.example/' })).url, '/');
|
|
assert.equal(w.subscriptionsAfterAdd(Array.from({ length: 30 }, (_, i) => ({ endpoint: `e${i}` })), { endpoint: 'x' }).length, w.MAX_SUBSCRIPTIONS);
|
|
console.log(' ok - no other endpoint, no foreign click target, bounded');
|
|
|
|
const server = read('server/webPush.js');
|
|
assert.match(server, /const clean = cleanSubscription\(subscription\);\s*if \(!clean\) throw new Meteor\.Error\('web-push-invalid-subscription'\);/);
|
|
assert.match(server, /const clean = cleanSubscription\(sub\);\s*if \(!clean\) continue;/, 'checked again before every send');
|
|
assert.match(server, /WebPushKeys\.deny\(\{ insert: \(\) => true, update: \(\) => true, remove: \(\) => true \}\);/);
|
|
assert.match(server, /async webPushPublicKey\(\) \{[\s\S]{0,120}return \(await vapidKeys\(\)\)\.publicKey;/, 'only the public key leaves the server');
|
|
assert.match(read('server/models/activities.js'), /require\('\/server\/webPush'\)\.sendActivityToWebPush\(prepared\);/);
|
|
assert.equal(JSON.parse(read('package.json')).dependencies['web-push'], '3.6.7', 'pinned');
|
|
const sw = read('public/pwa-service-worker.js');
|
|
assert.match(sw, /self\.addEventListener\('push'/);
|
|
assert.match(sw, /var url = typeof data\.url === 'string' && data\.url\.charAt\(0\) === '\/' \? data\.url : '\/';/);
|
|
assert.match(read('client/components/settings/notificationSettingsPopup.jade'), /li\.notification-web-push-settings/);
|
|
console.log(' ok - keys stay server-side; the service worker opens only local paths');
|
|
console.log('\nwebPush: all tests passed');
|