From 62f03e371fdcb1db434e182a10e67fca28f50351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 9 Mar 2021 14:47:04 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85=20Fail=20fast=20on=20invalid=20fil?= =?UTF-8?q?e=20names?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/server/api/files.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/octoprint/server/api/files.py b/src/octoprint/server/api/files.py index 324866159..bd3301e57 100644 --- a/src/octoprint/server/api/files.py +++ b/src/octoprint/server/api/files.py @@ -218,6 +218,9 @@ def readGcodeFile(target, filename): if target not in [FileDestinations.LOCAL, FileDestinations.SDCARD]: return make_response("Unknown target", 404) + if filename != fileManager.sanitize(target, filename): + return make_response("File not found", 404) + recursive = False if "recursive" in request.values: recursive = request.values["recursive"] in valid_boolean_trues @@ -736,7 +739,10 @@ def uploadGcodeFile(target): @no_firstrun_access def gcodeFileCommand(filename, target): if target not in [FileDestinations.LOCAL, FileDestinations.SDCARD]: - return make_response("Invalid target", 400) + return make_response("Unknown target", 404) + + if filename != fileManager.sanitize(target, filename): + return make_response("File not found", 404) # valid file commands, dict mapping command name to mandatory parameters valid_commands = { @@ -1112,15 +1118,18 @@ def gcodeFileCommand(filename, target): @no_firstrun_access @Permissions.FILES_DELETE.require(403) def deleteGcodeFile(filename, target): + if filename != fileManager.sanitize(target, filename): + return make_response("File not found", 400) + if not _verifyFileExists(target, filename) and not _verifyFolderExists( target, filename ): - return make_response("File/folder not found", 404) + return make_response("File not found", 404) + + if target not in [FileDestinations.LOCAL, FileDestinations.SDCARD]: + return make_response("Unknown target", 404) if _verifyFileExists(target, filename): - if target not in [FileDestinations.LOCAL, FileDestinations.SDCARD]: - return make_response("Unknown target", 400) - if _isBusy(target, filename): return make_response("Trying to delete a file that is currently in use", 409) @@ -1140,9 +1149,6 @@ def deleteGcodeFile(filename, target): fileManager.remove_file(target, filename) elif _verifyFolderExists(target, filename): - if target not in [FileDestinations.LOCAL]: - return make_response("Unknown target", 400) - if _isBusy(target, filename): return make_response( "Trying to delete a folder that contains a file that is currently in use",