-
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(relay): add view hierarchy scrubbing #4452
Open
Litarnus
wants to merge
20
commits into
master
Choose a base branch
from
martinl/viewhierarchy-scrubbing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f8e1918
feat(relay): initial version for view hierarchy pii scrubbing
Litarnus 827ab9a
feat(relay): wip
Litarnus 321853b
feat(relay): remove unused
Litarnus 7a143a8
Merge branch 'master' into martinl/viewhierarchy-scrubbing
Litarnus f330685
feat(relay): changes after merge
Litarnus 8e8179d
feat(relay): add missing docs
Litarnus 9ae62de
feat(relay): update tests, cleanup unused
Litarnus 1e87396
feat(relay): update comment
Litarnus 0122341
feat(relay): change visibility
Litarnus ff0707b
feat(relay): dependencies
Litarnus 2984a27
lint
Litarnus 030c7b9
python unused import
Litarnus 8189f77
move impl to other impls
Litarnus 7a4d087
remove redundant test case
Litarnus 10bfb24
changelog
Litarnus 3993571
Merge branch 'master' into martinl/viewhierarchy-scrubbing
Litarnus b88cc4e
feedback
Litarnus 9d396e8
update comment
Litarnus cdc6620
error message
Litarnus cd615cc
remove unused dependency
Litarnus 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,180 @@ | ||
use crate::transform::Transform; | ||
use crate::{CompiledPiiConfig, PiiProcessor}; | ||
use relay_event_schema::processor::{FieldAttrs, Pii, ProcessingState, Processor, ValueType}; | ||
use relay_protocol::Meta; | ||
use std::borrow::Cow; | ||
|
||
const FIELD_ATTRS_PII_TRUE: FieldAttrs = FieldAttrs::new().pii(Pii::True); | ||
|
||
/// Describes the error cases that can happen during JSON scrubbing. | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum JsonScrubError { | ||
/// If the transcoding process fails. This will most likely happen if a JSON document | ||
/// is invalid. | ||
#[error("transcoding json failed")] | ||
TranscodeFailed, | ||
} | ||
|
||
/// Visitor for JSON file scrubbing. It will be used to walk through the structure and scrub | ||
/// PII based on the config defined in the processor. | ||
pub struct JsonScrubVisitor<'a> { | ||
processor: PiiProcessor<'a>, | ||
/// The state encoding the current path, which is fed by `push_path` and `pop_path`. | ||
state: ProcessingState<'a>, | ||
/// The current path. This is redundant with `state`, which also contains the full path, | ||
/// but easier to match on. | ||
path: Vec<String>, | ||
} | ||
|
||
impl<'a> JsonScrubVisitor<'a> { | ||
/// Creates a new [`JsonScrubVisitor`] using the supplied config. | ||
pub fn new(config: &'a CompiledPiiConfig) -> Self { | ||
let processor = PiiProcessor::new(config); | ||
Self { | ||
processor, | ||
state: ProcessingState::new_root(None, None), | ||
path: Vec::new(), | ||
} | ||
} | ||
} | ||
|
||
impl<'de> Transform<'de> for JsonScrubVisitor<'de> { | ||
fn push_path(&mut self, key: &'de str) { | ||
self.path.push(key.to_owned()); | ||
|
||
self.state = std::mem::take(&mut self.state).enter_owned( | ||
key.to_owned(), | ||
Some(Cow::Borrowed(&FIELD_ATTRS_PII_TRUE)), | ||
Some(ValueType::String), // Pretend everything is a string. | ||
); | ||
} | ||
|
||
fn pop_path(&mut self) { | ||
if let Ok(Some(parent)) = std::mem::take(&mut self.state).try_into_parent() { | ||
self.state = parent; | ||
} | ||
let popped = self.path.pop(); | ||
debug_assert!(popped.is_some()); // pop_path should never be called on an empty state. | ||
} | ||
|
||
fn transform_str<'a>(&mut self, v: &'a str) -> Cow<'a, str> { | ||
self.transform_string(v.to_owned()) | ||
} | ||
|
||
fn transform_string(&mut self, mut v: String) -> Cow<'static, str> { | ||
let mut meta = Meta::default(); | ||
if self | ||
.processor | ||
.process_string(&mut v, &mut meta, &self.state) | ||
.is_err() | ||
{ | ||
return Cow::Borrowed(""); | ||
} | ||
Cow::Owned(v) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::{PiiAttachmentsProcessor, PiiConfig}; | ||
use serde_json::Value; | ||
|
||
#[test] | ||
pub fn test_view_hierarchy() { | ||
let payload = r#" | ||
{ | ||
"rendering_system": "UIKIT", | ||
"identifier": "192.45.128.54", | ||
"windows": [ | ||
{ | ||
"type": "UIWindow", | ||
"identifier": "123.123.123.123", | ||
"width": 414, | ||
"height": 896, | ||
"x": 0, | ||
"y": 0, | ||
"alpha": 1, | ||
"visible": true, | ||
"children": [] | ||
} | ||
] | ||
} | ||
"# | ||
.as_bytes(); | ||
let config = serde_json::from_str::<PiiConfig>( | ||
r#" | ||
{ | ||
"applications": { | ||
"$string": ["@ip"] | ||
} | ||
} | ||
"#, | ||
) | ||
.unwrap(); | ||
let processor = PiiAttachmentsProcessor::new(config.compiled()); | ||
let result = processor.scrub_json(payload).unwrap(); | ||
let parsed: Value = serde_json::from_slice(&result).unwrap(); | ||
assert_eq!("[ip]", parsed["identifier"].as_str().unwrap()); | ||
} | ||
|
||
#[test] | ||
pub fn test_view_hierarchy_nested_path_rule() { | ||
let payload = r#" | ||
{ | ||
"nested": { | ||
"stuff": { | ||
"ident": "10.0.0.1" | ||
} | ||
} | ||
} | ||
"# | ||
.as_bytes(); | ||
let config = serde_json::from_str::<PiiConfig>( | ||
r#" | ||
{ | ||
"applications": { | ||
"nested.stuff.ident": ["@ip"] | ||
} | ||
} | ||
"#, | ||
) | ||
.unwrap(); | ||
|
||
let processor = PiiAttachmentsProcessor::new(config.compiled()); | ||
let result = processor.scrub_json(payload).unwrap(); | ||
let parsed: Value = serde_json::from_slice(&result).unwrap(); | ||
assert_eq!("[ip]", parsed["nested"]["stuff"]["ident"].as_str().unwrap()); | ||
} | ||
|
||
#[test] | ||
pub fn test_view_hierarchy_not_existing_path() { | ||
let payload = r#" | ||
{ | ||
"nested": { | ||
"stuff": { | ||
"ident": "10.0.0.1" | ||
} | ||
} | ||
} | ||
"# | ||
.as_bytes(); | ||
let config = serde_json::from_str::<PiiConfig>( | ||
r#" | ||
{ | ||
"applications": { | ||
"non.existent.path": ["@ip"] | ||
} | ||
} | ||
"#, | ||
) | ||
.unwrap(); | ||
|
||
let processor = PiiAttachmentsProcessor::new(config.compiled()); | ||
let result = processor.scrub_json(payload).unwrap(); | ||
let parsed: Value = serde_json::from_slice(&result).unwrap(); | ||
assert_eq!( | ||
"10.0.0.1", | ||
parsed["nested"]["stuff"]["ident"].as_str().unwrap() | ||
); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -49,6 +49,7 @@ use serde::de; | |
/// } | ||
/// } | ||
/// ``` | ||
#[allow(missing_docs)] | ||
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. Should we add some docs instead? |
||
pub trait Transform<'de> { | ||
fn push_path(&mut self, _key: &'de str) {} | ||
|
||
|
@@ -839,7 +840,10 @@ where | |
where | ||
S: de::DeserializeSeed<'de>, | ||
{ | ||
self.0.next_element_seed(DeserializeValueSeed(seed, self.1)) | ||
// We want to use a special ValueSeed for sequences that does not call pop_path | ||
// because we don't push paths when entering sequences right now. | ||
self.0 | ||
.next_element_seed(DeserializeSeqValueSeed(seed, self.1)) | ||
} | ||
} | ||
|
||
|
@@ -889,6 +893,24 @@ where | |
} | ||
} | ||
|
||
struct DeserializeSeqValueSeed<'a, D, T>(D, &'a mut T); | ||
|
||
impl<'de, D, T> de::DeserializeSeed<'de> for DeserializeSeqValueSeed<'_, D, T> | ||
where | ||
D: de::DeserializeSeed<'de>, | ||
T: Transform<'de>, | ||
{ | ||
type Value = D::Value; | ||
|
||
fn deserialize<X>(self, deserializer: X) -> Result<Self::Value, X::Error> | ||
where | ||
X: serde::Deserializer<'de>, | ||
{ | ||
self.0 | ||
.deserialize(Deserializer::borrowed(deserializer, self.1)) | ||
} | ||
} | ||
|
||
struct DeserializeKeySeed<'a, D, T>(D, &'a mut T); | ||
|
||
impl<'de, D, T> de::DeserializeSeed<'de> for DeserializeKeySeed<'_, D, T> | ||
|
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 |
---|---|---|
|
@@ -14,4 +14,3 @@ | |
#![warn(missing_docs)] | ||
|
||
pub mod recording; | ||
mod transform; |
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.
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.
nit, to make it super explicit which deserializer is which: