Share error_response and default host allowlist; drop AppResult
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
This commit is contained in:
1 parent
66eb91bf01
commit
d022ebeae5
5 files changed
+38
-60
No files matched your search
+8
-17
@@ -1,4 +1,4 @@
|
||||
//! Router assembly and the serve loop (P3). `build_router` is used by both
|
||||
//! Router assembly and the serve loop. `build_router` is used by both
|
||||
//! `slinuxm serve` and the integration tests.
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
@@ -12,10 +12,10 @@ use std::sync::Arc;
|
||||
use axum::extract::{Request, State};
|
||||
use axum::http::{header, StatusCode};
|
||||
use axum::middleware::Next;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::Json;
|
||||
use axum::response::Response;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::error::error_response;
|
||||
use crate::state::AppState;
|
||||
use crate::{api, auth, mcp};
|
||||
|
||||
@@ -57,13 +57,7 @@ pub async fn serve_listener(
|
||||
}
|
||||
|
||||
fn allowed_hosts(c: &Config) -> Arc<Vec<String>> {
|
||||
let mut v: Vec<String> = ["localhost", "127.0.0.1", "::1"]
|
||||
.into_iter()
|
||||
.map(String::from)
|
||||
.collect();
|
||||
if !c.server.bind.is_unspecified() {
|
||||
v.push(c.server.bind.to_string());
|
||||
}
|
||||
let mut v = c.server.default_hosts();
|
||||
v.extend(
|
||||
c.server
|
||||
.allowed_hosts
|
||||
@@ -94,14 +88,11 @@ async fn host_guard(State(allowed): State<Arc<Vec<String>>>, req: Request, next:
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.map(host_only);
|
||||
match host {
|
||||
Some(h) if !allowed.iter().any(|a| a.eq_ignore_ascii_case(h)) => (
|
||||
Some(h) if !allowed.iter().any(|a| a.eq_ignore_ascii_case(h)) => error_response(
|
||||
StatusCode::FORBIDDEN,
|
||||
Json(serde_json::json!({"error": {
|
||||
"code": "forbidden_host",
|
||||
"message": "Host header not allowed; add it to server.allowed_hosts",
|
||||
}})),
|
||||
)
|
||||
.into_response(),
|
||||
"forbidden_host",
|
||||
"Host header not allowed; add it to server.allowed_hosts",
|
||||
),
|
||||
_ => next.run(req).await,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user