Skip to content

Commit 44b5076

Browse files
committed
LCORE-973: CORS configuration
1 parent b26bc6a commit 44b5076

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

src/models/config.py

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,16 @@ class ConfigurationBase(BaseModel):
3838
class TLSConfiguration(ConfigurationBase):
3939
"""TLS configuration.
4040
41+
Transport Layer Security (TLS) is a cryptographic protocol designed to
42+
provide communications security over a computer network, such as the
43+
Internet. The protocol is widely used in applications such as email,
44+
instant messaging, and voice over IP, but its use in securing HTTPS remains
45+
the most publicly visible.
46+
4147
See also:
42-
- https://fastapi.tiangolo.com/deployment/https/
43-
- https://en.wikipedia.org/wiki/Transport_Layer_Security
48+
- [FastAPI HTTPS Deployment](https://fastapi.tiangolo.com/deployment/https/)
49+
- [Transport Layer Security Overview](https://en.wikipedia.org/wiki/Transport_Layer_Security)
50+
- [What is TLS](https://www.ssltrust.eu/learning/ssl/transport-layer-security-tls)
4451
"""
4552

4653
tls_certificate_path: Optional[FilePath] = Field(
@@ -68,14 +75,49 @@ def check_tls_configuration(self) -> Self:
6875

6976

7077
class CORSConfiguration(ConfigurationBase):
71-
"""CORS configuration."""
78+
"""CORS configuration.
79+
80+
CORS or 'Cross-Origin Resource Sharing' refers to the situations when a
81+
frontend running in a browser has JavaScript code that communicates with a
82+
backend, and the backend is in a different 'origin' than the frontend.
83+
84+
See also:
85+
- [CORS in FastAPI](https://fastapi.tiangolo.com/tutorial/cors/)
86+
- [Wikipedia article](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
87+
- [What is CORS?](https://dev.to/akshay_chauhan/what-is-cors-explained-8f1)
88+
"""
89+
90+
# not AnyHttpUrl: we need to support "*" that is not valid URL
91+
allow_origins: list[str] = Field(
92+
["*"],
93+
title="Allow origins",
94+
description="An origin is the combination of protocol (http, https), "
95+
"domain (myapp.com, localhost, localhost.tiangolo.com), and port (80, "
96+
"443, 8080).",
97+
)
7298

73-
allow_origins: list[str] = [
74-
"*"
75-
] # not AnyHttpUrl: we need to support "*" that is not valid URL
76-
allow_credentials: bool = False
77-
allow_methods: list[str] = ["*"]
78-
allow_headers: list[str] = ["*"]
99+
allow_credentials: bool = Field(
100+
False,
101+
title="Allow credentials",
102+
description="Indicate that cookies should be supported for cross-origin requests",
103+
)
104+
105+
allow_methods: list[str] = Field(
106+
["*"],
107+
title="Allow methods",
108+
description="A list of HTTP methods that should be allowed for "
109+
"cross-origin requests. You can use ['*'] to allow "
110+
"all standard methods.",
111+
)
112+
113+
allow_headers: list[str] = Field(
114+
["*"],
115+
title="Allow headers",
116+
description="A list of HTTP request headers that should be supported "
117+
"for cross-origin requests. You can use ['*'] to allow all headers. The "
118+
"Accept, Accept-Language, Content-Language and Content-Type headers are "
119+
"always allowed for simple CORS requests.",
120+
)
79121

80122
@model_validator(mode="after")
81123
def check_cors_configuration(self) -> Self:
@@ -183,7 +225,14 @@ class ServiceConfiguration(ConfigurationBase):
183225
tls_certificate_path=None, tls_key_path=None, tls_key_password=None
184226
)
185227
)
186-
cors: CORSConfiguration = Field(default_factory=CORSConfiguration)
228+
cors: CORSConfiguration = Field(
229+
default_factory=lambda: CORSConfiguration(
230+
allow_origins=["*"],
231+
allow_credentials=False,
232+
allow_methods=["*"],
233+
allow_headers=["*"],
234+
)
235+
)
187236

188237
@model_validator(mode="after")
189238
def check_service_configuration(self) -> Self:

0 commit comments

Comments
 (0)