Removed clonings.

Ylian Saint-Hilaire committed Oct 11, 2019 at 11:16 UTC 1b9778d4ed601c7336554be85738111d043f802f
8 files changed +62 -66
agents/meshcore.js
+1 -1
@@ -2427,7 +2427,7 @@ function createMeshCore(agent) {
2427 if ((flags & 4) && (process.platform == 'win32')) {
2428 // Update anti-virus information
2429 var av, pr;
2430 - try { av = require('win-info').av(); } catch (ex) { av = []; } // Antivirus
2430 + try { av = require('win-info').av(); } catch (ex) { av = null; } // Antivirus
2431 //if (process.platform == 'win32') { try { pr = require('win-info').pendingReboot(); } catch (ex) { pr = null; } } // Pending reboot
2432 if ((meshCoreObj.av == null) || (JSON.stringify(meshCoreObj.av) != JSON.stringify(av))) { meshCoreObj.av = av; mesh.SendCommand(meshCoreObj); }
2433 }
amtscanner.js
+1 -3
@@ -332,9 +332,7 @@ module.exports.CreateAmtScanner = function (parent) {
332
333 // Event the node change
334 event.msg = 'Intel® AMT changed device ' + node.name + ' from mesh ' + mesh.name + ': ' + changes.join(', ');
335 - var node2 = obj.parent.common.Clone(node);
336 - if (node2.intelamt && node2.intelamt.pass) delete node2.intelamt.pass; // Remove the Intel AMT password before eventing this.
337 - event.node = node2;
335 + event.node = obj.parent.webserver.CloneSafeNode(node);
336 if (obj.parent.db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
337 obj.parent.DispatchEvent(['*', node.meshid], obj, event);
338 }
apfserver.js
+3 -9
@@ -248,10 +248,8 @@ module.exports.CreateApfServer = function (parent, db, args) {
248
249 // Event the new node
250 addedDeviceCount++;
251 - var device2 = common.Clone(device);
252 - if (device2.intelamt.pass != null) delete device2.intelamt.pass; // Remove the Intel AMT password before eventing this.
251 var change = 'APF added device ' + socket.tag.name + ' to group ' + mesh.name;
254 - obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: device2, msg: change, domain: mesh.domain });
252 + obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: mesh.domain });
253
254 // Add the connection to the APF connection list
255 obj.apfConnections[socket.tag.nodeid] = socket;
@@ -268,10 +266,8 @@ module.exports.CreateApfServer = function (parent, db, args) {
266
267 // Event the new node
268 addedDeviceCount++;
271 - var device2 = common.Clone(device);
272 - if (device2.intelamt.pass != null) delete device2.intelamt.pass; // Remove the Intel AMT password before eventing this.
269 var change = 'APF added device ' + socket.tag.name + ' to group ' + mesh.name;
274 - obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: device2, msg: change, domain: mesh.domain });
270 + obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: mesh.domain });
271 }
272 } else {
273 // Node is already present
@@ -717,9 +713,7 @@ module.exports.CreateApfServer = function (parent, db, args) {
713
714 // Event the node change
715 event.msg = 'APF changed device ' + node.name + ' from group ' + mesh.name + ': ' + changes.join(', ');
720 - var node2 = common.Clone(node);
721 - if (node2.intelamt && node2.intelamt.pass) delete node2.intelamt.pass; // Remove the Intel AMT password before eventing this.
722 - event.node = node2;
716 + event.node = parent.webserver.CloneSafeNode(node);
717 if (obj.db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
718 obj.parent.DispatchEvent(['*', node.meshid], obj, event);
719 });
meshagent.js
+4 -16
@@ -709,11 +709,8 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
709 if (obj.agentInfo.capabilities & 0x20) { log = 0; }
710
711 // Event the node change
712 - var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id };
712 + var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id, node: parent.CloneSafeNode(device) };
713 if (log == 0) { event.nolog = 1; } else { event.msg = 'Changed device ' + device.name + ' from group ' + mesh.name + ': ' + changes.join(', '); }
714 - const device2 = common.Clone(device);
715 - if (device2.intelamt && device2.intelamt.pass) delete device2.intelamt.pass; // Remove the Intel AMT password before eventing this.
716 - event.node = device;
714 if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
715 parent.parent.DispatchEvent(['*', device.meshid], obj, event);
716 }
@@ -1359,12 +1356,9 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1356 db.Set(device);
1357
1358 // Event the node change
1362 - var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id };
1359 + var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id, node: parent.CloneSafeNode(device) };
1360 if (changes.length > 0) { event.msg = 'Changed device ' + device.name + ' from group ' + mesh.name + ': ' + changes.join(', '); }
1361 if ((log == 0) || ((obj.agentInfo) && (obj.agentInfo.capabilities) && (obj.agentInfo.capabilities & 0x20)) || (changes.length == 0)) { event.nolog = 1; } // If this is a temporary device, don't log changes
1365 - var device2 = common.Clone(device);
1366 - if (device2.intelamt && device2.intelamt.pass) { delete device2.intelamt.pass; } // Remove the Intel AMT password before eventing this.
1367 - event.node = device;
1362 if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
1363 parent.parent.DispatchEvent(['*', device.meshid], obj, event);
1364 }
@@ -1404,11 +1398,8 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1398 db.Set(device);
1399
1400 // Event the node change
1407 - var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id, msg: 'Changed device ' + device.name + ' from group ' + mesh.name + ': ' + changes.join(', ') };
1401 + var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id, node: parent.CloneSafeNode(device), msg: 'Changed device ' + device.name + ' from group ' + mesh.name + ': ' + changes.join(', ') };
1402 if (obj.agentInfo.capabilities & 0x20) { event.nolog = 1; } // If this is a temporary device, don't log changes
1409 - var device2 = common.Clone(device);
1410 - if (device2.intelamt && device2.intelamt.pass) { delete device2.intelamt.pass; } // Remove the Intel AMT password before eventing this.
1411 - event.node = device;
1403 if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
1404 parent.parent.DispatchEvent(['*', device.meshid], obj, event);
1405 }
@@ -1437,10 +1428,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1428 db.Set(device);
1429
1430 // Event the node change
1440 - var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id, nolog: 1 };
1441 - var device2 = common.Clone(device);
1442 - if (device2.intelamt && device2.intelamt.pass) delete device2.intelamt.pass; // Remove the Intel AMT password before eventing this.
1443 - event.node = device;
1431 + var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id, node: parent.CloneSafeNode(device), nolog: 1 };
1432 if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
1433 parent.parent.DispatchEvent(['*', device.meshid], obj, event);
1434 }
meshcentral.js
+2 -2
@@ -1449,7 +1449,7 @@ function CreateMeshCentralServer(config, args) {
1449 });
1450 stream.on('end', function () {
1451 // Add the agent to the agent table with all information and the hash
1452 - obj.meshAgentInstallScripts[this.info.id] = obj.common.Clone(this.info);
1452 + obj.meshAgentInstallScripts[this.info.id] = Object.assign({}, this.info);
1453 obj.meshAgentInstallScripts[this.info.id].hash = this.hash.digest('hex');
1454 obj.meshAgentInstallScripts[this.info.id].path = this.agentpath;
1455 obj.meshAgentInstallScripts[this.info.id].data = this.xdata;
@@ -1515,7 +1515,7 @@ function CreateMeshCentralServer(config, args) {
1515 if ((stats != null)) {
1516 // If file exists
1517 archcount++;
1518 - obj.meshAgentBinaries[archid] = obj.common.Clone(obj.meshAgentsArchitectureNumbers[archid]);
1518 + obj.meshAgentBinaries[archid] = Object.assign({}, obj.meshAgentsArchitectureNumbers[archid]);
1519 obj.meshAgentBinaries[archid].path = agentpath;
1520 obj.meshAgentBinaries[archid].url = ((obj.args.notls == true) ? 'http://' : 'https://') + obj.certificates.CommonName + ':' + ((typeof obj.args.aliasport == 'number') ? obj.args.aliasport : obj.args.port) + '/meshagents?id=' + archid;
1521 obj.meshAgentBinaries[archid].size = stats.size;
meshuser.js
+5 -13
@@ -400,12 +400,8 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
400 var docs = [];
401 for (i in user.links) {
402 if ((parent.meshes[i]) && (parent.meshes[i].deleted == null)) {
403 - if (parent.meshes[i].amt && parent.meshes[i].amt.password) {
404 - // Remove the Intel AMT password if present
405 - var m = common.Clone(parent.meshes[i]); delete m.amt.password; docs.push(m);
406 - } else {
407 - docs.push(parent.meshes[i]);
408 - }
403 + // Remove the Intel AMT password if present
404 + docs.push(parent.CloneSafeMesh(parent.meshes[i]));
405 }
406 }
407 try { ws.send(JSON.stringify({ action: 'meshes', meshes: docs, tag: command.tag })); } catch (ex) { }
@@ -1911,7 +1907,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1907 else if (command.amtpolicy.type === 3) { amtpolicy = { type: command.amtpolicy.type, password: command.amtpolicy.password, cirasetup: command.amtpolicy.cirasetup }; }
1908 mesh.amt = amtpolicy;
1909 db.Set(common.escapeLinksFieldName(mesh));
1914 - var amtpolicy2 = common.Clone(amtpolicy);
1910 + var amtpolicy2 = Object.assign({}, amtpolicy); // Shallow clone
1911 delete amtpolicy2.password;
1912 var event = { etype: 'mesh', userid: user._id, username: user.name, meshid: mesh._id, amt: amtpolicy2, action: 'meshchange', msg: change, domain: domain.id };
1913 if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the mesh. Another event will come.
@@ -1959,9 +1955,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1955 db.Set(device);
1956
1957 // Event the new node
1962 - var device2 = common.Clone(device);
1963 - delete device2.intelamt.pass; // Remove the Intel AMT password before eventing this.
1964 - parent.parent.DispatchEvent(['*', command.meshid], obj, { etype: 'node', userid: user._id, username: user.name, action: 'addnode', node: device2, msg: 'Added device ' + command.devicename + ' to mesh ' + mesh.name, domain: domain.id });
1958 + parent.parent.DispatchEvent(['*', command.meshid], obj, { etype: 'node', userid: user._id, username: user.name, action: 'addnode', node: parent.CloneSafeNode(device), msg: 'Added device ' + command.devicename + ' to mesh ' + mesh.name, domain: domain.id });
1959 });
1960 }
1961 break;
@@ -2312,9 +2306,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2306
2307 // Event the node change. Only do this if the database will not do it.
2308 event.msg = 'Changed device ' + node.name + ' from group ' + mesh.name + ': ' + changes.join(', ');
2315 - var node2 = common.Clone(node);
2316 - if (node2.intelamt && node2.intelamt.pass) delete node2.intelamt.pass; // Remove the Intel AMT password before eventing this.
2317 - event.node = node2;
2309 + event.node = parent.CloneSafeNode(device);
2310 if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
2311 parent.parent.DispatchEvent(['*', node.meshid, user._id], obj, event);
2312 }
mpsserver.js
+5 -15
@@ -298,10 +298,8 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
298
299 // Event the new node
300 addedTlsDeviceCount++;
301 - var device2 = common.Clone(device);
302 - if (device2.intelamt.pass != null) delete device2.intelamt.pass; // Remove the Intel AMT password before eventing this.
301 var change = 'CIRA added device ' + socket.tag.name + ' to mesh ' + mesh.name;
304 - obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: device2, msg: change, domain: domainid });
302 + obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: domainid });
303
304 // Add the connection to the MPS connection list
305 obj.ciraConnections[socket.tag.nodeid] = socket;
@@ -316,10 +314,8 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
314
315 // Event the new node
316 addedTlsDeviceCount++;
319 - var device2 = common.Clone(device);
320 - if (device2.intelamt.pass != null) delete device2.intelamt.pass; // Remove the Intel AMT password before eventing this.
317 var change = 'CIRA added device ' + socket.tag.name + ' to mesh ' + mesh.name;
322 - obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: device2, msg: change, domain: domainid });
318 + obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: domainid });
319 }
320 } else {
321 // New CIRA connection for unknown node, disconnect.
@@ -440,10 +436,8 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
436
437 // Event the new node
438 addedDeviceCount++;
443 - var device2 = common.Clone(device);
444 - if (device2.intelamt.pass != null) delete device2.intelamt.pass; // Remove the Intel AMT password before eventing this.
439 var change = 'CIRA added device ' + socket.tag.name + ' to group ' + mesh.name;
446 - obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: device2, msg: change, domain: mesh.domain });
440 + obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: mesh.domain });
441
442 // Add the connection to the MPS connection list
443 obj.ciraConnections[socket.tag.nodeid] = socket;
@@ -459,10 +453,8 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
453
454 // Event the new node
455 addedDeviceCount++;
462 - var device2 = common.Clone(device);
463 - if (device2.intelamt.pass != null) delete device2.intelamt.pass; // Remove the Intel AMT password before eventing this.
456 var change = 'CIRA added device ' + socket.tag.name + ' to group ' + mesh.name;
465 - obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: device2, msg: change, domain: mesh.domain });
457 + obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: mesh.domain });
458 }
459 } else {
460 // Node is already present
@@ -925,9 +917,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
917
918 // Event the node change
919 event.msg = 'CIRA changed device ' + node.name + ' from group ' + mesh.name + ': ' + changes.join(', ');
928 - var node2 = common.Clone(node);
929 - if (node2.intelamt && node2.intelamt.pass) delete node2.intelamt.pass; // Remove the Intel AMT password before eventing this.
930 - event.node = node2;
920 + event.node = parent.webserver.CloneSafeNode(node);
921 if (obj.db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
922 obj.parent.DispatchEvent(['*', node.meshid], obj, event);
923 });
webserver.js
+41 -7
@@ -2599,7 +2599,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
2599 db.Set(device);
2600
2601 // Event the new node
2602 - var device2 = obj.common.Clone(device);
2602 + var device2 = Object.assign({}, device); // Shallow clone
2603 + device2.intelamt = Object.assign({}, device2.intelamt); // Shallow clone
2604 delete device2.intelamt.pass; // Remove the Intel AMT password before eventing this.
2605 parent.DispatchEvent(['*', ws.meshid], obj, { etype: 'node', action: 'addnode', node: device2, msg: 'Added device ' + ws.xxstate.name + ' to mesh ' + mesh.name, domain: domain.id });
2606 });
@@ -2624,7 +2625,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
2625 db.Set(device);
2626
2627 // Event the new node
2627 - var device2 = obj.common.Clone(device);
2628 + var device2 = Object.assign({}, device); // Shallow clone
2629 + device2.intelamt = Object.assign({}, device2.intelamt); // Shallow clone
2630 delete device2.intelamt.pass; // Remove the Intel AMT password before eventing this.
2631 if (obj.db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
2632 parent.DispatchEvent(['*', ws.meshid], obj, { etype: 'node', action: 'changenode', nodeid: device2._id, node: device2, msg: 'Changed device ' + device.name + ' in mesh ' + mesh.name, domain: domain.id });
@@ -2736,9 +2738,15 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
2738
2739 // Event the node change
2740 var event = { etype: 'node', action: 'changenode', nodeid: node._id, domain: domain.id, msg: 'Intel(R) AMT host change ' + node.name + ' from group ' + mesh.name + ': ' + oldname + ' to ' + amthost };
2739 - var node2 = obj.common.Clone(node);
2740 - if (node2.intelamt && node2.intelamt.pass) delete node2.intelamt.pass; // Remove the Intel AMT password before eventing this.
2741 - event.node = node2;
2741 +
2742 + // Remove the Intel AMT password before eventing this.
2743 + event.node = node;
2744 + if (event.node.intelamt && event.node.intelamt.pass) {
2745 + event.node = Object.assign({}, event.node); // Shallow clone
2746 + event.node.intelamt = Object.assign({}, event.node.intelamt); // Shallow clone
2747 + delete event.node.intelamt.pass;
2748 + }
2749 +
2750 if (obj.db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
2751 obj.parent.DispatchEvent(['*', node.meshid], obj, event);
2752 }
@@ -3218,12 +3226,14 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
3226 res.set(domain.httpheaders);
3227 } else {
3228 // Use default security headers
3229 + var geourl = (domain.geolocation ? ' *.openstreetmap.org' : '');
3230 + var selfurl = ((args.notls !== true) ? (" wss://" + req.headers.host) : (" ws://" + req.headers.host));
3231 res.set({
3232 "X-Frame-Options": "sameorigin",
3233 "Referrer-Policy": "no-referrer",
3234 "X-XSS-Protection": "1; mode=block",
3235 "X-Content-Type-Options": "nosniff",
3226 - "Content-Security-Policy": "default-src 'none'; script-src 'self' 'unsafe-inline'; connect-src 'self' ws" + ((args.notls !== true) ? 's' : '') + "://" + req.headers.host + "; img-src 'self' data:; style-src 'self' 'unsafe-inline'; frame-src 'self'; media-src 'self'"
3236 + "Content-Security-Policy": "default-src 'none'; script-src 'self' 'unsafe-inline'; connect-src 'self'" + geourl + selfurl + "; img-src 'self'" + geourl + " data:; style-src 'self' 'unsafe-inline'; frame-src 'self'; media-src 'self'"
3237 });
3238 }
3239
@@ -3701,7 +3711,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
3711 // Clone a safe version of a user object, remove everything that is secret.
3712 obj.CloneSafeUser = function (user) {
3713 if (typeof user != 'object') { return user; }
3704 - var user2 = obj.common.Clone(user);
3714 + var user2 = Object.assign({}, user); // Shallow clone
3715 delete user2.hash;
3716 delete user2.passhint;
3717 delete user2.salt;
@@ -3715,6 +3725,30 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
3725 return user2;
3726 }
3727
3728 + // Clone a safe version of a node object, remove everything that is secret.
3729 + obj.CloneSafeNode = function (node) {
3730 + if (typeof node != 'object') { return node; }
3731 + var r = node;
3732 + if (r.intelamt && r.intelamt.pass) {
3733 + r = Object.assign({}, r); // Shallow clone
3734 + r.intelamt = Object.assign({}, r.intelamt); // Shallow clone
3735 + delete r.intelamt.pass; // Remove the Intel AMT password from the node
3736 + }
3737 + return r;
3738 + }
3739 +
3740 + // Clone a safe version of a mesh object, remove everything that is secret.
3741 + obj.CloneSafeMesh = function (mesh) {
3742 + if (typeof mesh != 'object') { return mesh; }
3743 + var r = mesh;
3744 + if (r.amt && r.amt.password) {
3745 + r = Object.assign({}, r); // Shallow clone
3746 + r.amt = Object.assign({}, r.amt); // Shallow clone
3747 + delete r.amt.password; // Remove the Intel AMT password from the policy
3748 + }
3749 + return r;
3750 + }
3751 +
3752 // Return the correct render page given mobile, minify and override path.
3753 function getRenderPage(pagename, req) {
3754 var mobile = isMobileBrowser(req), minify = obj.args.minify && !req.query.nominify, p;