mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-12-18 09:19:47 +00:00
Renamed and addressed PR comments
This commit is contained in:
parent
815b5c7279
commit
17f488036f
2 changed files with 31 additions and 43 deletions
|
|
@ -318,9 +318,7 @@ class QuotaConfig(BaseModel):
|
|||
period: QuotaPeriod = Field(default=QuotaPeriod.DAY, description="Quota period to set")
|
||||
|
||||
|
||||
class CORSSpec(BaseModel):
|
||||
"""CORS configuration with strict defaults (minimal permissions)."""
|
||||
|
||||
class CORSConfig(BaseModel):
|
||||
allow_origins: list[str] = Field(default_factory=list)
|
||||
allow_origin_regex: str | None = Field(default=None)
|
||||
allow_methods: list[str] = Field(default=["OPTIONS"])
|
||||
|
|
@ -330,39 +328,29 @@ class CORSSpec(BaseModel):
|
|||
max_age: int = Field(default=600, ge=0)
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _validate_credentials_with_wildcard(self) -> Self:
|
||||
def validate_credentials_config(self) -> Self:
|
||||
if self.allow_credentials and (self.allow_origins == ["*"] or "*" in self.allow_origins):
|
||||
raise ValueError("CORS: allow_credentials=True requires explicit origins")
|
||||
raise ValueError("Cannot use wildcard origins with credentials enabled")
|
||||
return self
|
||||
|
||||
|
||||
# Union type for flexible CORS configuration input
|
||||
# Accepts: bool (dev shortcuts) or CORSSpec (explicit config)
|
||||
CORSConfig = bool | CORSSpec
|
||||
|
||||
|
||||
def process_cors_config(cors_config: CORSConfig) -> CORSSpec | None:
|
||||
"""Process CORS config: bool -> dev defaults, CORSSpec -> passthrough."""
|
||||
if cors_config is False:
|
||||
def process_cors_config(cors_config: bool | CORSConfig | None) -> CORSConfig | None:
|
||||
if cors_config is False or cors_config is None:
|
||||
return None
|
||||
|
||||
if cors_config is True:
|
||||
# Dev mode: localhost with any port
|
||||
return CORSSpec(
|
||||
# dev mode: allow localhost on any port
|
||||
return CORSConfig(
|
||||
allow_origins=[],
|
||||
allow_origin_regex=r"https?://localhost:\d+",
|
||||
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||||
allow_headers=["Content-Type", "Authorization", "X-Requested-With"],
|
||||
allow_credentials=False,
|
||||
expose_headers=[],
|
||||
max_age=600,
|
||||
)
|
||||
|
||||
elif isinstance(cors_config, CORSSpec):
|
||||
if isinstance(cors_config, CORSConfig):
|
||||
return cors_config
|
||||
|
||||
else:
|
||||
raise ValueError(f"Invalid CORS configuration type: {type(cors_config)}")
|
||||
raise ValueError(f"Expected bool or CORSConfig, got {type(cors_config).__name__}")
|
||||
|
||||
|
||||
class ServerConfig(BaseModel):
|
||||
|
|
@ -396,7 +384,7 @@ class ServerConfig(BaseModel):
|
|||
default=None,
|
||||
description="Per client quota request configuration",
|
||||
)
|
||||
cors: CORSConfig | None = Field(
|
||||
cors: bool | CORSConfig | None = Field(
|
||||
default=None,
|
||||
description="CORS configuration for cross-origin requests. Can be:\n"
|
||||
"- true: Enable localhost CORS for development\n"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue