Second fix for mesh link escaping.

Ylian Saint-Hilaire committed Dec 30, 2020 at 22:18 UTC 333c7c7876ea3caab833f8df8ff52129d2b8f244
2 files changed +85 -88
db.js
+85 -86
@@ -108,105 +108,104 @@ module.exports.CreateDB = function (parent, func) {
108 // Add this as a valid user identifier
109 validIdentifiers[docs[i]._id] = 1;
110 }
111 + }
112 +
113 + // Fix all of the creating & login to ticks by seconds, not milliseconds.
114 + obj.GetAllType('user', function (err, docs) {
115 + if (err != null) { parent.debug('db', 'ERROR (GetAll user): ' + err); }
116 + if ((err == null) && (docs.length > 0)) {
117 + for (var i in docs) {
118 + var fixed = false;
119 +
120 + // Add this as a valid user identifier
121 + validIdentifiers[docs[i]._id] = 1;
122 +
123 + // Fix email address capitalization
124 + if (docs[i].email && (docs[i].email != docs[i].email.toLowerCase())) {
125 + docs[i].email = docs[i].email.toLowerCase(); fixed = true;
126 + }
127
112 - // Fix all of the creating & login to ticks by seconds, not milliseconds.
113 - obj.GetAllType('user', function (err, docs) {
114 - if (err != null) { parent.debug('db', 'ERROR (GetAll user): ' + err); }
115 - if ((err == null) && (docs.length > 0)) {
116 - for (var i in docs) {
117 - var fixed = false;
118 -
119 - // Add this as a valid user identifier
120 - validIdentifiers[docs[i]._id] = 1;
121 -
122 - // Fix email address capitalization
123 - if (docs[i].email && (docs[i].email != docs[i].email.toLowerCase())) {
124 - docs[i].email = docs[i].email.toLowerCase(); fixed = true;
125 - }
126 -
127 - // Fix account creation
128 - if (docs[i].creation) {
129 - if (docs[i].creation > 1300000000000) { docs[i].creation = Math.floor(docs[i].creation / 1000); fixed = true; }
130 - if ((docs[i].creation % 1) != 0) { docs[i].creation = Math.floor(docs[i].creation); fixed = true; }
131 - }
128 + // Fix account creation
129 + if (docs[i].creation) {
130 + if (docs[i].creation > 1300000000000) { docs[i].creation = Math.floor(docs[i].creation / 1000); fixed = true; }
131 + if ((docs[i].creation % 1) != 0) { docs[i].creation = Math.floor(docs[i].creation); fixed = true; }
132 + }
133
133 - // Fix last account login
134 - if (docs[i].login) {
135 - if (docs[i].login > 1300000000000) { docs[i].login = Math.floor(docs[i].login / 1000); fixed = true; }
136 - if ((docs[i].login % 1) != 0) { docs[i].login = Math.floor(docs[i].login); fixed = true; }
137 - }
134 + // Fix last account login
135 + if (docs[i].login) {
136 + if (docs[i].login > 1300000000000) { docs[i].login = Math.floor(docs[i].login / 1000); fixed = true; }
137 + if ((docs[i].login % 1) != 0) { docs[i].login = Math.floor(docs[i].login); fixed = true; }
138 + }
139
139 - // Fix last password change
140 - if (docs[i].passchange) {
141 - if (docs[i].passchange > 1300000000000) { docs[i].passchange = Math.floor(docs[i].passchange / 1000); fixed = true; }
142 - if ((docs[i].passchange % 1) != 0) { docs[i].passchange = Math.floor(docs[i].passchange); fixed = true; }
143 - }
140 + // Fix last password change
141 + if (docs[i].passchange) {
142 + if (docs[i].passchange > 1300000000000) { docs[i].passchange = Math.floor(docs[i].passchange / 1000); fixed = true; }
143 + if ((docs[i].passchange % 1) != 0) { docs[i].passchange = Math.floor(docs[i].passchange); fixed = true; }
144 + }
145
145 - // Fix subscriptions
146 - if (docs[i].subscriptions != null) { delete docs[i].subscriptions; fixed = true; }
146 + // Fix subscriptions
147 + if (docs[i].subscriptions != null) { delete docs[i].subscriptions; fixed = true; }
148
148 - // Save the user if needed
149 - if (fixed) { obj.Set(docs[i]); }
150 - }
149 + // Save the user if needed
150 + if (fixed) { obj.Set(docs[i]); }
151 + }
152
152 - // Remove all objects that have a "meshid" that no longer points to a valid mesh.
153 - // Fix any incorrectly escaped user identifiers
154 - obj.GetAllType('mesh', function (err, docs) {
155 - if (err != null) { parent.debug('db', 'ERROR (GetAll mesh): ' + err); }
156 - var meshlist = [];
157 - if ((err == null) && (docs.length > 0)) {
158 - for (var i in docs) {
159 - var meshChange = false;
160 - docs[i] = common.unEscapeLinksFieldName(docs[i]);
161 - meshlist.push(docs[i]._id);
162 -
163 - // Make sure all mesh types are number type, if not, fix it.
164 - if (typeof docs[i].mtype == 'string') { docs[i].mtype = parseInt(docs[i].mtype); meshChange = true; }
165 -
166 - // Take a look at the links
167 - if (docs[i].links != null) {
168 - for (var j in docs[i].links) {
169 - if (validIdentifiers[j] == null) {
170 - // This identifier is not known, let see if we can fix it.
171 - var xid = j, xid2 = common.unEscapeFieldName(xid);
172 - while ((xid != xid2) && (validIdentifiers[xid2] == null)) { xid = xid2; xid2 = common.unEscapeFieldName(xid2); }
173 - if (validIdentifiers[xid2] == 1) {
174 - //console.log('Fixing id: ' + j + ' to ' + common.escapeFieldName(xid2));
175 - docs[i].links[xid2] = docs[i].links[j];
176 - delete docs[i].links[j];
177 - meshChange = true;
178 - } else {
179 - // TODO: here, we may want to clean up links to users and user groups that do not exist anymore.
180 - //console.log('Unknown id: ' + j);
181 - }
153 + // Remove all objects that have a "meshid" that no longer points to a valid mesh.
154 + // Fix any incorrectly escaped user identifiers
155 + obj.GetAllType('mesh', function (err, docs) {
156 + if (err != null) { parent.debug('db', 'ERROR (GetAll mesh): ' + err); }
157 + var meshlist = [];
158 + if ((err == null) && (docs.length > 0)) {
159 + for (var i in docs) {
160 + var meshChange = false;
161 + docs[i] = common.unEscapeLinksFieldName(docs[i]);
162 + meshlist.push(docs[i]._id);
163 +
164 + // Make sure all mesh types are number type, if not, fix it.
165 + if (typeof docs[i].mtype == 'string') { docs[i].mtype = parseInt(docs[i].mtype); meshChange = true; }
166 +
167 + // Take a look at the links
168 + if (docs[i].links != null) {
169 + for (var j in docs[i].links) {
170 + if (validIdentifiers[j] == null) {
171 + // This identifier is not known, let see if we can fix it.
172 + var xid = j, xid2 = common.unEscapeFieldName(xid);
173 + while ((xid != xid2) && (validIdentifiers[xid2] == null)) { xid = xid2; xid2 = common.unEscapeFieldName(xid2); }
174 + if (validIdentifiers[xid2] == 1) {
175 + //console.log('Fixing id: ' + j + ' to ' + xid2);
176 + docs[i].links[xid2] = docs[i].links[j];
177 + delete docs[i].links[j];
178 + meshChange = true;
179 + } else {
180 + // TODO: here, we may want to clean up links to users and user groups that do not exist anymore.
181 + //console.log('Unknown id: ' + j);
182 }
183 }
184 }
185 -
186 - // Save the updated device group if needed
187 - if (meshChange) { obj.Set(docs[i]); }
185 }
186 +
187 + // Save the updated device group if needed
188 + if (meshChange) { obj.Set(docs[i]); }
189 }
190 - if ((obj.databaseType == 4) || (obj.databaseType == 5)) {
191 - // MariaDB
192 - sqlDbQuery('DELETE FROM MeshCentral.Main WHERE (extra LIKE ("mesh/%") AND (extra NOT IN ?)', [meshlist], func);
193 - } else if (obj.databaseType == 3) {
194 - // MongoDB
195 - obj.file.deleteMany({ meshid: { $exists: true, $nin: meshlist } }, { multi: true });
196 - } else {
197 - // NeDB or MongoJS
198 - obj.file.remove({ meshid: { $exists: true, $nin: meshlist } }, { multi: true });
199 - }
190 + }
191 + if ((obj.databaseType == 4) || (obj.databaseType == 5)) {
192 + // MariaDB
193 + sqlDbQuery('DELETE FROM MeshCentral.Main WHERE (extra LIKE ("mesh/%") AND (extra NOT IN ?)', [meshlist], func);
194 + } else if (obj.databaseType == 3) {
195 + // MongoDB
196 + obj.file.deleteMany({ meshid: { $exists: true, $nin: meshlist } }, { multi: true });
197 + } else {
198 + // NeDB or MongoJS
199 + obj.file.remove({ meshid: { $exists: true, $nin: meshlist } }, { multi: true });
200 + }
201
201 - // We are done
202 - validIdentifiers = null;
203 - if (func) { func(); }
204 - });
205 - }
206 - });
207 - }
202 + // We are done
203 + validIdentifiers = null;
204 + if (func) { func(); }
205 + });
206 + }
207 + });
208 });
209 -
209 };
210
211 // Get encryption key
meshuser.js
-2
@@ -2765,8 +2765,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2765 }
2766 case 'createmesh':
2767 {
2768 - console.log(command);
2769 -
2768 var err = null;
2769 try {
2770 // Check if we have new group restriction