Skip to content

Commit 91c7370

Browse files
authored
fix(pii): Early return if no text left (#1957)
If a list of chunks consists only of replacements, the following debug assertion at the end of `apply_replacement_chunks` might fail (see test): https://github.com/getsentry/relay/blob/a8ee53cc26b8d7bd10cfa919e672894edbd65916/relay-general/src/pii/processor.rs#L346 Add an early return to prevent this failure. This popped up in a test case in #1951.
1 parent 3cbfdd1 commit 91c7370

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

relay-general/src/pii/processor.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,22 @@ fn apply_regex_to_chunks<'a>(
263263
// on the chunks, but the `regex` crate does not support that.
264264

265265
let mut search_string = String::new();
266+
let mut has_text = false;
266267
for chunk in &chunks {
267268
match chunk {
268-
Chunk::Text { text } => search_string.push_str(&text.replace('\x00', "")),
269+
Chunk::Text { text } => {
270+
has_text = true;
271+
search_string.push_str(&text.replace('\x00', ""));
272+
}
269273
Chunk::Redaction { .. } => search_string.push('\x00'),
270274
}
271275
}
272276

277+
if !has_text {
278+
// Nothing to replace.
279+
return chunks;
280+
}
281+
273282
// Early exit if this regex does not match and return the original chunks.
274283
let mut captures_iter = regex.captures_iter(&search_string).peekable();
275284
if captures_iter.peek().is_none() {
@@ -1032,6 +1041,30 @@ mod tests {
10321041
assert_eq!(chunks, res);
10331042
}
10341043

1044+
#[test]
1045+
fn test_replace_replaced_text_anything() {
1046+
let chunks = vec![Chunk::Redaction {
1047+
text: "[Filtered]".into(),
1048+
rule_id: "@password:filter".into(),
1049+
ty: RemarkType::Substituted,
1050+
}];
1051+
let rule = RuleRef {
1052+
id: "@anything:filter".into(),
1053+
origin: "@anything:filter".into(),
1054+
ty: RuleType::Anything,
1055+
redaction: Redaction::Replace(ReplaceRedaction {
1056+
text: "[Filtered]".into(),
1057+
}),
1058+
};
1059+
let res = apply_regex_to_chunks(
1060+
chunks.clone(),
1061+
&rule,
1062+
&Regex::new(r#".*"#).unwrap(),
1063+
ReplaceBehavior::Groups(smallvec::smallvec![0]),
1064+
);
1065+
assert_eq!(chunks, res);
1066+
}
1067+
10351068
#[test]
10361069
fn test_scrub_span_data_not_scrubbed() {
10371070
let mut span = Annotated::new(Span {

0 commit comments

Comments
 (0)