From 519615aecc7a1c26d333f28a10d88724024047a9 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Mon, 7 Oct 2024 12:27:53 -0300 Subject: [PATCH] Refs #35818 - Fixed empty file root for dotted filename --- django/core/files/storage/base.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/django/core/files/storage/base.py b/django/core/files/storage/base.py index 31ecbd209a..f8b0e0cdc5 100644 --- a/django/core/files/storage/base.py +++ b/django/core/files/storage/base.py @@ -84,7 +84,15 @@ class Storage: "Detected path traversal attempt in '%s'" % dir_name ) validate_file_name(file_name) - file_ext = "".join(pathlib.PurePath(file_name).suffixes) + # Extract file extensions (.txt, .jpeg, .tar.gz) from the filename + suffixes = pathlib.PurePath(file_name).suffixes + file_ext = "" + # Iterate backwards from the last suffix. Consider suffixes + # shorter than 5 characters to be part of the extension + for index, suffix in enumerate(suffixes[::-1]): + if index > 0 and len(suffix) >= 5: + break + file_ext = suffix + file_ext file_root = file_name.removesuffix(file_ext) # If the filename is not available, generate an alternative # filename until one is available.