Skip to content

Commit

Permalink
Fixed #29022 -- Fixed handling protocol-relative URLs in ManifestStat…
Browse files Browse the repository at this point in the history
…icFilesStorage when STATIC_URL is set to /.
  • Loading branch information
adamzap authored and felixxm committed Feb 28, 2024
1 parent ef2434f commit 107aa76
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion django/contrib/staticfiles/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def converter(matchobj):
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
Expand Down
3 changes: 3 additions & 0 deletions tests/staticfiles_tests/project/static_url_slash/ignored.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: url("//foobar");
}
28 changes: 27 additions & 1 deletion tests/staticfiles_tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

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:
Expand Down Expand Up @@ -560,6 +560,32 @@ def test_manifest_hash_v1(self):
self.assertEqual(manifest_content, {"dummy.txt": "dummy.txt"})


@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,
Expand Down

0 comments on commit 107aa76

Please sign in to comment.