From fe717900a08c81efdae6591bdc77f2edd8480dd1 Mon Sep 17 00:00:00 2001 From: James Bligh Date: Sun, 17 Aug 2025 21:00:59 +0100 Subject: [PATCH] Fixed #26329 -- Support leading slashes and backslashes on static files URLs --- django/contrib/staticfiles/storage.py | 2 +- tests/staticfiles_tests/test_storage.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py index b16c77757c..5bfadb3857 100644 --- a/django/contrib/staticfiles/storage.py +++ b/django/contrib/staticfiles/storage.py @@ -178,7 +178,7 @@ class HashedFilesMixin: if urlsplit(clean_name).path.endswith("/"): # don't hash paths hashed_name = name else: - args = (clean_name,) + args = (self.clean_name(clean_name).lstrip("/"),) if hashed_files is not None: args += (hashed_files,) hashed_name = hashed_name_func(*args) diff --git a/tests/staticfiles_tests/test_storage.py b/tests/staticfiles_tests/test_storage.py index e09f9eda1c..260e60e24f 100644 --- a/tests/staticfiles_tests/test_storage.py +++ b/tests/staticfiles_tests/test_storage.py @@ -368,6 +368,22 @@ class TestHashedFiles: self.assertEqual("Post-processing 'nonutf8.css' failed!\n\n", err.getvalue()) self.assertPostCondition() + def test_leading_slash(self): + self.assertStaticRenders("/test/file.txt", "/static/test/file.dad0999e4f8f.txt") + self.assertStaticRenders( + "/test/file.txt", "/static/test/file.dad0999e4f8f.txt", asvar=True + ) + self.assertStaticRenders( + "/cached/styles.css", "/static/cached/styles.5e0040571e1a.css" + ) + self.assertStaticRenders("/path/", "/static/path/") + self.assertStaticRenders("/path/?query", "/static/path/?query") + self.assertPostCondition() + + def test_backslash_separator(self): + self.assertStaticRenders(r"test\file.txt", "/static/test/file.dad0999e4f8f.txt") + self.assertPostCondition() + @override_settings( STORAGES={