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
@@ -12,10 +12,113 @@ use aether_data_contracts::repository::usage::{
};
use aether_data::repository::auth::AuthApiKeyReadRepository;
use aether_data::repository::management_tokens::{
InMemoryManagementTokenRepository, ManagementTokenReadRepository,
ManagementTokenWriteRepository, StoredManagementToken, StoredManagementTokenUserSummary,
StoredManagementTokenWithUser,
};
use aether_data::repository::proxy_nodes::{InMemoryProxyNodeRepository, StoredProxyNode};
use aether_data::repository::proxy_nodes::{ProxyNodeReadRepository, ProxyNodeWriteRepository};
use aether_data::repository::users::{
InMemoryUserReadRepository, StoredUserAuthRecord, UserReadRepository,
};
use sha2::{Digest, Sha256};
use super::{GatewayDataConfig, GatewayDataState};
impl GatewayDataState {
pub(crate) fn with_tunnel_management_auth_for_testkit(
node_id: &str,
tunnel_generation: &str,
raw_token: &str,
encryption_key: impl Into<String>,
) -> Result<Self, aether_data::DataLayerError> {
const TOKEN_ID: &str = "token-tunnel-harness";
const USER_ID: &str = "user-tunnel-harness";
let node = StoredProxyNode::new(
node_id.to_string(),
"tunnel harness node".to_string(),
"127.0.0.1".to_string(),
0,
false,
"offline".to_string(),
30,
0,
0,
0,
0,
0,
true,
false,
0,
)?
.with_tunnel_generation(tunnel_generation.to_string());
let proxy_repository = Arc::new(InMemoryProxyNodeRepository::seed([node]));
let user_summary = StoredManagementTokenUserSummary::new(
USER_ID.to_string(),
Some("[email protected]".to_string()),
"tunnel_harness_admin".to_string(),
"admin".to_string(),
)?;
let token = StoredManagementToken::new(
TOKEN_ID.to_string(),
USER_ID.to_string(),
"tunnel harness token".to_string(),
)?
.with_permissions(Some(serde_json::json!(["admin:proxy_nodes:admin"])));
let token_hash = format!("{:x}", Sha256::digest(raw_token.as_bytes()));
let token_repository = Arc::new(InMemoryManagementTokenRepository::seed_with_hashes(
[StoredManagementTokenWithUser::new(token, user_summary)],
[(token_hash, TOKEN_ID.to_string())],
));
let token_reader: Arc<dyn ManagementTokenReadRepository> = token_repository.clone();
let token_writer: Arc<dyn ManagementTokenWriteRepository> = token_repository;
let user = StoredUserAuthRecord::new(
USER_ID.to_string(),
Some("[email protected]".to_string()),
true,
"tunnel_harness_admin".to_string(),
None,
"admin".to_string(),
"local".to_string(),
None,
None,
None,
true,
false,
None,
None,
)?;
let user_reader: Arc<dyn UserReadRepository> =
Arc::new(InMemoryUserReadRepository::seed_auth_users([user]));
let mut state =
Self::with_proxy_node_repository_for_testkit(proxy_repository, encryption_key);
state.management_token_reader = Some(token_reader);
state.management_token_writer = Some(token_writer);
state.user_reader = Some(user_reader);
Ok(state)
}
pub(crate) fn with_proxy_node_repository_for_testkit<T>(
repository: Arc<T>,
encryption_key: impl Into<String>,
) -> Self
where
T: ProxyNodeReadRepository + ProxyNodeWriteRepository + 'static,
{
let proxy_node_reader: Arc<dyn ProxyNodeReadRepository> = repository.clone();
let proxy_node_writer: Arc<dyn ProxyNodeWriteRepository> = repository;
let mut state = Self::disabled();
state.config = GatewayDataConfig::disabled().with_encryption_key(encryption_key);
state.proxy_node_reader = Some(proxy_node_reader);
state.proxy_node_writer = Some(proxy_node_writer);
state
}
pub(crate) fn with_openai_chat_pressure_repositories_for_testkit<T, U, V>(
auth_api_key_repository: Arc<dyn AuthApiKeyReadRepository>,
candidate_selection_repository: Arc<dyn MinimalCandidateSelectionReadRepository>,