Fixed #32304 -- Fixed prefixing STATIC_URL and MEDIA_URL by SCRIPT_NAME for absolute URLs with no domain.

Thanks Adam Hooper for the report.

Regression in c574bec092.
This commit is contained in:
Mariusz Felisiak 2020-12-31 13:18:57 +01:00 committed by GitHub
parent a2e3f95b09
commit e13b71403b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 11 deletions

View File

@ -15,8 +15,7 @@ from pathlib import Path
import django import django
from django.conf import global_settings from django.conf import global_settings
from django.core.exceptions import ImproperlyConfigured, ValidationError from django.core.exceptions import ImproperlyConfigured
from django.core.validators import URLValidator
from django.utils.deprecation import RemovedInDjango40Warning from django.utils.deprecation import RemovedInDjango40Warning
from django.utils.functional import LazyObject, empty from django.utils.functional import LazyObject, empty
@ -132,14 +131,8 @@ class LazySettings(LazyObject):
Useful when the app is being served at a subpath and manually prefixing Useful when the app is being served at a subpath and manually prefixing
subpath to STATIC_URL and MEDIA_URL in settings is inconvenient. subpath to STATIC_URL and MEDIA_URL in settings is inconvenient.
""" """
# Don't apply prefix to valid URLs. # Don't apply prefix to absolute paths and URLs.
try: if value.startswith(('http://', 'https://', '/')):
URLValidator()(value)
return value
except (ValidationError, AttributeError):
pass
# Don't apply prefix to absolute paths.
if value.startswith('/'):
return value return value
from django.urls import get_script_prefix from django.urls import get_script_prefix
return '%s%s' % (get_script_prefix(), value) return '%s%s' % (get_script_prefix(), value)

View File

@ -16,3 +16,9 @@ Bugfixes
* Fixed a bug in Django 3.1 that caused a crash when processing middlewares in * Fixed a bug in Django 3.1 that caused a crash when processing middlewares in
an async context with a middleware that raises a ``MiddlewareNotUsed`` an async context with a middleware that raises a ``MiddlewareNotUsed``
exception (:ticket:`32299`). exception (:ticket:`32299`).
* Fixed a regression in Django 3.1 that caused the incorrect prefixing of
``STATIC_URL`` and ``MEDIA_URL`` settings, by the server-provided value of
``SCRIPT_NAME`` (or ``/`` if not set), when set to a URL specifying the
protocol but without a top-level domain, e.g. ``http://myhost/``
(:ticket:`32304`).

View File

@ -573,10 +573,12 @@ class MediaURLStaticURLPrefixTest(SimpleTestCase):
set_script_prefix(val) set_script_prefix(val)
def test_not_prefixed(self): def test_not_prefixed(self):
# Don't add SCRIPT_NAME prefix to valid URLs, absolute paths or None. # Don't add SCRIPT_NAME prefix to absolute paths, URLs, or None.
tests = ( tests = (
'/path/', '/path/',
'http://myhost.com/path/', 'http://myhost.com/path/',
'http://myhost/path/',
'https://myhost/path/',
None, None,
) )
for setting in ('MEDIA_URL', 'STATIC_URL'): for setting in ('MEDIA_URL', 'STATIC_URL'):