From 3bbd3f7a3e4d1fc2a31957aefa30a169809f70aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 28 Oct 2021 17:08:27 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20URL=20encode=20all=20uploaded=20?= =?UTF-8?q?file=20names?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise a # in the URL might be treated as a fragment and lead to resource mismatches. Closes #4267 --- src/octoprint/server/api/files.py | 35 ++++++++++++++------- src/octoprint/static/js/app/client/files.js | 8 ++--- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/octoprint/server/api/files.py b/src/octoprint/server/api/files.py index 6453ce070..b76f2c737 100644 --- a/src/octoprint/server/api/files.py +++ b/src/octoprint/server/api/files.py @@ -37,6 +37,11 @@ from octoprint.server.util.flask import ( from octoprint.settings import settings, valid_boolean_trues from octoprint.util import sv, time_this +try: + from urllib.parse import quote as urlquote +except ImportError: + from urllib import quote as urlquote # noqa: F401 + # ~~ GCODE file handling _file_cache = {} @@ -325,7 +330,7 @@ def _getFileList( "resource": url_for( ".readGcodeFile", target=FileDestinations.SDCARD, - filename=f["name"], + filename=urlquote(f["name"]), _external=True, ) }, @@ -387,7 +392,7 @@ def _getFileList( "resource": url_for( ".readGcodeFile", target=FileDestinations.LOCAL, - filename=path + file_or_folder["name"], + filename=urlquote(path + file_or_folder["name"]), _external=True, ) } @@ -441,14 +446,14 @@ def _getFileList( "resource": url_for( ".readGcodeFile", target=FileDestinations.LOCAL, - filename=file_or_folder["path"], + filename=urlquote(file_or_folder["path"]), _external=True, ), "download": url_for("index", _external=True) + "downloads/files/" + FileDestinations.LOCAL + "/" - + file_or_folder["path"], + + urlquote(file_or_folder["path"]), } result.append(file_or_folder) @@ -687,7 +692,7 @@ def uploadGcodeFile(target): location = url_for( ".readGcodeFile", target=FileDestinations.LOCAL, - filename=filename, + filename=urlquote(filename), _external=True, ) files.update( @@ -702,7 +707,7 @@ def uploadGcodeFile(target): + "downloads/files/" + FileDestinations.LOCAL + "/" - + filename, + + urlquote(filename), }, } } @@ -712,7 +717,7 @@ def uploadGcodeFile(target): location = url_for( ".readGcodeFile", target=FileDestinations.SDCARD, - filename=sdFilename, + filename=urlquote(sdFilename), _external=True, ) files.update( @@ -764,7 +769,7 @@ def uploadGcodeFile(target): location = url_for( ".readGcodeFile", target=FileDestinations.LOCAL, - filename=added_folder, + filename=urlquote(added_folder), _external=True, ) folder = { @@ -1034,7 +1039,10 @@ def gcodeFileCommand(filename, target): abort(404, description="Unknown profile") location = url_for( - ".readGcodeFile", target=target, filename=full_path, _external=True + ".readGcodeFile", + target=target, + filename=urlquote(full_path), + _external=True, ) result = { "name": destination, @@ -1047,7 +1055,7 @@ def gcodeFileCommand(filename, target): + "downloads/files/" + target + "/" - + full_path, + + urlquote(full_path), }, } @@ -1154,7 +1162,10 @@ def gcodeFileCommand(filename, target): fileManager.move_folder(target, filename, destination) location = url_for( - ".readGcodeFile", target=target, filename=destination, _external=True + ".readGcodeFile", + target=target, + filename=urlquote(destination), + _external=True, ) result = { "name": name, @@ -1168,7 +1179,7 @@ def gcodeFileCommand(filename, target): + "downloads/files/" + target + "/" - + destination + + urlquote(destination) ) r = make_response(jsonify(result), 201) diff --git a/src/octoprint/static/js/app/client/files.js b/src/octoprint/static/js/app/client/files.js index 517f57d1e..dbbde7ede 100644 --- a/src/octoprint/static/js/app/client/files.js +++ b/src/octoprint/static/js/app/client/files.js @@ -14,19 +14,19 @@ }; var resourceForLocation = function (location) { - return url + "/" + location; + return url + "/" + encodeURIComponent(location); }; var downloadForLocation = function (location) { - return downloadUrl + "/" + location; + return downloadUrl + "/" + encodeURIComponent(location); }; var downloadForEntry = function (location, filename) { - return downloadForLocation(location) + "/" + filename; + return downloadForLocation(location) + "/" + encodeURIComponent(filename); }; var resourceForEntry = function (location, filename) { - return resourceForLocation(location) + "/" + filename; + return resourceForLocation(location) + "/" + encodeURIComponent(filename); }; var preProcessList = function (response) {