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
+19 -7
View File
@@ -27,9 +27,10 @@ use crate::worker::{
use crate::{
apply_usage_body_capture_policy_to_event, build_stream_terminal_usage_seed,
build_sync_terminal_usage_seed, build_terminal_usage_event_from_seed,
build_upsert_usage_record_from_event, settle_usage_if_needed, LifecycleUsageSeed,
StreamTerminalUsagePayloadSeed, SyncTerminalUsagePayloadSeed, TerminalUsageContextSeed,
UsageEvent, UsageQueue, UsageRecordWriter, UsageRuntimeConfig, UsageSettlementWriter,
build_upsert_usage_record_from_event, reconcile_usage_policy_cost_for_event,
settle_usage_if_needed, LifecycleUsageSeed, StreamTerminalUsagePayloadSeed,
SyncTerminalUsagePayloadSeed, TerminalUsageContextSeed, UsageEvent, UsageQueue,
UsageRecordWriter, UsageRuntimeConfig, UsageSettlementWriter,
};
#[async_trait]
@@ -39,8 +40,8 @@ pub trait UsageBillingEventEnricher: Send + Sync {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum UsageRequestRecordLevel {
Basic,
#[default]
Basic,
Full,
}
@@ -55,7 +56,7 @@ pub struct UsageBodyCapturePolicy {
impl Default for UsageBodyCapturePolicy {
fn default() -> Self {
Self {
record_level: UsageRequestRecordLevel::Full,
record_level: UsageRequestRecordLevel::Basic,
}
}
}
@@ -4130,9 +4131,9 @@ impl UsageRuntime {
event_name = "usage_body_capture_policy_read_failed",
log_type = "event",
request_id = %event.request_id,
fallback = "default",
fallback = "basic",
error = %err,
"usage runtime failed to read body capture policy; keeping default capture"
"usage runtime failed to read body capture policy; disabling body capture"
);
apply_usage_body_capture_policy_to_event(UsageBodyCapturePolicy::default(), event);
}
@@ -4765,6 +4766,17 @@ impl UsageRuntime {
where
T: UsageRuntimeAccess,
{
if let Err(err) = reconcile_usage_policy_cost_for_event(data, event).await {
warn!(
event_name = "usage_event_cost_reconciliation_failed",
log_type = "event",
usage_event_type = ?event.event_type,
request_id = %event.request_id,
error = %err,
"usage runtime failed to reconcile plan cost before direct usage upsert"
);
return false;
}
match build_upsert_usage_record_from_event(event) {
Ok(record) => match catch_usage_writer_panic(
"direct usage upsert",