Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pii): Early return if no text left #1957

Merged
merged 2 commits into from
Mar 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion relay-general/src/pii/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,22 @@ fn apply_regex_to_chunks<'a>(
// on the chunks, but the `regex` crate does not support that.

let mut search_string = String::new();
let mut has_text = false;
for chunk in &chunks {
match chunk {
Chunk::Text { text } => search_string.push_str(&text.replace('\x00', "")),
Chunk::Text { text } => {
has_text = true;
search_string.push_str(&text.replace('\x00', ""));
}
Chunk::Redaction { .. } => search_string.push('\x00'),
}
}

if !has_text {
// Nothing to replace.
return chunks;
}

// Early exit if this regex does not match and return the original chunks.
let mut captures_iter = regex.captures_iter(&search_string).peekable();
if captures_iter.peek().is_none() {
Expand Down Expand Up @@ -1032,6 +1041,30 @@ mod tests {
assert_eq!(chunks, res);
}

#[test]
fn test_replace_replaced_text_anything() {
let chunks = vec![Chunk::Redaction {
text: "[Filtered]".into(),
rule_id: "@password:filter".into(),
ty: RemarkType::Substituted,
}];
let rule = RuleRef {
id: "@anything:filter".into(),
origin: "@anything:filter".into(),
ty: RuleType::Anything,
redaction: Redaction::Replace(ReplaceRedaction {
text: "[Filtered]".into(),
}),
};
let res = apply_regex_to_chunks(
chunks.clone(),
&rule,
&Regex::new(r#".*"#).unwrap(),
ReplaceBehavior::Groups(smallvec::smallvec![0]),
);
assert_eq!(chunks, res);
}

#[test]
fn test_scrub_span_data_not_scrubbed() {
let mut span = Annotated::new(Span {
Expand Down