Skip to content

Commit 00d3a59

Browse files
authored
fix CORS issue for CreateBucket and ListBuckets (#7961)
1 parent 336218f commit 00d3a59

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

localstack/aws/handlers/cors.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ def should_enforce_self_managed_service(context: RequestContext) -> bool:
118118
if context.service:
119119
service_name = context.service.service_name
120120
if not config.DISABLE_CUSTOM_CORS_S3 and service_name == "s3":
121-
# ListBuckets is not concerned by S3 CORS handling, it should follow general LocalStack CORS rules.
122-
# we can also check if the path is "/", no need for operation
123-
if context.operation and context.operation.name == "ListBuckets":
124-
return True
125121
return False
126122
if not config.DISABLE_CUSTOM_CORS_APIGATEWAY and service_name == "apigateway":
127123
is_user_request = (

localstack/services/s3/cors.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import re
23
from typing import Optional, Tuple
34

@@ -18,6 +19,9 @@
1819
from localstack.services.s3.models import BucketCorsIndex
1920
from localstack.services.s3.utils import S3_VIRTUAL_HOSTNAME_REGEX
2021

22+
# TODO: add more logging statements
23+
LOG = logging.getLogger(__name__)
24+
2125
_s3_virtual_host_regex = re.compile(S3_VIRTUAL_HOSTNAME_REGEX)
2226
FAKE_HOST_ID = "9Gjjt1m+cjU4OPvX9O9/8RuvnG41MRb/18Oux2o5H5MY7ISNTlXN+Dz9IG62/ILVxhAGI0qyPfg="
2327

@@ -249,3 +253,28 @@ def _get_op_from_request(self, request: Request):
249253
except Exception:
250254
# if we can't parse the request, just set GetObject
251255
return self._service.operation_model("GetObject")
256+
257+
258+
def s3_cors_request_handler(chain: HandlerChain, context: RequestContext, response: Response):
259+
"""
260+
Handler to add default CORS headers to S3 operations not concerned with CORS configuration
261+
"""
262+
# if DISABLE_CUSTOM_CORS_S3 is true, the default CORS handling will take place, so we won't need to do it here
263+
if config.LEGACY_S3_PROVIDER or config.DISABLE_CUSTOM_CORS_S3:
264+
return
265+
266+
if context.service.service_name != "s3":
267+
return
268+
269+
if not context.operation or context.operation.name not in ("ListBuckets", "CreateBucket"):
270+
return
271+
272+
if not config.DISABLE_CORS_CHECKS and not is_origin_allowed_default(context.request.headers):
273+
LOG.info(
274+
"Blocked CORS request from forbidden origin %s",
275+
context.request.headers.get("origin") or context.request.headers.get("referer"),
276+
)
277+
response.status_code = 403
278+
chain.terminate()
279+
280+
add_default_headers(response_headers=response.headers, request_headers=context.request.headers)

localstack/services/s3/provider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
from localstack.services.moto import call_moto
9999
from localstack.services.plugins import ServiceLifecycleHook
100100
from localstack.services.s3 import constants as s3_constants
101-
from localstack.services.s3.cors import S3CorsHandler
101+
from localstack.services.s3.cors import S3CorsHandler, s3_cors_request_handler
102102
from localstack.services.s3.models import S3Store, get_moto_s3_backend, s3_stores
103103
from localstack.services.s3.notifications import NotificationDispatcher, S3EventNotificationContext
104104
from localstack.services.s3.presigned_url import (
@@ -1409,5 +1409,6 @@ def bucket_get_permission(fn, self, *args, **kwargs):
14091409

14101410

14111411
def register_custom_handlers():
1412+
serve_custom_service_request_handlers.append(s3_cors_request_handler)
14121413
serve_custom_service_request_handlers.append(s3_presigned_url_request_handler)
14131414
modify_service_response.append(S3Provider.service, s3_presigned_url_response_handler)

0 commit comments

Comments
 (0)