-
Notifications
You must be signed in to change notification settings - Fork 93
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
feat(pii): Scrub arrays of 2 elements as key-value pairs #3639
Merged
Merged
Changes from 11 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
0ef6862
feat(pii): Scrub key value pairs
iambriccardo b5c2d3e
feat(pii): Scrub key value pairs
iambriccardo 7aedc7b
Add code
iambriccardo fd9a5b2
Fix
iambriccardo d1acc1c
Fix
iambriccardo 0967300
Fix
iambriccardo 0672c9c
Fix
iambriccardo 4902859
Update snapshot
iambriccardo 901545f
Change
iambriccardo e13348d
Change
iambriccardo 8b44603
Change
iambriccardo a99c1ae
wip
jjbayer 399814f
wip
jjbayer a2d7868
test
jjbayer 8bfc141
Fix
iambriccardo 7a2c98e
Fix
iambriccardo 0d40b4c
Fix
iambriccardo b3194c9
Fix
iambriccardo 53e68be
Fix
iambriccardo 684b095
Update relay-pii/src/processor.rs
iambriccardo 48aef40
Fix
iambriccardo b96d3d7
Merge branch 'riccardo/feat/add-scrubbing' of github.com:getsentry/re…
iambriccardo 123f6be
Fix
iambriccardo 24d772b
Fix
iambriccardo d776b99
Merge
iambriccardo 815a230
Update CHANGELOG.md
iambriccardo 26842af
Fix
iambriccardo eff37fe
Fix
iambriccardo 685cf83
Fix
iambriccardo d4af9b6
Fix
iambriccardo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,13 @@ use std::sync::OnceLock; | |
|
||
use regex::Regex; | ||
use relay_event_schema::processor::{ | ||
self, enum_set, Chunk, Pii, ProcessValue, ProcessingAction, ProcessingResult, ProcessingState, | ||
Processor, ValueType, | ||
self, enum_set, process_value, Chunk, Pii, ProcessValue, ProcessingAction, ProcessingResult, | ||
ProcessingState, Processor, ValueType, | ||
}; | ||
use relay_event_schema::protocol::{ | ||
AsPair, Event, IpAddr, NativeImagePath, PairList, Replay, ResponseContext, User, | ||
}; | ||
use relay_protocol::{Annotated, Meta, Remark, RemarkType, Value}; | ||
use relay_protocol::{Annotated, Array, Meta, Remark, RemarkType, Value}; | ||
|
||
use crate::compiledconfig::{CompiledPiiConfig, RuleRef}; | ||
use crate::config::RuleType; | ||
|
@@ -101,6 +101,41 @@ impl<'a> Processor for PiiProcessor<'a> { | |
self.apply_all_rules(meta, state, None) | ||
} | ||
|
||
fn process_array<T>( | ||
&mut self, | ||
value: &mut Array<T>, | ||
_meta: &mut Meta, | ||
state: &ProcessingState<'_>, | ||
) -> ProcessingResult | ||
where | ||
T: ProcessValue, | ||
{ | ||
// If the array has length 2, we treat it as key-value pair and try to scrub it. If the | ||
// scrubbing doesn't do anything, we try to scrub values individually. | ||
if value.len() == 2 { | ||
if let Some(key) = value[0].clone().into_value() { | ||
if let Value::String(key_name) = key.into_value() { | ||
if let Some(inner_value) = value[1].value() { | ||
// We compute a new state which has the first element of the array as the | ||
// key. | ||
let entered = state.enter_borrowed( | ||
key_name.as_str(), | ||
state.inner_attrs(), | ||
inner_value.value_type(), | ||
); | ||
process_value(&mut value[1], self, &entered)?; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Recurse into each element of the array since we also want to individually scrub each | ||
// value in the array. | ||
value.process_child_values(self, state)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn process_string( | ||
&mut self, | ||
value: &mut String, | ||
|
@@ -466,10 +501,10 @@ fn insert_replacement_chunks(rule: &RuleRef, text: &str, output: &mut Vec<Chunk< | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use insta::assert_debug_snapshot; | ||
use insta::{allow_duplicates, assert_debug_snapshot}; | ||
use relay_event_schema::processor::process_value; | ||
use relay_event_schema::protocol::{ | ||
Addr, Breadcrumb, DebugImage, DebugMeta, ExtraValue, Headers, LogEntry, Message, | ||
Addr, Breadcrumb, DebugImage, DebugMeta, ExtraValue, FrameVars, Headers, LogEntry, Message, | ||
NativeDebugImage, Request, Span, TagEntry, Tags, TraceContext, | ||
}; | ||
use relay_protocol::{assert_annotated_snapshot, get_value, FromValue, Object}; | ||
|
@@ -489,6 +524,30 @@ mod tests { | |
rv | ||
} | ||
|
||
fn extract_vars(event: Option<&Event>) -> &FrameVars { | ||
event | ||
.unwrap() | ||
.exceptions | ||
.value() | ||
.unwrap() | ||
.values | ||
.value() | ||
.unwrap()[0] | ||
.value() | ||
.unwrap() | ||
.stacktrace | ||
.value() | ||
.unwrap() | ||
.frames | ||
.value() | ||
.unwrap()[0] | ||
.value() | ||
.unwrap() | ||
.vars | ||
.value() | ||
.unwrap() | ||
} | ||
|
||
#[test] | ||
fn test_scrub_original_value() { | ||
let mut data = Event::from_value( | ||
|
@@ -1598,4 +1657,202 @@ mod tests { | |
], | ||
)"#); | ||
} | ||
|
||
#[test] | ||
fn test_tuple_array_scrubbed_with_path_selector() { | ||
// We expect that both of these configs express the same semantics. | ||
let configs = vec![ | ||
// This configuration matches on the authorization element (the 1st element of the array | ||
// represents the key). | ||
r##" | ||
{ | ||
"applications": { | ||
"exception.values.0.stacktrace.frames.0.vars.headers.0.authorization": ["@anything:replace"] | ||
} | ||
} | ||
"##, | ||
// This configuration matches on the 2nd element of the array. | ||
r##" | ||
{ | ||
"applications": { | ||
"exception.values.0.stacktrace.frames.0.vars.headers.0.1": ["@anything:replace"] | ||
} | ||
} | ||
"##, | ||
]; | ||
|
||
let mut event = Event::from_value( | ||
serde_json::json!( | ||
{ | ||
"message": "hi", | ||
"exception": { | ||
"values": [ | ||
{ | ||
"type": "BrokenException", | ||
"value": "Something failed", | ||
"stacktrace": { | ||
"frames": [ | ||
{ | ||
"vars": { | ||
"headers": [ | ||
["authorization", "Bearer abc123"] | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}) | ||
.into(), | ||
); | ||
|
||
for config in configs { | ||
let config = serde_json::from_str::<PiiConfig>(config).unwrap(); | ||
let mut processor = PiiProcessor::new(config.compiled()); | ||
process_value(&mut event, &mut processor, ProcessingState::root()).unwrap(); | ||
|
||
let vars = extract_vars(event.value()); | ||
|
||
allow_duplicates!(assert_debug_snapshot!(vars, @r#" | ||
FrameVars( | ||
{ | ||
"headers": Array( | ||
[ | ||
Array( | ||
[ | ||
String( | ||
"authorization", | ||
), | ||
Annotated( | ||
String( | ||
"[Filtered]", | ||
), | ||
Meta { | ||
remarks: [ | ||
Remark { | ||
ty: Substituted, | ||
rule_id: "@anything:replace", | ||
range: Some( | ||
( | ||
0, | ||
10, | ||
), | ||
), | ||
}, | ||
], | ||
errors: [], | ||
original_length: Some( | ||
13, | ||
), | ||
original_value: None, | ||
}, | ||
), | ||
], | ||
), | ||
], | ||
), | ||
}, | ||
) | ||
"#)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_tuple_array_scrubbed_with_string_selector_and_password_matcher() { | ||
let config = serde_json::from_str::<PiiConfig>( | ||
r##" | ||
{ | ||
"applications": { | ||
"$string": ["@password:remove"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This rule |
||
} | ||
} | ||
"##, | ||
) | ||
.unwrap(); | ||
|
||
let mut event = Event::from_value( | ||
serde_json::json!( | ||
{ | ||
"message": "hi", | ||
"exception": { | ||
"values": [ | ||
{ | ||
"type": "BrokenException", | ||
"value": "Something failed", | ||
"stacktrace": { | ||
"frames": [ | ||
{ | ||
"vars": { | ||
"headers": [ | ||
["authorization", "abc123"] | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}) | ||
.into(), | ||
); | ||
|
||
let mut processor = PiiProcessor::new(config.compiled()); | ||
process_value(&mut event, &mut processor, ProcessingState::root()).unwrap(); | ||
|
||
let vars = extract_vars(event.value()); | ||
|
||
assert_debug_snapshot!(vars, @r###" | ||
FrameVars( | ||
{ | ||
"headers": Array( | ||
[ | ||
Array( | ||
[ | ||
Annotated( | ||
String( | ||
"", | ||
), | ||
Meta { | ||
remarks: [ | ||
Remark { | ||
ty: Removed, | ||
rule_id: "@password:remove", | ||
range: Some( | ||
( | ||
0, | ||
0, | ||
), | ||
), | ||
}, | ||
], | ||
errors: [], | ||
original_length: Some( | ||
13, | ||
), | ||
original_value: None, | ||
}, | ||
), | ||
Meta { | ||
remarks: [ | ||
Remark { | ||
ty: Removed, | ||
rule_id: "@password:remove", | ||
range: None, | ||
}, | ||
], | ||
errors: [], | ||
original_length: None, | ||
original_value: None, | ||
}, | ||
], | ||
), | ||
], | ||
), | ||
}, | ||
) | ||
"###); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought I've seen a helper macro for this already somewhere 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_value!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing