diff options
| author | Adam Zapletal <adamzap@gmail.com> | 2024-02-23 22:36:15 -0600 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2024-02-28 06:24:24 +0100 |
| commit | 107aa76bcf5d5599460fdce61dfa15bb147acc62 (patch) | |
| tree | 2c84920ff7630a1914698e3e65ef9a1c42b2ecca | |
| parent | ef2434f8508551fee183079ab471b1dc325c7acb (diff) | |
Fixed #29022 -- Fixed handling protocol-relative URLs in ManifestStaticFilesStorage when STATIC_URL is set to /.
| -rw-r--r-- | django/contrib/staticfiles/storage.py | 2 | ||||
| -rw-r--r-- | tests/staticfiles_tests/project/static_url_slash/ignored.css | 3 | ||||
| -rw-r--r-- | tests/staticfiles_tests/test_storage.py | 28 |
3 files changed, 31 insertions, 2 deletions
diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py index 85172ea42d..191fe3cbb5 100644 --- a/django/contrib/staticfiles/storage.py +++ b/django/contrib/staticfiles/storage.py @@ -221,7 +221,7 @@ class HashedFilesMixin: url = matches["url"] # Ignore absolute/protocol-relative and data-uri URLs. - if re.match(r"^[a-z]+:", url): + if re.match(r"^[a-z]+:", url) or url.startswith("//"): return matched # Ignore absolute URLs that don't point to a static file (dynamic diff --git a/tests/staticfiles_tests/project/static_url_slash/ignored.css b/tests/staticfiles_tests/project/static_url_slash/ignored.css new file mode 100644 index 0000000000..369ff04632 --- /dev/null +++ b/tests/staticfiles_tests/project/static_url_slash/ignored.css @@ -0,0 +1,3 @@ +body { + background: url("//foobar"); +} diff --git a/tests/staticfiles_tests/test_storage.py b/tests/staticfiles_tests/test_storage.py index 1e537dfe54..469d5ec690 100644 --- a/tests/staticfiles_tests/test_storage.py +++ b/tests/staticfiles_tests/test_storage.py @@ -22,7 +22,7 @@ from .settings import TEST_ROOT def hashed_file_path(test, path): fullpath = test.render_template(test.static_template_snippet(path)) - return fullpath.replace(settings.STATIC_URL, "") + return fullpath.removeprefix(settings.STATIC_URL) class TestHashedFiles: @@ -561,6 +561,32 @@ class TestCollectionManifestStorage(TestHashedFiles, CollectionTestCase): @override_settings( + STATIC_URL="/", + STORAGES={ + **settings.STORAGES, + STATICFILES_STORAGE_ALIAS: { + "BACKEND": "django.contrib.staticfiles.storage.ManifestStaticFilesStorage", + }, + }, +) +class TestCollectionManifestStorageStaticUrlSlash(CollectionTestCase): + run_collectstatic_in_setUp = False + hashed_file_path = hashed_file_path + + def test_protocol_relative_url_ignored(self): + with override_settings( + STATICFILES_DIRS=[os.path.join(TEST_ROOT, "project", "static_url_slash")], + STATICFILES_FINDERS=["django.contrib.staticfiles.finders.FileSystemFinder"], + ): + self.run_collectstatic() + relpath = self.hashed_file_path("ignored.css") + self.assertEqual(relpath, "ignored.61707f5f4942.css") + with storage.staticfiles_storage.open(relpath) as relfile: + content = relfile.read() + self.assertIn(b"//foobar", content) + + +@override_settings( STORAGES={ **settings.STORAGES, STATICFILES_STORAGE_ALIAS: { |
