mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-10 11:19:50 +08:00
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:
@@ -1,4 +1,5 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt;
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
@@ -16,7 +17,7 @@ use crate::snapshot::GatewayProviderTransportSnapshot;
|
||||
use crate::url::{build_openai_chat_url, build_openai_responses_url};
|
||||
use crate::vertex::uses_vertex_api_key_query_auth;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct StandardProviderRequestHeadersInput<'a> {
|
||||
pub transport: &'a GatewayProviderTransportSnapshot,
|
||||
pub provider_api_format: &'a str,
|
||||
@@ -31,13 +32,63 @@ pub struct StandardProviderRequestHeadersInput<'a> {
|
||||
pub upstream_is_stream: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
impl fmt::Debug for StandardProviderRequestHeadersInput<'_> {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter
|
||||
.debug_struct("StandardProviderRequestHeadersInput")
|
||||
.field("transport", &self.transport)
|
||||
.field("provider_api_format", &self.provider_api_format)
|
||||
.field("same_format", &self.same_format)
|
||||
.field(
|
||||
"request_header_names",
|
||||
&self
|
||||
.headers
|
||||
.keys()
|
||||
.map(|name| name.as_str())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.field("auth_header", &self.auth_header)
|
||||
.field("has_auth_value", &(!self.auth_value.is_empty()))
|
||||
.field(
|
||||
"extra_header_names",
|
||||
&self.extra_headers.keys().collect::<Vec<_>>(),
|
||||
)
|
||||
.field("has_header_rules", &self.header_rules.is_some())
|
||||
.field(
|
||||
"provider_request_body_bytes",
|
||||
&serde_json::to_vec(self.provider_request_body)
|
||||
.ok()
|
||||
.map(|bytes| bytes.len()),
|
||||
)
|
||||
.field(
|
||||
"original_request_body_bytes",
|
||||
&serde_json::to_vec(self.original_request_body)
|
||||
.ok()
|
||||
.map(|bytes| bytes.len()),
|
||||
)
|
||||
.field("upstream_is_stream", &self.upstream_is_stream)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct StandardProviderRequestHeaders {
|
||||
pub headers: BTreeMap<String, String>,
|
||||
pub auth_header: String,
|
||||
pub auth_value: String,
|
||||
}
|
||||
|
||||
impl fmt::Debug for StandardProviderRequestHeaders {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter
|
||||
.debug_struct("StandardProviderRequestHeaders")
|
||||
.field("header_names", &self.headers.keys().collect::<Vec<_>>())
|
||||
.field("auth_header", &self.auth_header)
|
||||
.field("has_auth_value", &(!self.auth_value.is_empty()))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum StandardPlanFallbackAcceptPolicy {
|
||||
None,
|
||||
@@ -47,7 +98,7 @@ pub enum StandardPlanFallbackAcceptPolicy {
|
||||
ProviderEventStreamIfMissing,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone)]
|
||||
pub struct StandardPlanFallbackHeadersInput<'a> {
|
||||
pub request_headers: &'a http::HeaderMap,
|
||||
pub existing_provider_request_headers: BTreeMap<String, String>,
|
||||
@@ -62,6 +113,44 @@ pub struct StandardPlanFallbackHeadersInput<'a> {
|
||||
pub accept_policy: StandardPlanFallbackAcceptPolicy,
|
||||
}
|
||||
|
||||
impl fmt::Debug for StandardPlanFallbackHeadersInput<'_> {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter
|
||||
.debug_struct("StandardPlanFallbackHeadersInput")
|
||||
.field(
|
||||
"request_header_names",
|
||||
&self
|
||||
.request_headers
|
||||
.keys()
|
||||
.map(|name| name.as_str())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.field(
|
||||
"existing_provider_header_names",
|
||||
&self
|
||||
.existing_provider_request_headers
|
||||
.keys()
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.field("auth_header", &self.auth_header)
|
||||
.field("has_auth_value", &self.auth_value.is_some())
|
||||
.field(
|
||||
"extra_header_names",
|
||||
&self.extra_headers.keys().collect::<Vec<_>>(),
|
||||
)
|
||||
.field("content_type", &self.content_type)
|
||||
.field("provider_api_format", &self.provider_api_format)
|
||||
.field("client_api_format", &self.client_api_format)
|
||||
.field("upstream_is_stream", &self.upstream_is_stream)
|
||||
.field(
|
||||
"build_from_request_when_empty",
|
||||
&self.build_from_request_when_empty,
|
||||
)
|
||||
.field("accept_policy", &self.accept_policy)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_standard_plan_fallback_openai_chat_url(
|
||||
upstream_base_url: &str,
|
||||
request_query: Option<&str>,
|
||||
@@ -152,6 +241,12 @@ pub fn build_standard_plan_fallback_headers(
|
||||
force_identity_accept_encoding(&mut headers);
|
||||
}
|
||||
|
||||
let declared_connection_headers = crate::headers::declared_connection_header_names(
|
||||
input.request_headers,
|
||||
input.extra_headers,
|
||||
);
|
||||
crate::headers::remove_declared_connection_headers(&mut headers, &declared_connection_headers);
|
||||
|
||||
headers
|
||||
}
|
||||
|
||||
@@ -301,6 +396,10 @@ pub fn build_standard_provider_request_headers(
|
||||
force_identity_accept_encoding(&mut headers);
|
||||
}
|
||||
|
||||
let declared_connection_headers =
|
||||
crate::headers::declared_connection_header_names(input.headers, input.extra_headers);
|
||||
crate::headers::remove_declared_connection_headers(&mut headers, &declared_connection_headers);
|
||||
|
||||
Some(StandardProviderRequestHeaders {
|
||||
headers,
|
||||
auth_header,
|
||||
|
||||
Reference in New Issue
Block a user