Skip to content

Commit 44fed75

Browse files
committed
CORS Configuration
Finding: CORS is wide open for the S3 bucket Location: Lines 206-213 in wa_genai_stack.py python cors=[ s3.CorsRule( allowed_methods=[s3.HttpMethods.GET, s3.HttpMethods.PUT], allowed_origins=["*"], # ⚠️ SECURITY RISK allowed_headers=["*"], # ⚠️ SECURITY RISK ) ] 1. allowed_origins=["*"] - Allows any website to make requests to this S3 bucket 2. allowed_headers=["*"] - Permits any HTTP headers in cross-origin requests 3. Data exposure - Malicious websites can potentially access stored analysis results 4. CSRF attacks - Enables cross-site request forgery from any domain python cors=[ s3.CorsRule( allowed_methods=[s3.HttpMethods.GET, s3.HttpMethods.PUT], allowed_origins=[ f"https://{alb_dns}", # Only allow the frontend ALB "https://your-domain.com" # Add specific domains as needed ], allowed_headers=[ "Content-Type", "Authorization", "x-amz-date", "x-amz-security-token" ], max_age=3600 # Cache preflight requests for 1 hour ) ] • **SEC03-BP02**: Enforce encryption in transit • **SEC05-BP01**: Reduce attack surface • **SEC06-BP01**: Implement secure network controls Severity: HIGH - This configuration exposes the S3 bucket to potential data access from any website on the internet.
1 parent 18d5c06 commit 44fed75

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ Once complete, you'll find a new CloudFormation stack named **WA-IaC-Analyzer-{r
141141

142142
1. If you enabled authentication with a custom domain:
143143
- Create a DNS record (CNAME or Alias) pointing to the ALB domain name
144+
- **For custom domains**: Set the `CUSTOM_DOMAIN` environment variable and redeploy to update CORS configuration:
145+
```bash
146+
export CUSTOM_DOMAIN=wa-analyzer.example.com
147+
cdk deploy # or re-run your deployment method
148+
```
144149

145150
2. If you created a new Cognito user pool:
146151
- Navigate to the Amazon Cognito console

ecs_fargate_app/wa_genai_stack.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,20 +398,17 @@ def __init__(self, scope: Construct, construct_id: str, **kwarg) -> None:
398398
KB_ID = kb.knowledge_base_id
399399

400400
# Create S3 bucket and DynamoDB table for storage layer
401+
# Get the ALB DNS name for CORS configuration
402+
# This will be set after the frontend service is created
403+
cors_origins = []
404+
401405
# Create S3 bucket for storing analysis results
402406
analysis_storage_bucket = s3.Bucket(
403407
self,
404408
"AnalysisStorageBucket",
405409
removal_policy=RemovalPolicy.DESTROY,
406410
auto_delete_objects=True,
407411
enforce_ssl=True,
408-
cors=[
409-
s3.CorsRule(
410-
allowed_methods=[s3.HttpMethods.GET, s3.HttpMethods.PUT],
411-
allowed_origins=["*"],
412-
allowed_headers=["*"],
413-
)
414-
],
415412
)
416413

417414
# Create DynamoDB table for metadata
@@ -957,6 +954,35 @@ def __init__(self, scope: Construct, construct_id: str, **kwarg) -> None:
957954
# Get the ALB DNS name after frontend service is created
958955
alb_dns = frontend_service.load_balancer.load_balancer_dns_name
959956

957+
# Configure CORS for S3 bucket with specific origins
958+
cors_origins = [f"http://{alb_dns}"]
959+
if auth_config["enabled"]:
960+
cors_origins.append(f"https://{alb_dns}")
961+
962+
# Add custom domain support from environment variable
963+
custom_domain = os.environ.get("CUSTOM_DOMAIN", "")
964+
if custom_domain:
965+
cors_origins.extend([f"http://{custom_domain}", f"https://{custom_domain}"])
966+
967+
# Add CORS configuration to the S3 bucket
968+
cfn_bucket = analysis_storage_bucket.node.default_child
969+
cfn_bucket.cors_configuration = {
970+
"corsRules": [
971+
{
972+
"allowedMethods": ["GET", "PUT"],
973+
"allowedOrigins": cors_origins,
974+
"allowedHeaders": [
975+
"Content-Type",
976+
"Content-Length",
977+
"Authorization",
978+
"x-amz-date",
979+
"x-amz-security-token"
980+
],
981+
"maxAge": 3000
982+
}
983+
]
984+
}
985+
960986
# Configure health check for ALB
961987
frontend_service.target_group.configure_health_check(path="/healthz")
962988

0 commit comments

Comments
 (0)