Files
pruefwerk/compliance-core/src/models/credentials.rs
T
sharang f5ac5321bd
CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Docs (push) Successful in 1m15s
CI / Deploy MCP (push) Successful in 5m14s
CI / Deploy Dashboard (push) Successful in 6m48s
CI / Deploy Agent (push) Successful in 9m2s
feat(dast): scans log in with the target's credentials (#1000)
2026-11-16 02:31:38 +00:00

300 lines
9.3 KiB
Rust

//! Which stored fields are credentials (#260).
//!
//! A tenant's git token, issue-tracker token, webhook secret and pentest
//! passwords live inside the records the API returns. [`Credentials`] names
//! those fields once, so the agent can seal every one before a write and strip
//! every one before a record leaves the service, and a field added later only
//! has to be listed here.
use std::collections::HashMap;
use super::dast::{DastAuthConfig, DastTarget};
use super::onboarding::{
Artifact, ArtifactAuth, IssueTrackerConfig, OnboardedTarget, TargetScanConfig,
};
use super::pentest::{PentestAuthConfig, PentestConfig, PentestSession, TestUserCleanup};
/// A record that carries stored credentials.
pub trait Credentials {
/// Every credential field of the record, for sealing before a write.
fn credentials_mut(&mut self) -> Vec<&mut Option<String>>;
/// Maps whose every value is a credential (login headers), sealed and
/// opened value by value. None by default.
fn credential_maps_mut(&mut self) -> Vec<&mut HashMap<String, String>> {
Vec::new()
}
/// Remove every credential before the record leaves the service.
fn redact(&mut self) {
for slot in self.credentials_mut() {
*slot = None;
}
}
/// The record without its credentials, for a handler's return value.
fn redacted(mut self) -> Self
where
Self: Sized,
{
self.redact();
self
}
}
impl<T: Credentials> Credentials for Option<T> {
fn credentials_mut(&mut self) -> Vec<&mut Option<String>> {
self.as_mut().map(T::credentials_mut).unwrap_or_default()
}
fn credential_maps_mut(&mut self) -> Vec<&mut HashMap<String, String>> {
self.as_mut()
.map(T::credential_maps_mut)
.unwrap_or_default()
}
fn redact(&mut self) {
if let Some(inner) = self {
inner.redact();
}
}
}
impl<T: Credentials> Credentials for Vec<T> {
fn credentials_mut(&mut self) -> Vec<&mut Option<String>> {
self.iter_mut().flat_map(T::credentials_mut).collect()
}
fn credential_maps_mut(&mut self) -> Vec<&mut HashMap<String, String>> {
self.iter_mut().flat_map(T::credential_maps_mut).collect()
}
fn redact(&mut self) {
self.iter_mut().for_each(T::redact);
}
}
impl Credentials for ArtifactAuth {
fn credentials_mut(&mut self) -> Vec<&mut Option<String>> {
vec![&mut self.secret]
}
fn redact(&mut self) {
self.secret = None;
// Authentication headers (a cookie, an API-key header) are
// credentials too; nothing writes them through the API, so they are
// only ever removed, never sealed.
self.headers = None;
}
}
impl Credentials for Artifact {
fn credentials_mut(&mut self) -> Vec<&mut Option<String>> {
self.auth.credentials_mut()
}
fn redact(&mut self) {
self.auth.redact();
}
}
impl Credentials for IssueTrackerConfig {
fn credentials_mut(&mut self) -> Vec<&mut Option<String>> {
vec![&mut self.token]
}
}
impl Credentials for PentestAuthConfig {
fn credentials_mut(&mut self) -> Vec<&mut Option<String>> {
let mut slots = vec![&mut self.password, &mut self.imap_password];
slots.extend(self.cleanup.credentials_mut());
slots
}
}
impl Credentials for TestUserCleanup {
fn credentials_mut(&mut self) -> Vec<&mut Option<String>> {
match self {
Self::Keycloak { client_secret, .. } | Self::Auth0 { client_secret, .. } => {
vec![client_secret]
}
Self::Okta { api_token, .. } => vec![api_token],
}
}
}
impl Credentials for PentestConfig {
fn credentials_mut(&mut self) -> Vec<&mut Option<String>> {
self.auth.credentials_mut()
}
}
impl Credentials for TargetScanConfig {
fn credentials_mut(&mut self) -> Vec<&mut Option<String>> {
let mut slots = self.issue_tracker.credentials_mut();
slots.extend(self.pentest.credentials_mut());
slots
}
}
impl Credentials for OnboardedTarget {
fn credentials_mut(&mut self) -> Vec<&mut Option<String>> {
let mut slots = vec![&mut self.webhook_secret];
slots.extend(self.artifacts.credentials_mut());
slots.extend(self.scan_config.credentials_mut());
slots
}
fn redact(&mut self) {
self.webhook_secret = None;
self.artifacts.redact();
self.scan_config.redact();
}
}
impl Credentials for DastAuthConfig {
fn credentials_mut(&mut self) -> Vec<&mut Option<String>> {
vec![&mut self.password, &mut self.token]
}
/// Login headers (an API key, a session cookie) are sent with every scan
/// request, so they are sealed like the token (#1000).
fn credential_maps_mut(&mut self) -> Vec<&mut HashMap<String, String>> {
self.headers.as_mut().into_iter().collect()
}
fn redact(&mut self) {
self.password = None;
self.token = None;
self.headers = None;
}
}
impl Credentials for DastTarget {
fn credentials_mut(&mut self) -> Vec<&mut Option<String>> {
self.auth_config.credentials_mut()
}
fn credential_maps_mut(&mut self) -> Vec<&mut HashMap<String, String>> {
self.auth_config.credential_maps_mut()
}
fn redact(&mut self) {
self.auth_config.redact();
}
}
impl Credentials for PentestSession {
fn credentials_mut(&mut self) -> Vec<&mut Option<String>> {
self.config.credentials_mut()
}
}
#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
use std::collections::HashMap;
use super::*;
use crate::models::{PentestStrategy, TargetType, TrackerType};
fn target_with_every_credential() -> OnboardedTarget {
let mut target = OnboardedTarget::new("t".into(), TargetType::WebApp);
target.webhook_secret = Some("sec-hook".into());
let mut artifact = Artifact::git_repo("https://example.com/r.git", "main");
artifact.auth = Some(ArtifactAuth {
method: "token".into(),
username: Some("bob".into()),
secret: Some("sec-pat".into()),
headers: Some(HashMap::from([("Cookie".into(), "sec-cookie".into())])),
..Default::default()
});
target.artifacts.push(artifact);
target.scan_config.issue_tracker = Some(IssueTrackerConfig {
tracker_type: Some(TrackerType::Gitea),
owner: Some("acme".into()),
repo: Some("web".into()),
token: Some("sec-tracker".into()),
});
target
}
#[test]
fn every_target_credential_is_listed() {
let mut target = target_with_every_credential();
let mut values: Vec<String> = target
.credentials_mut()
.into_iter()
.filter_map(|slot| slot.clone())
.collect();
values.sort();
assert_eq!(values, ["sec-hook", "sec-pat", "sec-tracker"]);
}
#[test]
fn redacted_target_serializes_without_any_credential() {
let mut target = target_with_every_credential();
target.redact();
let json = serde_json::to_string(&target).expect("serialize");
for secret in ["sec-hook", "sec-pat", "sec-tracker", "sec-cookie"] {
assert!(!json.contains(secret), "{secret} leaked: {json}");
}
// What identifies the credential stays, so the UI can say what is set.
assert!(json.contains("bob"));
assert!(json.contains("acme"));
}
#[test]
fn redacted_pentest_session_drops_passwords_but_keeps_the_user() {
let mut session = PentestSession::new("target".into(), PentestStrategy::Quick);
session.config = Some(PentestConfig {
app_url: "https://example.com".into(),
git_repo_url: None,
branch: None,
commit_hash: None,
app_type: None,
rate_limit: None,
auth: PentestAuthConfig {
username: Some("tester".into()),
password: Some("pw".into()),
imap_password: Some("imap".into()),
cleanup: Some(TestUserCleanup::Keycloak {
base_url: "https://login.example.com".into(),
realm: "app".into(),
client_id: "cleanup".into(),
client_secret: Some("kc-secret".into()),
}),
..Default::default()
},
custom_headers: HashMap::new(),
strategy: None,
allow_destructive: false,
initial_instructions: None,
scope_exclusions: Vec::new(),
disclaimer_accepted: true,
disclaimer_accepted_at: None,
environment: Default::default(),
tester: Default::default(),
max_duration_minutes: None,
skip_mode: false,
});
session.redact();
let auth = &session.config.expect("config").auth;
assert_eq!(auth.username.as_deref(), Some("tester"));
assert_eq!(auth.password, None);
assert_eq!(auth.imap_password, None);
assert_eq!(
auth.cleanup,
Some(TestUserCleanup::Keycloak {
base_url: "https://login.example.com".into(),
realm: "app".into(),
client_id: "cleanup".into(),
client_secret: None,
})
);
}
}