Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid silently changing settings; raise ImproperlyConfigured instead #520

Merged
merged 1 commit into from
Jul 6, 2018
Merged
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
5 changes: 3 additions & 2 deletions storages/backends/gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.utils.deconstruct import deconstructible
from django.utils.encoding import force_bytes, smart_str

from storages.utils import clean_name, safe_join, setting
from storages.utils import check_location, clean_name, safe_join, setting

try:
from google.cloud.storage.client import Client
Expand Down Expand Up @@ -101,7 +101,8 @@ def __init__(self, **settings):
if hasattr(self, name):
setattr(self, name, value)

self.location = (self.location or '').lstrip('/')
check_location(self)

self._bucket = None
self._client = None

Expand Down
5 changes: 3 additions & 2 deletions storages/backends/s3boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)
from django.utils.six import BytesIO

from storages.utils import clean_name, safe_join, setting
from storages.utils import check_location, clean_name, safe_join, setting

try:
from boto import __version__ as boto_version
Expand Down Expand Up @@ -235,7 +235,8 @@ def __init__(self, acl=None, bucket=None, **settings):
if bucket is not None:
self.bucket_name = bucket

self.location = (self.location or '').lstrip('/')
check_location(self)

# Backward-compatibility: given the anteriority of the SECURE_URL setting
# we fall back to https if specified in order to avoid the construction
# of unsecure urls.
Expand Down
5 changes: 3 additions & 2 deletions storages/backends/s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from django.utils.six.moves.urllib import parse as urlparse
from django.utils.timezone import is_naive, localtime

from storages.utils import safe_join, setting
from storages.utils import check_location, safe_join, setting

try:
import boto3.session
Expand Down Expand Up @@ -237,7 +237,8 @@ def __init__(self, acl=None, bucket=None, **settings):
if bucket is not None:
self.bucket_name = bucket

self.location = (self.location or '').lstrip('/')
check_location(self)

# Backward-compatibility: given the anteriority of the SECURE_URL setting
# we fall back to https if specified in order to avoid the construction
# of unsecure urls.
Expand Down
12 changes: 12 additions & 0 deletions storages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ def safe_join(base, *paths):
' component')

return final_path.lstrip('/')


def check_location(storage):
if storage.location.startswith('/'):
correct = storage.location.lstrip('/')
raise ImproperlyConfigured(
"%s.location cannot begin with a leading slash. Found '%s'. Use '%s' instead." % (
storage.__class__.__name__,
storage.location,
correct,
)
)
9 changes: 9 additions & 0 deletions tests/test_gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import datetime
import mimetypes

from django.core.exceptions import ImproperlyConfigured
from django.core.files.base import ContentFile
from django.test import TestCase
from django.utils import timezone
Expand Down Expand Up @@ -359,3 +360,11 @@ def test_cache_control(self):
bucket = self.storage.client.get_bucket(self.bucket_name)
blob = bucket.get_blob(filename)
self.assertEqual(blob.cache_control, cache_control)

def test_location_leading_slash(self):
msg = (
"GoogleCloudStorage.location cannot begin with a leading slash. "
"Found '/'. Use '' instead."
)
with self.assertRaises(ImproperlyConfigured, msg=msg):
gcloud.GoogleCloudStorage(location='/')
9 changes: 9 additions & 0 deletions tests/test_gs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.exceptions import ImproperlyConfigured
from django.core.files.base import ContentFile
from django.test import TestCase

Expand Down Expand Up @@ -30,3 +31,11 @@ def test_gs_gzip(self):
policy=self.storage.default_acl,
rewind=True,
)

def test_location_leading_slash(self):
msg = (
"GSBotoStorage.location cannot begin with a leading slash. "
"Found '/'. Use '' instead."
)
with self.assertRaises(ImproperlyConfigured, msg=msg):
gs.GSBotoStorage(location='/')
9 changes: 9 additions & 0 deletions tests/test_s3boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from boto.exception import S3ResponseError
from boto.s3.key import Key
from boto.utils import ISO8601, parse_ts
from django.core.exceptions import ImproperlyConfigured
from django.core.files.base import ContentFile
from django.test import TestCase
from django.utils import timezone as tz
Expand Down Expand Up @@ -343,3 +344,11 @@ def upload_part_from_file(fp, part_num, *args, **kwargs):
f.close()

assert content.size == sum(file_part_size)

def test_location_leading_slash(self):
msg = (
"S3BotoStorage.location cannot begin with a leading slash. "
"Found '/'. Use '' instead."
)
with self.assertRaises(ImproperlyConfigured, msg=msg):
s3boto.S3BotoStorage(location='/')
9 changes: 9 additions & 0 deletions tests/test_s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from botocore.exceptions import ClientError
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.files.base import ContentFile
from django.test import TestCase
from django.utils.six.moves.urllib import parse as urlparse
Expand Down Expand Up @@ -509,3 +510,11 @@ def test_file_write_after_exceeding_5mb(self):
'PartNumber': 2
}
]})

def test_location_leading_slash(self):
msg = (
"S3Boto3Storage.location cannot begin with a leading slash. "
"Found '/'. Use '' instead."
)
with self.assertRaises(ImproperlyConfigured, msg=msg):
s3boto3.S3Boto3Storage(location='/')