Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions common/config_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ class NetworkConfig(BaseConfigModel):
"Turn on this option if you are ONLY connecting from localhost."
),
)
allowed_origins: Optional[List[str]] = Field(
["*"],
description=(
'Origins allowed to call the API from a browser (default: ["*"]).\n'
"This is a CORS allowlist, not an auth mechanism: it only governs which\n"
"web pages a browser will let read this API's responses.\n"
'The default "*" means any site open in your browser can send requests to\n'
"this instance, which matters most when disable_auth is on. Restrict this to\n"
'your own frontends (e.g. ["http://localhost:8000"]) to close that off, or\n'
"use an empty list [] to block all browser (cross-origin) callers."
),
)
disable_fetch_requests: Optional[bool] = Field(
False,
description=(
Expand Down
7 changes: 7 additions & 0 deletions config_sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ network:
# Turn on this option if you are ONLY connecting from localhost.
disable_auth: false

# Origins allowed to call the API from a browser (default: ["*"]).
# This is a CORS allowlist, not authentication: "*" lets any site open in your
# browser send requests to this instance, which matters most when disable_auth
# is on. Restrict to your own frontends (e.g. ["http://localhost:8000"]) or use
# an empty list [] to block all browser (cross-origin) callers.
allowed_origins: ["*"]

# Disable fetching external content in response to requests,such as images from URLs.
disable_fetch_requests: false

Expand Down
9 changes: 6 additions & 3 deletions endpoints/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ def setup_app(host: Optional[str] = None, port: Optional[int] = None):
)
app.add_exception_handler(ContextLengthHTTPException, context_length_exception_handler)

# ALlow CORS requests
# Allow CORS requests from the configured origins.
# allow_credentials stays False: TabbyAPI authenticates with a header/query
# token rather than cookies, so credentialed CORS buys nothing and would make
# Starlette reflect an arbitrary requesting origin back instead of sending "*".
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_origins=config.network.allowed_origins,
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
Expand Down
Loading