Skip to content

Commit

Permalink
S3: Add MOTO_DISABLE_GLOBAL_CORS for disabling wildcard CORS (#8409)
Browse files Browse the repository at this point in the history
  • Loading branch information
kangasta authored Jan 10, 2025
1 parent db72e95 commit 92b2c5c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/docs/configuration/environment_variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ The following is a non-exhaustive list of the environment variables that can be
+-------------------------------+----------+-----------+-------------------------------------------------------------------------------------------------+
| MOTO_PRETTIFY_RESPONSES | bool | False | Prettify responses from Moto, making it easier to read and debug. |
+-------------------------------+----------+-----------+-------------------------------------------------------------------------------------------------+
| MOTO_DISABLE_GLOBAL_CORS | bool | False | Disable configuring global wildcard CORS. |
+-------------------------------+----------+-----------+-------------------------------------------------------------------------------------------------+

5 changes: 5 additions & 0 deletions docs/docs/services/s3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ s3
- [X] put_bucket_acl
- [ ] put_bucket_analytics_configuration
- [X] put_bucket_cors

Note that the moto server configures global wildcard CORS settings by default. To avoid this from overriding empty bucket CORS, disable global CORS with an environment variable:

MOTO_DISABLE_GLOBAL_CORS=true

- [X] put_bucket_encryption
- [ ] put_bucket_intelligent_tiering_configuration
- [ ] put_bucket_inventory_configuration
Expand Down
5 changes: 4 additions & 1 deletion moto/moto_server/werkzeug_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from moto.core import DEFAULT_ACCOUNT_ID
from moto.core.base_backend import BackendDict
from moto.core.utils import convert_to_flask_response
from moto.settings import DISABLE_GLOBAL_CORS

from .utilities import AWSTestHelper, RegexConverter

Expand Down Expand Up @@ -274,7 +275,9 @@ def create_backend_app(service: backends.SERVICE_NAMES) -> Flask:
backend_app = Flask("moto", template_folder=template_dir)
backend_app.debug = True
backend_app.service = service # type: ignore[attr-defined]
CORS(backend_app)

if not DISABLE_GLOBAL_CORS:
CORS(backend_app)

# Reset view functions to reset the app
backend_app.view_functions = {}
Expand Down
2 changes: 2 additions & 0 deletions moto/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def is_test_proxy_mode() -> bool:

PRETTIFY_RESPONSES = bool(os.environ.get("MOTO_PRETTIFY_RESPONSES", False))

DISABLE_GLOBAL_CORS = bool(os.environ.get("MOTO_DISABLE_GLOBAL_CORS", False))

# Fully skip test that require docker
SKIP_REQUIRES_DOCKER = bool(os.environ.get("TESTS_SKIP_REQUIRES_DOCKER", False))

Expand Down
19 changes: 19 additions & 0 deletions tests/test_s3/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,25 @@ def test_s3_server_post_cors():
assert res.headers["Access-Control-Allow-Headers"] == "origin, x-requested-with"


def test_s3_no_default_cors():
"""Test default CORS headers are not set if MOTO_DISABLE_GLOBAL_CORS is true"""
for disable_cors in [True, False]:
with patch("moto.moto_server.werkzeug_app.DISABLE_GLOBAL_CORS", disable_cors):
test_client = authenticated_client()

# Create a bucket and a file
test_client.put("/", "http://nodefaultcors.localhost:5000/")
test_client.put("/test", "http://nodefaultcors.localhost:5000/")
test_client.put("/", "http://tester.localhost:5000/")

resp = test_client.get(
"/test",
"http://nodefaultcors.localhost:5000/",
headers={"Origin": "example.com"},
)
assert ("Access-Control-Allow-Origin" not in resp.headers) == disable_cors


def test_s3_server_post_cors_exposed_header():
"""Test overriding default CORS headers with custom bucket rules"""
# github.com/getmoto/moto/issues/4220
Expand Down

0 comments on commit 92b2c5c

Please sign in to comment.