Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

feat(pii): sanitize OCR text_json bounding boxes (#2119)

Louis Beaumontc0d5cce10133
diff --git a/screenpipe-core/src/pii_removal.rs b/screenpipe-core/src/pii_removal.rs
index 96c9e793e..7b701f631 100644
--- a/screenpipe-core/src/pii_removal.rs
+++ b/screenpipe-core/src/pii_removal.rs
@@ -60,6 +60,23 @@ pub fn remove_pii(text: &str) -> String {
sanitized
}
+/// Remove PII from OCR text_json entries
+///
+/// This function sanitizes the "text" field in each OCR bounding box entry,
+/// preserving the coordinate information while redacting any PII.
+pub fn remove_pii_from_text_json(text_json: &[HashMap<String, String>]) -> Vec<HashMap<String, String>> {
+ text_json
+ .iter()
+ .map(|entry| {
+ let mut sanitized_entry = entry.clone();
+ if let Some(text) = sanitized_entry.get("text") {
+ sanitized_entry.insert("text".to_string(), remove_pii(text));
+ }
+ sanitized_entry
+ })
+ .collect()
+}
+
/// Check if a given text contains PII
pub fn contains_pii(text: &str) -> bool {
for (pattern, _) in PII_PATTERNS.iter() {
@@ -455,4 +472,75 @@ mod tests {
duration
);
}
+
+ // ==================== TEXT_JSON PII REMOVAL TESTS ====================
+
+ #[test]
+ fn test_remove_pii_from_text_json_basic() {
+ let text_json = vec![
+ HashMap::from([
+ ("text".to_string(), "[email protected]".to_string()),
+ ("left".to_string(), "100".to_string()),
+ ("top".to_string(), "200".to_string()),
+ ]),
+ HashMap::from([
+ ("text".to_string(), "Normal text".to_string()),
+ ("left".to_string(), "150".to_string()),
+ ("top".to_string(), "250".to_string()),
+ ]),
+ ];
+
+ let result = remove_pii_from_text_json(&text_json);
+
+ assert_eq!(result.len(), 2);
+ assert_eq!(result[0].get("text").unwrap(), "[EMAIL]");
+ assert_eq!(result[0].get("left").unwrap(), "100"); // Coordinates preserved
+ assert_eq!(result[1].get("text").unwrap(), "Normal text"); // Non-PII unchanged
+ }
+
+ #[test]
+ fn test_remove_pii_from_text_json_multiple_pii_types() {
+ let text_json = vec![
+ HashMap::from([
+ ("text".to_string(), "4532-1234-5678-9012".to_string()),
+ ("left".to_string(), "10".to_string()),
+ ]),
+ HashMap::from([
+ ("text".to_string(), "123-45-6789".to_string()),
+ ("left".to_string(), "20".to_string()),
+ ]),
+ HashMap::from([
+ ("text".to_string(), "555-123-4567".to_string()),
+ ("left".to_string(), "30".to_string()),
+ ]),
+ ];
+
+ let result = remove_pii_from_text_json(&text_json);
+
+ assert_eq!(result[0].get("text").unwrap(), "[CREDIT_CARD]");
+ assert_eq!(result[1].get("text").unwrap(), "[SSN]");
+ assert_eq!(result[2].get("text").unwrap(), "[PHONE]");
+ }
+
+ #[test]
+ fn test_remove_pii_from_text_json_empty() {
+ let text_json: Vec<HashMap<String, String>> = vec![];
+ let result = remove_pii_from_text_json(&text_json);
+ assert!(result.is_empty());
+ }
+
+ #[test]
+ fn test_remove_pii_from_text_json_no_text_field() {
+ let text_json = vec![HashMap::from([
+ ("left".to_string(), "100".to_string()),
+ ("top".to_string(), "200".to_string()),
+ ])];
+
+ let result = remove_pii_from_text_json(&text_json);
+
+ // Should return entry unchanged if no text field
+ assert_eq!(result.len(), 1);
+ assert!(!result[0].contains_key("text"));
+ assert_eq!(result[0].get("left").unwrap(), "100");
+ }
}
diff --git a/screenpipe-server/src/core.rs b/screenpipe-server/src/core.rs
index 82f453d7e..71025e851 100644
--- a/screenpipe-server/src/core.rs
+++ b/screenpipe-server/src/core.rs
@@ -1,7 +1,7 @@
use crate::VideoCapture;
use anyhow::Result;
use futures::future::join_all;
-use screenpipe_core::pii_removal::remove_pii;
+use screenpipe_core::pii_removal::{remove_pii, remove_pii_from_text_json};
use screenpipe_core::Language;
use screenpipe_db::{DatabaseManager, Speaker};
use screenpipe_events::{poll_meetings_events, send_event};
@@ -287,14 +287,17 @@ pub async fn record_video(
frame_id,
insert_duration.as_millis()
);
- let text_json =
- serde_json::to_string(&window_result.text_json).unwrap_or_default();
- let text = if use_pii_removal {
- &remove_pii(&window_result.text)
+ // Apply PII removal if enabled
+ let (text, sanitized_text_json) = if use_pii_removal {
+ let sanitized_text = remove_pii(&window_result.text);
+ let sanitized_json = remove_pii_from_text_json(&window_result.text_json);
+ (sanitized_text, sanitized_json)
} else {
- &window_result.text
+ (window_result.text.clone(), window_result.text_json.clone())
};
+ let text_json =
+ serde_json::to_string(&sanitized_text_json).unwrap_or_default();
if realtime_vision {
let send_event_start = std::time::Instant::now();
@@ -303,7 +306,7 @@ pub async fn record_video(
WindowOcr {
image: Some(frame.image.clone()),
text: text.clone(),
- text_json: window_result.text_json.clone(),
+ text_json: sanitized_text_json.clone(),
app_name: window_result.app_name.clone(),
window_name: window_result.window_name.clone(),
focused: window_result.focused,
@@ -329,7 +332,7 @@ pub async fn record_video(
if let Err(e) = db
.insert_ocr_text(
frame_id,
- text,
+ &text,
&text_json,
Arc::new((*ocr_engine).clone().into()),
)