Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Jun 6, 2022
1 parent 4a21786 commit b0d007e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 18 deletions.
3 changes: 2 additions & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ We use [pre-signed POST URLs](s3-pre-signed-url) to upload files to AWS S3.
it before fetching files from S3.

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

[s3-pre-signed-url]: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-presigned-urls.html
[django-signing]: https://docs.djangoproject.com/en/stable/topics/signing/
Expand Down
4 changes: 2 additions & 2 deletions s3file/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from storages.utils import safe_join

from s3file.middleware import S3FileMiddleware
from s3file.storages import storage
from s3file.storages import get_aws_location, storage

logger = logging.getLogger("s3file")

Expand All @@ -18,7 +18,7 @@ class S3FileInputMixin:

needs_multipart_form = False
upload_path = safe_join(
str(storage.aws_location),
str(get_aws_location()),
str(
getattr(
settings, "S3FILE_UPLOAD_PATH", pathlib.PurePosixPath("tmp", "s3file")
Expand Down
9 changes: 3 additions & 6 deletions s3file/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.utils.crypto import constant_time_compare

from . import views
from .storages import local_dev, storage
from .storages import get_aws_location, local_dev, storage

logger = logging.getLogger("s3file")

Expand Down Expand Up @@ -40,10 +40,7 @@ def __call__(self, request):
@classmethod
def get_files_from_storage(cls, paths, signature):
"""Return S3 file where the name does not include the path."""
try:
location = storage.aws_location
except AttributeError:
location = storage.location
location = get_aws_location()
for path in paths:
path = pathlib.PurePosixPath(path)
if not constant_time_compare(
Expand All @@ -54,7 +51,7 @@ def get_files_from_storage(cls, paths, signature):
relative_path = str(path.relative_to(location))
except ValueError as e:
raise SuspiciousFileOperation(
f"Path is not inside the designated upload location: {path}"
f"Path is outside the storage location: {path}"
) from e

try:
Expand Down
10 changes: 5 additions & 5 deletions s3file/storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@


class S3MockStorage(FileSystemStorage):
@property
def aws_location(self):
return getattr(settings, "AWS_LOCATION", "")

@property
def location(self):
return safe_join(os.path.abspath(self.base_location), self.aws_location)
return safe_join(os.path.abspath(self.base_location), get_aws_location())

class connection:
class meta:
Expand Down Expand Up @@ -56,3 +52,7 @@ class bucket:
local_dev = isinstance(default_storage, FileSystemStorage)

storage = default_storage if not local_dev else S3MockStorage()


def get_aws_location():
return getattr(settings, "AWS_LOCATION", "")
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from selenium import webdriver
from selenium.common.exceptions import WebDriverException

from s3file.storages import storage
from s3file.storages import get_aws_location


@pytest.fixture(scope="session")
Expand All @@ -26,7 +26,7 @@ def driver():
@pytest.fixture
def freeze_upload_folder(monkeypatch):
"""Freeze the upload folder which by default contains a random UUID v4."""
upload_folder = Path(storage.aws_location) / "tmp" / "s3file"
upload_folder = Path(get_aws_location()) / "tmp" / "s3file"
monkeypatch.setattr(
"s3file.forms.S3FileInputMixin.upload_folder",
str(upload_folder),
Expand Down
4 changes: 2 additions & 2 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.core.files.uploadedfile import SimpleUploadedFile

from s3file.middleware import S3FileMiddleware
from s3file.storages import storage
from s3file.storages import get_aws_location, storage


class TestS3FileMiddleware:
Expand All @@ -16,7 +16,7 @@ def test_get_files_from_storage(self, freeze_upload_folder):
"tmp/s3file/test_get_files_from_storage", ContentFile(content)
)
files = S3FileMiddleware.get_files_from_storage(
[os.path.join(storage.aws_location, name)],
[os.path.join(get_aws_location(), name)],
"VRIPlI1LCjUh1EtplrgxQrG8gSAaIwT48mMRlwaCytI",
)
file = next(files)
Expand Down

0 comments on commit b0d007e

Please sign in to comment.