feat: complete Epic 1 production foundation

This commit is contained in:
bsodfather committed 2026-08-25 01:24:11 +03:00
1 parent 767428436d
commit 182bde8ac0
298 files changed
+35711 -5291

No files matched your search

+136 -1
View File
@@ -6,7 +6,7 @@ use serde_json::Value;
use crate::{PreparedRequest, RuntimeError};
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq)]
pub enum ResolvedAuth {
Bearer { header_name: String, token: String },
Basic { username: String, password: String },
@@ -14,11 +14,34 @@ pub enum ResolvedAuth {
ApiKeyQuery { param_name: String, value: String },
}
impl std::fmt::Debug for ResolvedAuth {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let kind = match self {
Self::Bearer { .. } => "bearer",
Self::Basic { .. } => "basic",
Self::ApiKeyHeader { .. } => "api_key_header",
Self::ApiKeyQuery { .. } => "api_key_query",
};
formatter
.debug_struct("ResolvedAuth")
.field("kind", &kind)
.field("material", &"[REDACTED]")
.finish()
}
}
impl ResolvedAuth {
pub fn from_profile(
profile: &AuthProfile,
secrets: &BTreeMap<SecretId, Value>,
) -> Result<Self, RuntimeError> {
if profile.kind != profile.config.kind() {
return Err(RuntimeError::InvalidAuthProfileConfig {
auth_profile_id: profile.id.as_str().to_owned(),
reason: "auth kind and config kind do not match".to_owned(),
});
}
match &profile.config {
AuthConfig::Bearer(config) => Ok(Self::Bearer {
header_name: config.header_name.clone(),
@@ -50,20 +73,32 @@ impl ResolvedAuth {
pub fn apply(&self, prepared_request: &mut PreparedRequest) {
match self {
Self::Bearer { header_name, token } => {
remove_header_case_insensitive(&mut prepared_request.headers, header_name);
prepared_request
.headers
.insert(header_name.clone(), format!("Bearer {token}"));
prepared_request
.trusted_header_names
.insert(header_name.to_ascii_lowercase());
}
Self::Basic { username, password } => {
let credentials = STANDARD.encode(format!("{username}:{password}"));
remove_header_case_insensitive(&mut prepared_request.headers, "Authorization");
prepared_request
.headers
.insert("Authorization".to_owned(), format!("Basic {credentials}"));
prepared_request
.trusted_header_names
.insert("authorization".to_owned());
}
Self::ApiKeyHeader { header_name, value } => {
remove_header_case_insensitive(&mut prepared_request.headers, header_name);
prepared_request
.headers
.insert(header_name.clone(), value.clone());
prepared_request
.trusted_header_names
.insert(header_name.to_ascii_lowercase());
}
Self::ApiKeyQuery { param_name, value } => {
prepared_request
@@ -74,6 +109,17 @@ impl ResolvedAuth {
}
}
fn remove_header_case_insensitive(headers: &mut BTreeMap<String, String>, header_name: &str) {
let existing = headers
.keys()
.filter(|candidate| candidate.eq_ignore_ascii_case(header_name))
.cloned()
.collect::<Vec<_>>();
for existing in existing {
headers.remove(&existing);
}
}
pub(crate) fn apply_resolved_auth(
mut prepared_request: PreparedRequest,
resolved_auth: Option<&ResolvedAuth>,
@@ -167,6 +213,95 @@ mod tests {
);
}
#[test]
fn bearer_auth_overwrites_untrusted_mapped_header_and_marks_it_trusted() {
let auth = ResolvedAuth::Bearer {
header_name: "Authorization".to_owned(),
token: "system-token".to_owned(),
};
let mut request = PreparedRequest {
headers: BTreeMap::from([
("authorization".to_owned(), "Bearer attacker".to_owned()),
(
"AUTHORIZATION".to_owned(),
"Bearer second-attacker".to_owned(),
),
]),
..PreparedRequest::default()
};
auth.apply(&mut request);
assert_eq!(
request.headers.get("Authorization").map(String::as_str),
Some("Bearer system-token")
);
assert!(!request.headers.contains_key("authorization"));
assert!(!request.headers.contains_key("AUTHORIZATION"));
assert!(request.trusted_header_names.contains("authorization"));
}
#[test]
fn api_key_header_auth_overwrites_untrusted_mapped_header_and_marks_it_trusted() {
let auth = ResolvedAuth::ApiKeyHeader {
header_name: "X-Api-Key".to_owned(),
value: "system-key".to_owned(),
};
let mut request = PreparedRequest {
headers: BTreeMap::from([
("x-api-key".to_owned(), "attacker".to_owned()),
("X-API-KEY".to_owned(), "second-attacker".to_owned()),
]),
..PreparedRequest::default()
};
auth.apply(&mut request);
assert_eq!(
request.headers.get("X-Api-Key").map(String::as_str),
Some("system-key")
);
assert!(!request.headers.contains_key("x-api-key"));
assert!(!request.headers.contains_key("X-API-KEY"));
assert!(request.trusted_header_names.contains("x-api-key"));
}
#[test]
fn rejects_auth_profile_kind_config_mismatch_before_secret_use() {
let error = ResolvedAuth::from_profile(
&AuthProfile {
id: "auth_01".into(),
workspace_id: WorkspaceId::new("ws_default"),
name: "mismatch".to_owned(),
kind: AuthKind::Bearer,
config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig {
header_name: "X-Api-Key".to_owned(),
secret_id: SecretId::new("secret_api_key"),
}),
created_at: timestamp("2026-04-07T00:00:00Z"),
updated_at: timestamp("2026-04-07T00:00:00Z"),
},
&BTreeMap::from([(SecretId::new("secret_api_key"), json!("key-123"))]),
)
.unwrap_err();
assert!(matches!(
error,
crate::RuntimeError::InvalidAuthProfileConfig { .. }
));
}
#[test]
fn resolved_auth_debug_never_exposes_secret_material() {
let auth = ResolvedAuth::Bearer {
header_name: "Authorization".to_owned(),
token: "story18-secret-canary".to_owned(),
};
let rendered = format!("{auth:?}");
assert!(!rendered.contains("story18-secret-canary"));
assert!(rendered.contains("[REDACTED]"));
}
#[test]
fn applies_basic_auth_to_headers() {
let auth = ResolvedAuth::from_profile(