Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(pii): add redact_identifiers for structured PII

Steven Enamakelb1e3d7743ff4
diff --git a/crates/openhuman-core/src/security/pii/detector.rs b/crates/openhuman-core/src/security/pii/detector.rs
index 2db45dcbac..b04ac3123c 100644
--- a/crates/openhuman-core/src/security/pii/detector.rs
+++ b/crates/openhuman-core/src/security/pii/detector.rs
@@ -94,6 +94,38 @@ pub fn scan(content: &str) -> PiiScanResult {
}
}
+/// Replacement for every value [`redact_identifiers`] removes.
+pub const REDACTED_IDENTIFIER: &str = "[redacted]";
+
+/// Replace every **structured identifier** in `content` (email, phone number,
+/// national id, payment card, IBAN, IPv4 address, postal address) with
+/// [`REDACTED_IDENTIFIER`].
+///
+/// Uses the same rules (and validators) as [`scan`], so what is redacted is
+/// exactly what would be counted. Topical keyword categories (medical, legal,
+/// ...) describe context rather than identify anyone and are left in place.
+/// For text shown back to a user from a source that may quote someone's
+/// details, such as a tool's error line.
+pub fn redact_identifiers(content: &str) -> String {
+ let mut out = content.to_string();
+ for rule in rules() {
+ if !rule.category.is_structured_identifier() {
+ continue;
+ }
+ let redacted = rule.regex.replace_all(&out, |caps: &regex::Captures<'_>| {
+ let matched = &caps[0];
+ match rule.validator {
+ Some(validator) if !validator(matched) => matched.to_string(),
+ _ => REDACTED_IDENTIFIER.to_string(),
+ }
+ });
+ if let std::borrow::Cow::Owned(changed) = redacted {
+ out = changed;
+ }
+ }
+ out
+}
+
#[cfg(test)]
#[path = "detector_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/security/pii/mod.rs b/crates/openhuman-core/src/security/pii/mod.rs
index f807b74d96..da30c5c171 100644
--- a/crates/openhuman-core/src/security/pii/mod.rs
+++ b/crates/openhuman-core/src/security/pii/mod.rs
@@ -33,5 +33,5 @@ mod types;
#[path = "pii_tests.rs"]
mod tests;
-pub use detector::scan;
+pub use detector::{redact_identifiers, scan, REDACTED_IDENTIFIER};
pub use types::{CategoryHit, PiiCategory, PiiScanResult, RiskLevel};
diff --git a/crates/openhuman-core/src/security/pii/pii_tests.rs b/crates/openhuman-core/src/security/pii/pii_tests.rs
index 9ca8586bb1..23305094b0 100644
--- a/crates/openhuman-core/src/security/pii/pii_tests.rs
+++ b/crates/openhuman-core/src/security/pii/pii_tests.rs
@@ -10,7 +10,7 @@
//! * category identifiers serialize to stable snake_case strings.
use super::rules::{is_ipv4, is_luhn_valid};
-use super::{scan, PiiCategory, RiskLevel};
+use super::{redact_identifiers, scan, PiiCategory, RiskLevel};
// ---------------------------------------------------------------------------
// Per-category detection (recall / true positives)
@@ -316,3 +316,27 @@ fn ipv4_validator_bounds_octets() {
assert!(!is_ipv4("256.1.1.1"));
assert!(!is_ipv4("1.2.3"));
}
+
+// ---------------------------------------------------------------------------
+// Redaction
+// ---------------------------------------------------------------------------
+
+#[test]
+fn redact_identifiers_replaces_structured_values_only() {
+ let out = redact_identifiers(
+ "user [email protected] logged in from 203.0.113.7, call +1 415-555-0100 about the patient",
+ );
+ assert!(!out.contains("[email protected]"), "{out}");
+ assert!(!out.contains("203.0.113.7"), "{out}");
+ assert!(!out.contains("415-555-0100"), "{out}");
+ assert!(out.contains("[redacted]"), "{out}");
+ // Topical keywords are context, not identifiers: kept.
+ assert!(out.contains("patient"), "{out}");
+}
+
+#[test]
+fn redact_identifiers_keeps_values_that_fail_validation() {
+ // Not a valid IPv4 address, not a Luhn-valid card: left as written.
+ let text = "version 999.1.1.1 build 1234567890123456";
+ assert_eq!(redact_identifiers(text), text);
+}
diff --git a/crates/openhuman-core/src/security/pii/types.rs b/crates/openhuman-core/src/security/pii/types.rs
index e67da9ddea..b6d306bdd9 100644
--- a/crates/openhuman-core/src/security/pii/types.rs
+++ b/crates/openhuman-core/src/security/pii/types.rs
@@ -149,6 +149,22 @@ impl PiiCategory {
}
}
+ /// Whether this category is a concrete identifying value (an address, a
+ /// number) rather than topical context. These are what
+ /// [`crate::security::pii::redact_identifiers`] replaces.
+ pub fn is_structured_identifier(self) -> bool {
+ matches!(
+ self,
+ PiiCategory::Email
+ | PiiCategory::PhoneNumber
+ | PiiCategory::NationalId
+ | PiiCategory::CreditCard
+ | PiiCategory::BankAccount
+ | PiiCategory::IpAddress
+ | PiiCategory::PostalAddress
+ )
+ }
+
/// A "strong identifier" is a high-confidence, directly-identifying value
/// whose mere presence should force [`RiskLevel::High`], independent of the
/// numeric score. Guards recall against future weight edits.