Fixed event dispatching bug.

Ylian Saint-Hilaire committed Nov 10, 2021 at 17:56 UTC aab50dcbef60847a9615b673ffbf3df1b7ab41ff
5 files changed +52 -23
meshagent.js
+4 -3
@@ -1706,8 +1706,9 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1706
1707 function addGuestSharing(flags, viewOnly, func) {
1708 // Create cookie
1709 - var publicid = 'AS:' + obj.dbNodeKey;
1710 - var cookie = { a: 6, pid: publicid }; // New style sharing cookie
1709 + const publicid = 'AS:' + obj.dbNodeKey;
1710 + const extrakey = getRandomAmtPassword();
1711 + const cookie = { a: 6, pid: publicid, k: extrakey }; // New style sharing cookie
1712 const inviteCookie = parent.parent.encodeCookie(cookie, parent.parent.invitationLinkEncryptionKey);
1713 if (inviteCookie == null) return;
1714
@@ -1720,7 +1721,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1721 if (serverName.split('.') == 1) { url = '/' + xdomain + page + '?c=' + inviteCookie; }
1722
1723 // Create a device sharing database entry
1723 - var shareEntry = { _id: 'deviceshare-' + publicid, type: 'deviceshare', nodeid: obj.dbNodeKey, p: flags, domain: domain.id, publicid: publicid, guestName: 'Agent', consent: 0x7F, url: url };
1724 + var shareEntry = { _id: 'deviceshare-' + publicid, type: 'deviceshare', nodeid: obj.dbNodeKey, p: flags, domain: domain.id, publicid: publicid, guestName: 'Agent', consent: 0x7F, url: url, extrakey: extrakey };
1725 if (viewOnly === true) { shareEntry.viewOnly = true; }
1726 parent.db.Set(shareEntry);
1727
meshcentral.js
+36 -12
@@ -1969,7 +1969,21 @@ function CreateMeshCentralServer(config, args) {
1969 };
1970 obj.RemoveEventDispatch = function (ids, target) {
1971 obj.debug('dispatch', 'RemoveEventDispatch', ids);
1972 - for (var i in ids) { var id = ids[i]; if (obj.eventsDispatch[id]) { var j = obj.eventsDispatch[id].indexOf(target); if (j >= 0) { if (obj.eventsDispatch[id].length == 1) { delete obj.eventsDispatch[id]; } else { obj.eventsDispatch[id].splice(j, 1); } } } }
1972 + for (var i in ids) {
1973 + const id = ids[i];
1974 + if (obj.eventsDispatch[id]) {
1975 + var j = obj.eventsDispatch[id].indexOf(target);
1976 + if (j >= 0) {
1977 + if (obj.eventsDispatch[id].length == 1) {
1978 + delete obj.eventsDispatch[id];
1979 + } else {
1980 + const newList = []; // We create a new list so not to modify the original list. Allows this function to be called during an event dispatch.
1981 + for (var k in obj.eventsDispatch[i]) { if (obj.eventsDispatch[i][k] != target) { newList.push(obj.eventsDispatch[i][k]); } }
1982 + obj.eventsDispatch[i] = newList;
1983 + }
1984 + }
1985 + }
1986 + }
1987 };
1988 obj.RemoveEventDispatchId = function (id) {
1989 obj.debug('dispatch', 'RemoveEventDispatchId', id);
@@ -1977,7 +1991,18 @@ function CreateMeshCentralServer(config, args) {
1991 };
1992 obj.RemoveAllEventDispatch = function (target) {
1993 obj.debug('dispatch', 'RemoveAllEventDispatch');
1980 - for (var i in obj.eventsDispatch) { var j = obj.eventsDispatch[i].indexOf(target); if (j >= 0) { if (obj.eventsDispatch[i].length == 1) { delete obj.eventsDispatch[i]; } else { obj.eventsDispatch[i].splice(j, 1); } } }
1994 + for (var i in obj.eventsDispatch) {
1995 + const j = obj.eventsDispatch[i].indexOf(target);
1996 + if (j >= 0) {
1997 + if (obj.eventsDispatch[i].length == 1) {
1998 + delete obj.eventsDispatch[i];
1999 + } else {
2000 + const newList = []; // We create a new list so not to modify the original list. Allows this function to be called during an event dispatch.
2001 + for (var k in obj.eventsDispatch[i]) { if (obj.eventsDispatch[i][k] != target) { newList.push(obj.eventsDispatch[i][k]); } }
2002 + obj.eventsDispatch[i] = newList;
2003 + }
2004 + }
2005 + }
2006 };
2007 obj.DispatchEvent = function (ids, source, event, fromPeerServer) {
2008 // If the database is not setup, exit now.
@@ -1992,7 +2017,7 @@ function CreateMeshCentralServer(config, args) {
2017 if ((typeof event == 'object') && (!event.nolog)) {
2018 event.time = new Date();
2019 // The event we store is going to skip some of the fields so we don't store too much stuff in the database.
1995 - var storeEvent = Object.assign({}, event);
2020 + const storeEvent = Object.assign({}, event);
2021 if (storeEvent.node) { delete storeEvent.node; } // Skip the "node" field. May skip more in the future.
2022 if (storeEvent.links) {
2023 // Escape "links" names that may have "." and/or "$"
@@ -2002,16 +2027,15 @@ function CreateMeshCentralServer(config, args) {
2027 storeEvent.ids = ids;
2028 obj.db.StoreEvent(storeEvent);
2029 }
2005 - var targets = []; // List of targets we dispatched the event to, we don't want to dispatch to the same target twice.
2030 + const targets = []; // List of targets we dispatched the event to, we don't want to dispatch to the same target twice.
2031 for (var j in ids) {
2007 - var id = ids[j];
2008 - if (obj.eventsDispatch[id]) {
2009 - for (var i in obj.eventsDispatch[id]) {
2010 - if (targets.indexOf(obj.eventsDispatch[id][i]) == -1) { // Check if we already displatched to this target
2011 - targets.push(obj.eventsDispatch[id][i]);
2012 - try { obj.eventsDispatch[id][i].HandleEvent(source, event, ids, id); } catch (ex) {
2013 - console.log(ex, obj.eventsDispatch[id][i]);
2014 - }
2032 + const id = ids[j];
2033 + const eventsDispatch = obj.eventsDispatch[id];
2034 + if (eventsDispatch) {
2035 + for (var i in eventsDispatch) {
2036 + if (targets.indexOf(eventsDispatch[i]) == -1) { // Check if we already displatched to this target
2037 + targets.push(eventsDispatch[i]);
2038 + try { eventsDispatch[i].HandleEvent(source, event, ids, id); } catch (ex) { console.log(ex, eventsDispatch[i]); }
2039 }
2040 }
2041 }
meshdesktopmultiplex.js
+3 -3
@@ -941,21 +941,21 @@ function CreateDesktopMultiplexor(parent, domain, nodeid, func) {
941 return obj;
942 }
943
944 -function checkDeviceSharePublicIdentifier(parent, domain, nodeid, pid, func) {
944 +function checkDeviceSharePublicIdentifier(parent, domain, nodeid, pid, extraKey, func) {
945 // Check the public id
946 parent.db.GetAllTypeNodeFiltered([nodeid], domain.id, 'deviceshare', null, function (err, docs) {
947 if ((err != null) || (docs.length == 0)) { func(false); return; }
948
949 // Search for the device share public identifier
950 var found = false;
951 - for (var i = 0; i < docs.length; i++) { if (docs[i].publicid == pid) { found = true; } }
951 + for (var i = 0; i < docs.length; i++) { if ((docs[i].publicid == pid) && ((docs[i].extrakey == null) || (docs[i].extrakey === extraKey))) { found = true; } }
952 func(found);
953 });
954 }
955
956 module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie) {
957 if ((cookie != null) && (typeof cookie.nid == 'string') && (typeof cookie.pid == 'string')) {
958 - checkDeviceSharePublicIdentifier(parent, domain, cookie.nid, cookie.pid, function (result) {
958 + checkDeviceSharePublicIdentifier(parent, domain, cookie.nid, cookie.pid, cookie.k, function (result) {
959 // If the identifier if not found, close the connection
960 if (result == false) { try { ws.close(); } catch (e) { } return; }
961 // Public device sharing identifier found, continue as normal.
meshrelay.js
+5 -3
@@ -47,21 +47,23 @@ const MESHRIGHT_ADMIN = 0xFFFFFFFF;
47 // 101 = Intel AMT Redirection
48 // 200 = Messenger
49
50 -function checkDeviceSharePublicIdentifier(parent, domain, nodeid, pid, func) {
50 +function checkDeviceSharePublicIdentifier(parent, domain, nodeid, pid, extraKey, func) {
51 // Check the public id
52 parent.db.GetAllTypeNodeFiltered([nodeid], domain.id, 'deviceshare', null, function (err, docs) {
53 if ((err != null) || (docs.length == 0)) { func(false); return; }
54
55 // Search for the device share public identifier
56 var found = false;
57 - for (var i = 0; i < docs.length; i++) { if (docs[i].publicid == pid) { found = true; } }
57 + for (var i = 0; i < docs.length; i++) {
58 + for (var i = 0; i < docs.length; i++) { if ((docs[i].publicid == pid) && ((docs[i].extrakey == null) || (docs[i].extrakey === extraKey))) { found = true; } }
59 + }
60 func(found);
61 });
62 }
63
64 module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie) {
65 if ((cookie != null) && (typeof cookie.nid == 'string') && (typeof cookie.pid == 'string')) {
64 - checkDeviceSharePublicIdentifier(parent, domain, cookie.nid, cookie.pid, function (result) {
66 + checkDeviceSharePublicIdentifier(parent, domain, cookie.nid, cookie.pid, cookie.k, function (result) {
67 // If the identifier if not found, close the connection
68 if (result == false) { try { ws.close(); } catch (e) { } return; }
69 // Public device sharing identifier found, continue as normal.
webserver.js
+4 -2
@@ -3583,7 +3583,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
3583 if ((err != null) || (docs == null) || (docs.length != 1)) { res.sendStatus(404); return; }
3584 const doc = docs[0];
3585 // Generate an old style cookie from the information in the database
3586 - var cookie = { a: 5, p: doc.p, uid: doc.userid, gn: doc.guestName, nid: doc.nodeid, cf: doc.consent, pid: doc.publicid };
3586 + var cookie = { a: 5, p: doc.p, gn: doc.guestName, nid: doc.nodeid, cf: doc.consent, pid: doc.publicid, k: doc.extrakey };
3587 + if (doc.userid) { cookie.uid = doc.userid; }
3588 if ((cookie.userid == null) && (cookie.pid.startsWith('AS:node/'))) { cookie.nouser = 1; }
3589 if ((doc.startTime != null) && (doc.expireTime != null)) { cookie.start = doc.startTime; cookie.expire = doc.expireTime; }
3590 if (doc.viewOnly === true) { cookie.vo = 1; }
@@ -3606,7 +3607,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
3607
3608 // Search for the device share public identifier, expire message.
3609 var found = false;
3609 - for (var i = 0; i < docs.length; i++) { if (docs[i].publicid == c.pid) { found = true; } }
3610 + for (var i = 0; i < docs.length; i++) { if ((docs[i].publicid == c.pid) && ((docs[i].extrakey == null) || (docs[i].extrakey === c.k))) { found = true; } }
3611 if (found == false) { render(req, res, getRenderPage((domain.sitestyle == 2) ? 'message2' : 'message', req, domain), getRenderArgs({ titleid: 2, msgid: 12, domainurl: encodeURIComponent(domain.url).replace(/'/g, '%27') }, req, domain)); return; }
3612
3613 // Get information about this node
@@ -3621,6 +3622,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
3622 // Consent flags are 1 = Notify, 8 = Prompt, 64 = Privacy Bar.
3623 const authCookieData = { userid: c.uid, domainid: domain.id, nid: c.nid, ip: req.clientIp, p: c.p, gn: c.gn, cf: c.cf, r: 8, expire: c.expire, pid: c.pid, vo: c.vo };
3624 if ((authCookieData.userid == null) && (authCookieData.pid.startsWith('AS:node/'))) { authCookieData.nouser = 1; }
3625 + if (c.k != null) { authCookieData.k = c.k; }
3626 const authCookie = obj.parent.encodeCookie(authCookieData, obj.parent.loginCookieEncryptionKey);
3627
3628 // Server features