Skip to content

Commit b0d007e

Browse files
committed
Address review comments
1 parent 4a21786 commit b0d007e

File tree

6 files changed

+16
-18
lines changed

6 files changed

+16
-18
lines changed

SECURITY.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ We use [pre-signed POST URLs](s3-pre-signed-url) to upload files to AWS S3.
1212
it before fetching files from S3.
1313

1414
Please note, that Django's signer uses the `SECRET_KEY`, rotating the key will void all
15-
signatures.
15+
signatures. Should you rotate the secret key, between a form GET and POST request, the
16+
form will fail. Similarly, Django will expire all sessions if you rotate the key.
1617

1718
[s3-pre-signed-url]: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-presigned-urls.html
1819
[django-signing]: https://docs.djangoproject.com/en/stable/topics/signing/

s3file/forms.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from storages.utils import safe_join
99

1010
from s3file.middleware import S3FileMiddleware
11-
from s3file.storages import storage
11+
from s3file.storages import get_aws_location, storage
1212

1313
logger = logging.getLogger("s3file")
1414

@@ -18,7 +18,7 @@ class S3FileInputMixin:
1818

1919
needs_multipart_form = False
2020
upload_path = safe_join(
21-
str(storage.aws_location),
21+
str(get_aws_location()),
2222
str(
2323
getattr(
2424
settings, "S3FILE_UPLOAD_PATH", pathlib.PurePosixPath("tmp", "s3file")

s3file/middleware.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from django.utils.crypto import constant_time_compare
77

88
from . import views
9-
from .storages import local_dev, storage
9+
from .storages import get_aws_location, local_dev, storage
1010

1111
logger = logging.getLogger("s3file")
1212

@@ -40,10 +40,7 @@ def __call__(self, request):
4040
@classmethod
4141
def get_files_from_storage(cls, paths, signature):
4242
"""Return S3 file where the name does not include the path."""
43-
try:
44-
location = storage.aws_location
45-
except AttributeError:
46-
location = storage.location
43+
location = get_aws_location()
4744
for path in paths:
4845
path = pathlib.PurePosixPath(path)
4946
if not constant_time_compare(
@@ -54,7 +51,7 @@ def get_files_from_storage(cls, paths, signature):
5451
relative_path = str(path.relative_to(location))
5552
except ValueError as e:
5653
raise SuspiciousFileOperation(
57-
f"Path is not inside the designated upload location: {path}"
54+
f"Path is outside the storage location: {path}"
5855
) from e
5956

6057
try:

s3file/storages.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@
1010

1111

1212
class S3MockStorage(FileSystemStorage):
13-
@property
14-
def aws_location(self):
15-
return getattr(settings, "AWS_LOCATION", "")
16-
1713
@property
1814
def location(self):
19-
return safe_join(os.path.abspath(self.base_location), self.aws_location)
15+
return safe_join(os.path.abspath(self.base_location), get_aws_location())
2016

2117
class connection:
2218
class meta:
@@ -56,3 +52,7 @@ class bucket:
5652
local_dev = isinstance(default_storage, FileSystemStorage)
5753

5854
storage = default_storage if not local_dev else S3MockStorage()
55+
56+
57+
def get_aws_location():
58+
return getattr(settings, "AWS_LOCATION", "")

tests/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from selenium import webdriver
88
from selenium.common.exceptions import WebDriverException
99

10-
from s3file.storages import storage
10+
from s3file.storages import get_aws_location
1111

1212

1313
@pytest.fixture(scope="session")
@@ -26,7 +26,7 @@ def driver():
2626
@pytest.fixture
2727
def freeze_upload_folder(monkeypatch):
2828
"""Freeze the upload folder which by default contains a random UUID v4."""
29-
upload_folder = Path(storage.aws_location) / "tmp" / "s3file"
29+
upload_folder = Path(get_aws_location()) / "tmp" / "s3file"
3030
monkeypatch.setattr(
3131
"s3file.forms.S3FileInputMixin.upload_folder",
3232
str(upload_folder),

tests/test_middleware.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from django.core.files.uploadedfile import SimpleUploadedFile
77

88
from s3file.middleware import S3FileMiddleware
9-
from s3file.storages import storage
9+
from s3file.storages import get_aws_location, storage
1010

1111

1212
class TestS3FileMiddleware:
@@ -16,7 +16,7 @@ def test_get_files_from_storage(self, freeze_upload_folder):
1616
"tmp/s3file/test_get_files_from_storage", ContentFile(content)
1717
)
1818
files = S3FileMiddleware.get_files_from_storage(
19-
[os.path.join(storage.aws_location, name)],
19+
[os.path.join(get_aws_location(), name)],
2020
"VRIPlI1LCjUh1EtplrgxQrG8gSAaIwT48mMRlwaCytI",
2121
)
2222
file = next(files)

0 commit comments

Comments
 (0)