From 9c2809c195df44c374b043a578f8d9e514a8c42e Mon Sep 17 00:00:00 2001 From: kqjy Date: Sun, 22 Feb 2026 18:03:38 +0800 Subject: [PATCH] Backwards compatibility for Proxy trust config --- app/__init__.py | 8 +++++++- app/config.py | 2 +- app/s3_api.py | 6 +++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index a8f673d..eb4a753 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations import html as html_module import logging import mimetypes +import os import shutil import sys import time @@ -93,8 +94,13 @@ def create_app( app.config.setdefault("WTF_CSRF_ENABLED", False) # Trust X-Forwarded-* headers from proxies - num_proxies = app.config.get("NUM_TRUSTED_PROXIES", 0) + num_proxies = app.config.get("NUM_TRUSTED_PROXIES", 1) if num_proxies: + if "NUM_TRUSTED_PROXIES" not in os.environ: + logging.getLogger(__name__).warning( + "NUM_TRUSTED_PROXIES not set, defaulting to 1. " + "Set NUM_TRUSTED_PROXIES=0 if not behind a reverse proxy." + ) app.wsgi_app = ProxyFix(app.wsgi_app, x_for=num_proxies, x_proto=num_proxies, x_host=num_proxies, x_prefix=num_proxies) # Enable gzip compression for responses (10-20x smaller JSON payloads) diff --git a/app/config.py b/app/config.py index 00023c5..9949d81 100644 --- a/app/config.py +++ b/app/config.py @@ -314,7 +314,7 @@ class AppConfig: site_region = str(_get("SITE_REGION", "us-east-1")) site_priority = int(_get("SITE_PRIORITY", 100)) ratelimit_admin = _validate_rate_limit(str(_get("RATE_LIMIT_ADMIN", "60 per minute"))) - num_trusted_proxies = int(_get("NUM_TRUSTED_PROXIES", 0)) + num_trusted_proxies = int(_get("NUM_TRUSTED_PROXIES", 1)) allowed_redirect_hosts_raw = _get("ALLOWED_REDIRECT_HOSTS", "") allowed_redirect_hosts = [h.strip() for h in str(allowed_redirect_hosts_raw).split(",") if h.strip()] allow_internal_endpoints = str(_get("ALLOW_INTERNAL_ENDPOINTS", "0")).lower() in {"1", "true", "yes", "on"} diff --git a/app/s3_api.py b/app/s3_api.py index 7a5e5da..7222ffa 100644 --- a/app/s3_api.py +++ b/app/s3_api.py @@ -2481,7 +2481,11 @@ def _post_object(bucket_name: str) -> Response: if success_action_redirect: allowed_hosts = current_app.config.get("ALLOWED_REDIRECT_HOSTS", []) if not allowed_hosts: - return _error_response("InvalidArgument", "Redirect not allowed: ALLOWED_REDIRECT_HOSTS not configured", 400) + current_app.logger.warning( + "ALLOWED_REDIRECT_HOSTS not configured, falling back to request Host header. " + "Set ALLOWED_REDIRECT_HOSTS for production deployments." + ) + allowed_hosts = [request.host] parsed = urlparse(success_action_redirect) if parsed.scheme not in ("http", "https"): return _error_response("InvalidArgument", "Redirect URL must use http or https", 400)