feat(security): harden gateway boundaries and usage policies

Consolidate subscription usage policy enforcement, privacy-safe persistence, and gateway security hardening into one reviewable change.

Includes bounded HTTP and execution envelopes, header and protocol guards, DNS and relay validation, authentication and secret projection hardening, secure backup/install paths, and regression coverage.
This commit is contained in:
elky
2026-09-04 03:45:52 +08:00
parent ddcbeb3ae9
commit 579f2c7cc1
1019 changed files with 190437 additions and 26080 deletions
@@ -5,19 +5,21 @@ pub(super) fn classify_internal_route(
method: &http::Method,
normalized_path: &str,
) -> Option<ClassifiedRoute> {
if method == http::Method::POST && normalized_path.starts_with("/api/internal/gateway/") {
let route_kind = match normalized_path {
"/api/internal/gateway/resolve" => "resolve",
"/api/internal/gateway/auth-context" => "auth_context",
"/api/internal/gateway/decision-sync" => "decision_sync",
"/api/internal/gateway/decision-stream" => "decision_stream",
"/api/internal/gateway/plan-sync" => "plan_sync",
"/api/internal/gateway/plan-stream" => "plan_stream",
"/api/internal/gateway/report-sync" => "report_sync",
"/api/internal/gateway/report-stream" => "report_stream",
"/api/internal/gateway/finalize-sync" => "finalize_sync",
"/api/internal/gateway/execute-sync" => "execute_sync",
"/api/internal/gateway/execute-stream" => "execute_stream",
if normalized_path == "/api/internal/gateway"
|| normalized_path.starts_with("/api/internal/gateway/")
{
let route_kind = match (method, normalized_path) {
(&http::Method::POST, "/api/internal/gateway/resolve") => "resolve",
(&http::Method::POST, "/api/internal/gateway/auth-context") => "auth_context",
(&http::Method::POST, "/api/internal/gateway/decision-sync") => "decision_sync",
(&http::Method::POST, "/api/internal/gateway/decision-stream") => "decision_stream",
(&http::Method::POST, "/api/internal/gateway/plan-sync") => "plan_sync",
(&http::Method::POST, "/api/internal/gateway/plan-stream") => "plan_stream",
(&http::Method::POST, "/api/internal/gateway/report-sync") => "report_sync",
(&http::Method::POST, "/api/internal/gateway/report-stream") => "report_stream",
(&http::Method::POST, "/api/internal/gateway/finalize-sync") => "finalize_sync",
(&http::Method::POST, "/api/internal/gateway/execute-sync") => "execute_sync",
(&http::Method::POST, "/api/internal/gateway/execute-stream") => "execute_stream",
_ => "unhandled",
};
Some(classified(
+22 -2
View File
@@ -11,7 +11,7 @@ mod oauth;
mod public_support;
use super::auth::{
resolve_control_decision_auth, resolve_gateway_credential_carrier,
resolve_control_decision_auth_with_trusted_auth, resolve_gateway_credential_carrier,
ControlDecisionAuthResolution, GatewayCredentialCarrier,
};
use super::{GatewayAdminPrincipalContext, GatewayControlAuthContext, GatewayLocalAuthRejection};
@@ -175,6 +175,17 @@ pub(crate) async fn resolve_control_route(
uri: &Uri,
headers: &http::HeaderMap,
trace_id: &str,
) -> Result<Option<GatewayControlDecision>, GatewayError> {
resolve_control_route_with_trusted_auth(state, method, uri, headers, trace_id, cfg!(test)).await
}
pub(crate) async fn resolve_control_route_with_trusted_auth(
state: &AppState,
method: &http::Method,
uri: &Uri,
headers: &http::HeaderMap,
trace_id: &str,
trusted_auth_verified: bool,
) -> Result<Option<GatewayControlDecision>, GatewayError> {
let Some(mut decision) = classify_control_route(method, uri, headers) else {
return Ok(None);
@@ -185,7 +196,16 @@ pub(crate) async fn resolve_control_route(
crate::system_features::ModelDirectivePolicySnapshot::load(state).await;
}
match resolve_control_decision_auth(state, headers, uri, trace_id, decision).await? {
match resolve_control_decision_auth_with_trusted_auth(
state,
headers,
uri,
trace_id,
decision,
trusted_auth_verified,
)
.await?
{
ControlDecisionAuthResolution::Resolved(decision) => Ok(Some(decision)),
}
}
@@ -285,7 +285,7 @@ pub(super) fn classify_oauth_route(
"admin_proxy",
"provider_oauth_manage",
"batch_import_oauth",
"admin:pool",
"admin:provider_oauth",
false,
))
} else if method == http::Method::POST
@@ -296,7 +296,7 @@ pub(super) fn classify_oauth_route(
"admin_proxy",
"provider_oauth_manage",
"start_batch_import_oauth_task",
"admin:pool",
"admin:provider_oauth",
false,
))
} else if method == http::Method::GET
@@ -307,7 +307,7 @@ pub(super) fn classify_oauth_route(
"admin_proxy",
"provider_oauth_manage",
"get_batch_import_task_status",
"admin:pool",
"admin:provider_oauth",
false,
))
} else if method == http::Method::POST
@@ -197,18 +197,22 @@ pub(super) fn classify_public_support_route(
"public:auth",
false,
))
} else if matches!(method, &http::Method::GET | &http::Method::POST)
// Authentication state-changing endpoints must never be dispatched for GET.
// Besides violating HTTP method semantics, accepting GET here would allow
// browser prefetches/cross-site requests to trigger login, refresh, logout,
// registration, or verification side effects. `/me` is the sole read route.
} else if (method == http::Method::POST
&& matches!(
normalized_path,
"/api/auth/login"
| "/api/auth/refresh"
| "/api/auth/register"
| "/api/auth/me"
| "/api/auth/logout"
| "/api/auth/send-verification-code"
| "/api/auth/verify-email"
| "/api/auth/verification-status"
)
))
|| (method == http::Method::GET && normalized_path == "/api/auth/me")
{
let route_kind = match normalized_path {
"/api/auth/login" => "login",