feat: add CORS support to gateway#39
Open
ashwing wants to merge 1 commit into
Open
Conversation
Wire tower-http CorsLayer into the router with a permissive default (allow any origin, method, and header). This unblocks browser-based clients like playground UIs and Swagger from calling the API directly. Closes vllm-project#38 Signed-off-by: Ashwin Giridharan <girida@amazon.com>
| pub fn build_router(state: ProxyState) -> Router { | ||
| // Permissive CORS default — allows any origin for local dev and playground UIs. | ||
| // For production, this should be tightened. Two options: | ||
| // 1. Env-driven: read CORS_ALLOWED_ORIGINS (comma-separated), fall back to Any |
Collaborator
There was a problem hiding this comment.
let's create a ServerConfig struct and add there, yeah? we can pass it to the build_router function.
something like:
impl ServerConfig {
/// Read `CORS_ALLOWED_ORIGINS` (comma-separated). Unset or empty = permissive.
pub fn from_env() -> Self {
let cors_allowed_origins = std::env::var("CORS_ALLOWED_ORIGINS")
.ok()
.map(|s| {
s.split(',')
.map(str::trim)
.filter(|o| !o.is_empty())
.map(str::to_owned)
.collect::<Vec<_>>()
})
.unwrap_or_default();
Self { cors_allowed_origins }
}
}would be good to update the docs with this as setting here would default to permissive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Wire
tower-httpCorsLayerinto the router with a permissive default (allow any origin, method, and header). This unblocks browser-based clients — playground UIs, Swagger, JS apps — from calling the API directly.The current default is intentionally permissive (
Any) for local development. The code includes a comment proposing two options for production tightening:CORS_ALLOWED_ORIGINS(comma-separated), fallback toAny--cors-allow-origin <value>alongside existing gateway argsHappy to implement either in a follow-up based on maintainer preference.
Closes #38
Test Plan
Automated (2 integration tests):
test_cors_preflight_returns_200— OPTIONS with Origin + Access-Control-Request-Method → 200 with all CORS headerstest_cors_headers_on_regular_request— POST with Origin → response includesaccess-control-allow-originLint/format:
cargo clippy --workspace --all-targets -- -D warnings— cleancargo fmt -- --check— cleanDiscovery: Found via live stress testing against deployed gateway on EC2 (see #38 for reproduction).