YTShortMakerArchx commited on
Commit
357eb85
·
verified ·
1 Parent(s): 96d375d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -3
app.py CHANGED
@@ -136,11 +136,10 @@ RATE_LIMIT_MAX_PER_HOUR = 10
136
  RATE_LIMIT_WINDOW_HOURS = 1
137
 
138
  ALLOWED_ORIGINS = [
139
- "https://shortgen-archx.pages.dev",
140
  "http://localhost:3000",
141
  "http://127.0.0.1:3000",
142
- "http://localhost:5500",
143
- "http://127.0.0.1:5500",
144
  ]
145
 
146
  TTS_SPACES = [
@@ -570,6 +569,43 @@ app.add_middleware(
570
  expose_headers=["X-Pipeline-ID", "X-Session-ID"],
571
  )
572
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
573
  # =============================================================================
574
  # HTTP CLIENT
575
  # =============================================================================
 
136
  RATE_LIMIT_WINDOW_HOURS = 1
137
 
138
  ALLOWED_ORIGINS = [
139
+ "https://shortgenx.pages.dev",
140
  "http://localhost:3000",
141
  "http://127.0.0.1:3000",
142
+ "https://ytshortmakerarchx-archnemix-controller.hf.space",
 
143
  ]
144
 
145
  TTS_SPACES = [
 
569
  expose_headers=["X-Pipeline-ID", "X-Session-ID"],
570
  )
571
 
572
+ @app.middleware("http")
573
+ async def enforce_cors_origin(request: Request, call_next):
574
+ """
575
+ Extra security: Reject requests from non-allowed origins with clear error.
576
+ This complements CORS middleware for better security.
577
+ """
578
+ origin = request.headers.get("origin")
579
+
580
+ # Allow requests with no origin (same-origin, API calls from servers)
581
+ if not origin:
582
+ response = await call_next(request)
583
+ return response
584
+
585
+ # Check if origin is in allowed list
586
+ is_allowed = origin in ALLOWED_ORIGINS
587
+
588
+ # Allow localhost development ports (any port)
589
+ if not is_allowed:
590
+ is_allowed = any(
591
+ origin.startswith(f"http://localhost:3000") or
592
+ origin.startswith(f"http://127.0.0.1:3000")
593
+ )
594
+
595
+ if not is_allowed:
596
+ logger.warning(f"[CORS SECURITY] Rejected request from unauthorized origin: {origin}")
597
+ return JSONResponse(
598
+ status_code=403,
599
+ content={
600
+ "error": "cors_origin_not_allowed",
601
+ "message": f"Origin '{origin}' is not allowed to access this service.",
602
+ "allowed_origins": ["shortgenx.pages.dev", "localhost:3000"]
603
+ }
604
+ )
605
+
606
+ response = await call_next(request)
607
+ return response
608
+
609
  # =============================================================================
610
  # HTTP CLIENT
611
  # =============================================================================