244 lines
7.3 KiB
Rust
244 lines
7.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 super::dast::{DastAuthConfig, DastTarget};
|
|
use super::onboarding::{
|
|
Artifact, ArtifactAuth, IssueTrackerConfig, OnboardedTarget, TargetScanConfig,
|
|
};
|
|
use super::pentest::{PentestAuthConfig, PentestConfig, PentestSession};
|
|
|
|
/// 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>>;
|
|
|
|
/// 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 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 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>> {
|
|
vec![&mut self.password, &mut self.imap_password]
|
|
}
|
|
}
|
|
|
|
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]
|
|
}
|
|
|
|
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 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()),
|
|
..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);
|
|
}
|
|
}
|