Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion django/contrib/staticfiles/handlers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from urllib.parse import urlparse
from urllib.request import url2pathname

Expand Down Expand Up @@ -34,8 +35,16 @@ def _should_handle(self, path):
Check if the path should be handled. Ignore the path if:
* the host is provided as part of the base_url
* the request's path isn't under the media path (or equal)
* the path contains a special character (':' or '|')
"""
return path.startswith(self.base_url[2]) and not self.base_url[1]
valid_path_nt_platform = (
os.name == 'nt' and ':' not in path and '|' not in path
)
return (
path.startswith(self.base_url[2])
and not self.base_url[1]
and valid_path_nt_platform
)

def file_path(self, url):
"""
Expand Down
15 changes: 15 additions & 0 deletions tests/staticfiles_tests/test_liveserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import os
from urllib.error import HTTPError
from urllib.request import urlopen

from django.contrib.staticfiles.testing import StaticLiveServerTestCase
Expand Down Expand Up @@ -98,3 +99,17 @@ def test_collectstatic_emulation(self):
"""
with self.urlopen('/static/test/file.txt') as f:
self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.')

# The test is going to access a non-existent static file with a special character.
@modify_settings(INSTALLED_APPS={'append': 'staticfiles_tests.apps.test'})
def test_staticfiles_special_characters(self):
"""
StaticLiveServerTestCase fails on Windows with special characters
(':' or '|')
"""
for filename in ('/static/test/file:abc.txt', '/static/test/file|abc.txt'):
with self.subTest(filename=filename):
with self.assertRaises(HTTPError) as err:
self.urlopen(filename)
err.exception.close()
self.assertEqual(err.exception.code, 404, 'Expected 404 response')