Fixed server crash when downloading odd files.

Ylian Saint-Hilaire committed Oct 21, 2020 at 20:04 UTC 9fadfd73fb2ae966652de5ff44f7fc1b74cfedb2
1 file changed +11 -2
meshdevicefile.js
+11 -2
@@ -203,10 +203,19 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us
203 try { cmd = JSON.parse(data); } catch (ex) { }
204 if ((cmd == null) || (typeof cmd.op == 'string')) {
205 if (cmd.op == 'ok') {
206 + var filename = require('path').basename(this.file).split('\\').join('').split('/').join('').split(':').join('').split('*').join('').split('?').join('').split('"').join('').split('<').join('').split('>').join('').split('|').join('').split(' ').join('').split('\'').join('');
207 if (typeof cmd.size == 'number') {
207 - this.res.set({ 'Cache-Control': 'no-store', 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename="' + require('path').basename(this.file) + '"', 'Content-Length': cmd.size });
208 + try {
209 + this.res.set({ 'Cache-Control': 'no-store', 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename="' + filename + '"', 'Content-Length': cmd.size });
210 + } catch (ex) {
211 + this.res.set({ 'Cache-Control': 'no-store', 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename="file.bin"', 'Content-Length': cmd.size });
212 + }
213 } else {
209 - this.res.set({ 'Cache-Control': 'no-store', 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename="' + require('path').basename(this.file) + '"' });
214 + try {
215 + this.res.set({ 'Cache-Control': 'no-store', 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename="' + filename + '"' });
216 + } catch (ex) {
217 + this.res.set({ 'Cache-Control': 'no-store', 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename="file.bin"'});
218 + }
219 }
220 } else {
221 try { this.res.sendStatus(401); } catch (ex) { }