Diiegoal commited on
Commit
0248050
·
1 Parent(s): afbef75

Actualizo la app

Browse files
.env.example CHANGED
@@ -17,7 +17,7 @@ ORACULO_JWT_SECRET_KEY=replace-this-with-a-long-random-secret-at-least-32-chars
17
  ORACULO_JWT_ALGORITHM=HS256
18
  ORACULO_ACCESS_TOKEN_EXPIRE_MINUTES=60
19
 
20
- ORACULO_ALLOWED_HOSTS=localhost,127.0.0.1,*.hf.space,*.huggingface.co
21
  ORACULO_CORS_ALLOW_ORIGINS=http://localhost:3000,http://127.0.0.1:3000
22
 
23
  ORACULO_MAX_REQUEST_SIZE_BYTES=32768
 
17
  ORACULO_JWT_ALGORITHM=HS256
18
  ORACULO_ACCESS_TOKEN_EXPIRE_MINUTES=60
19
 
20
+ ORACULO_ALLOWED_HOSTS=localhost,127.0.0.1,diiegoal-oraculo-api.hf.space,*.hf.space,*.huggingface.co
21
  ORACULO_CORS_ALLOW_ORIGINS=http://localhost:3000,http://127.0.0.1:3000
22
 
23
  ORACULO_MAX_REQUEST_SIZE_BYTES=32768
app/core/config.py CHANGED
@@ -4,6 +4,7 @@ import json
4
  from functools import lru_cache
5
  from pathlib import Path
6
  from typing import Annotated, Literal
 
7
 
8
  from pydantic import Field, field_validator
9
  from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict
@@ -88,6 +89,54 @@ class Settings(BaseSettings):
88
  return [str(item).strip() for item in parsed_value if str(item).strip()]
89
  return [item.strip() for item in value.split(",") if item.strip()]
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  @property
92
  def base_dir(self) -> Path:
93
  return Path(__file__).resolve().parents[2]
 
4
  from functools import lru_cache
5
  from pathlib import Path
6
  from typing import Annotated, Literal
7
+ from urllib.parse import urlsplit
8
 
9
  from pydantic import Field, field_validator
10
  from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict
 
89
  return [str(item).strip() for item in parsed_value if str(item).strip()]
90
  return [item.strip() for item in value.split(",") if item.strip()]
91
 
92
+ @field_validator("allowed_hosts", mode="after")
93
+ @classmethod
94
+ def _normalize_allowed_hosts(cls, value: list[str]) -> list[str]:
95
+ normalized_hosts: list[str] = []
96
+ for raw_host in value:
97
+ normalized_host = cls._normalize_allowed_host(raw_host)
98
+ if normalized_host and normalized_host not in normalized_hosts:
99
+ normalized_hosts.append(normalized_host)
100
+ return normalized_hosts
101
+
102
+ @field_validator("cors_allow_origins", mode="after")
103
+ @classmethod
104
+ def _normalize_cors_allow_origins(cls, value: list[str]) -> list[str]:
105
+ normalized_origins: list[str] = []
106
+ for raw_origin in value:
107
+ normalized_origin = cls._normalize_origin(raw_origin)
108
+ if normalized_origin and normalized_origin not in normalized_origins:
109
+ normalized_origins.append(normalized_origin)
110
+ return normalized_origins
111
+
112
+ @staticmethod
113
+ def _normalize_allowed_host(value: str) -> str:
114
+ normalized_value = value.strip()
115
+ if not normalized_value or normalized_value == "*":
116
+ return normalized_value
117
+
118
+ candidate = normalized_value if "://" in normalized_value else f"//{normalized_value}"
119
+ parsed = urlsplit(candidate)
120
+ hostname = parsed.hostname or parsed.netloc
121
+ if hostname:
122
+ return hostname.strip().lower()
123
+
124
+ host_candidate = normalized_value.split("/", 1)[0].strip()
125
+ if "@" in host_candidate:
126
+ host_candidate = host_candidate.rsplit("@", 1)[-1]
127
+ return host_candidate.lower()
128
+
129
+ @staticmethod
130
+ def _normalize_origin(value: str) -> str:
131
+ normalized_value = value.strip().rstrip("/")
132
+ if not normalized_value or normalized_value == "*" or "://" not in normalized_value:
133
+ return normalized_value
134
+
135
+ parsed = urlsplit(normalized_value)
136
+ if not parsed.scheme or not parsed.netloc:
137
+ return normalized_value
138
+ return f"{parsed.scheme.lower()}://{parsed.netloc.lower()}"
139
+
140
  @property
141
  def base_dir(self) -> Path:
142
  return Path(__file__).resolve().parents[2]
tests/api/test_security_middleware.py CHANGED
@@ -69,3 +69,22 @@ def test_trusted_host_middleware_rejects_invalid_hosts(client: TestClient) -> No
69
  response = client.get("/api/v1/health/live", headers={"Host": "evil.example.com"})
70
 
71
  assert response.status_code == 400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  response = client.get("/api/v1/health/live", headers={"Host": "evil.example.com"})
70
 
71
  assert response.status_code == 400
72
+
73
+
74
+ def test_allowed_hosts_configuration_normalizes_full_urls(tmp_path) -> None:
75
+ settings = build_test_settings(
76
+ f"sqlite:///{tmp_path / 'trusted_hosts.db'}",
77
+ allowed_hosts=[
78
+ "localhost/docs",
79
+ "127.0.0.1:8000/docs",
80
+ "https://demo-space.hf.space/docs",
81
+ ],
82
+ )
83
+ app = create_app(settings=settings, model_manager=FakeModelManager())
84
+
85
+ with TestClient(app) as client:
86
+ localhost_response = client.get("/api/v1/health/live", headers={"Host": "localhost:8000"})
87
+ space_response = client.get("/api/v1/health/live", headers={"Host": "demo-space.hf.space"})
88
+
89
+ assert localhost_response.status_code == 200
90
+ assert space_response.status_code == 200