-
Notifications
You must be signed in to change notification settings - Fork 103
feat(processor): Add Span Attachment scrubbing logic #5449
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
Draft
tobias-wilfert
wants to merge
1
commit into
master
Choose a base branch
from
tobias-wilfert/feat/attachmentv2-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.
+497
−5
Draft
Changes from all commits
Commits
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 hidden or 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 |
|---|---|---|
|
|
@@ -7,14 +7,15 @@ use relay_event_normalization::{ | |
| }; | ||
| use relay_event_schema::processor::{ProcessingState, ValueType, process_value}; | ||
| use relay_event_schema::protocol::{AttachmentV2Meta, Span, SpanId, SpanV2}; | ||
| use relay_pii::PiiAttachmentsProcessor; | ||
| use relay_protocol::Annotated; | ||
|
|
||
| use crate::envelope::{ContainerItems, EnvelopeHeaders, Item, ItemContainer, ParentId, WithHeader}; | ||
| use crate::managed::Managed; | ||
| use crate::processing::Context; | ||
| use crate::processing::spans::{ | ||
| self, Error, ExpandedAttachment, ExpandedSpan, ExpandedSpans, Result, SampledSpans, | ||
| }; | ||
| use crate::processing::{Context, utils}; | ||
| use crate::services::outcome::DiscardReason; | ||
|
|
||
| /// Parses all serialized spans. | ||
|
|
@@ -224,16 +225,32 @@ fn validate_timestamps(span: &SpanV2) -> Result<()> { | |
| pub fn scrub(spans: &mut Managed<ExpandedSpans>, ctx: Context<'_>) { | ||
| spans.retain( | ||
| |spans| &mut spans.spans, | ||
| |span, _| { | ||
| |span, r| { | ||
| scrub_span(&mut span.span, ctx) | ||
| .inspect_err(|err| relay_log::debug!("failed to scrub pii from span: {err}"))?; | ||
|
|
||
| // TODO: Also scrub the attachment | ||
| span.attachments | ||
| .retain_mut(|attachment| match scrub_attachment(attachment, ctx) { | ||
| Ok(()) => true, | ||
| Err(err) => { | ||
| relay_log::debug!("failed to scrub pii from span attachment: {err}"); | ||
| r.reject_err(err, &*attachment); | ||
| false | ||
| } | ||
| }); | ||
|
|
||
| Ok::<(), Error>(()) | ||
| }, | ||
| ); | ||
|
|
||
| // TODO: Also scrub the standalone attachments | ||
| spans.retain( | ||
| |spans| &mut spans.stand_alone_attachments, | ||
| |attachment, _| { | ||
| scrub_attachment(attachment, ctx).inspect_err(|err| { | ||
| relay_log::debug!("failed to scrub pii from span attachment: {err}") | ||
| }) | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| fn scrub_span(span: &mut Annotated<SpanV2>, ctx: Context<'_>) -> Result<()> { | ||
|
|
@@ -254,6 +271,47 @@ fn scrub_span(span: &mut Annotated<SpanV2>, ctx: Context<'_>) -> Result<()> { | |
| Ok(()) | ||
| } | ||
|
|
||
| fn scrub_attachment(attachment: &mut ExpandedAttachment, ctx: Context<'_>) -> Result<()> { | ||
| let pii_config_from_scrubbing = ctx | ||
| .project_info | ||
| .config | ||
| .datascrubbing_settings | ||
| .pii_config() | ||
| .map_err(|e| Error::PiiConfig(e.clone()))?; | ||
|
|
||
| let ExpandedAttachment { meta, body } = attachment; | ||
|
|
||
| relay_pii::eap::scrub( | ||
| ValueType::Attachments, // TODO: How do we event want to allow people to filter here? | ||
| meta, | ||
| ctx.project_info.config.pii_config.as_ref(), | ||
| pii_config_from_scrubbing.as_ref(), | ||
| )?; | ||
|
|
||
| if let Some(config) = ctx.project_info.config.pii_config.as_ref() | ||
| // FIXME: Not a fan of this at all, the fact that the UI is misleading around this does not help IMO. | ||
| // Also also kind of weird since you can add a bogus rule to get around this (or even add a rule by accident to get around this). | ||
| // From attachments.rs: | ||
| // We temporarily only scrub attachments to projects that have at least one simple attachment rule, | ||
| // such as `$attachments.'foo.txt'`. | ||
| // After we have assessed the impact on performance we can relax this condition. | ||
| && utils::attachments::has_simple_attachment_selector(config) | ||
| { | ||
| let filename = meta | ||
| .value() | ||
| .and_then(|m| m.filename.as_str()) | ||
| .unwrap_or_default(); | ||
|
|
||
| let processor = PiiAttachmentsProcessor::new(config.compiled()); | ||
| let mut payload = body.to_vec(); | ||
| if processor.scrub_attachment(filename, &mut payload) { | ||
| *body = Bytes::from(payload); | ||
| }; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn span_duration(span: &SpanV2) -> Option<Duration> { | ||
| let start_timestamp = *span.start_timestamp.value()?; | ||
| let end_timestamp = *span.end_timestamp.value()?; | ||
|
|
@@ -266,6 +324,7 @@ mod tests { | |
| use relay_pii::{DataScrubbingConfig, PiiConfig}; | ||
| use relay_protocol::SerializableAnnotated; | ||
| use relay_sampling::evaluation::ReservoirCounters; | ||
| use uuid::Uuid; | ||
|
|
||
| use crate::services::projects::project::ProjectInfo; | ||
|
|
||
|
|
@@ -701,6 +760,91 @@ mod tests { | |
| "###); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_scrub_attachment_body() { | ||
| let pii_config = serde_json::from_value::<PiiConfig>(serde_json::json!({ | ||
| "rules": {"0": {"type": "ip", "redaction": {"method": "remove"}}}, | ||
| "applications": {"$attachments.'data.txt'": ["0"]} | ||
| })) | ||
| .unwrap(); | ||
|
|
||
| let mut attachment = ExpandedAttachment { | ||
| meta: Annotated::new(AttachmentV2Meta { | ||
| attachment_id: Annotated::new(Uuid::new_v4()), | ||
| filename: Annotated::new("data.txt".to_owned()), | ||
| content_type: Annotated::new("text/plain".to_owned()), | ||
| ..Default::default() | ||
| }), | ||
| body: Bytes::from("Some IP: 127.0.0.1"), | ||
| }; | ||
|
|
||
| let ctx = make_context(DataScrubbingConfig::default(), Some(pii_config)); | ||
| scrub_attachment(&mut attachment, ctx).unwrap(); | ||
|
|
||
| assert_eq!(attachment.body, "Some IP: *********"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_scrub_attachment_meta() { | ||
| use relay_event_schema::protocol::{Attribute, AttributeType, Attributes}; | ||
| use relay_protocol::Value; | ||
|
|
||
| let pii_config = serde_json::from_value::<PiiConfig>(serde_json::json!({ | ||
| "applications": {"$string": ["@email:replace"]} | ||
| })) | ||
| .unwrap(); | ||
|
|
||
| let mut attributes = Attributes::new(); | ||
| attributes.0.insert( | ||
| "email_attr".to_owned(), | ||
| Annotated::new(Attribute::new( | ||
| AttributeType::String, | ||
| Value::String("[email protected]".to_owned()), | ||
| )), | ||
| ); | ||
|
|
||
| let mut attachment = ExpandedAttachment { | ||
| meta: Annotated::new(AttachmentV2Meta { | ||
| attachment_id: Annotated::new(Uuid::new_v4()), | ||
| filename: Annotated::new("data.txt".to_owned()), | ||
| content_type: Annotated::new("text/plain".to_owned()), | ||
| attributes: Annotated::new(attributes), | ||
| ..Default::default() | ||
| }), | ||
| body: Bytes::from("Some attachment body"), | ||
| }; | ||
|
|
||
| let ctx = make_context(DataScrubbingConfig::default(), Some(pii_config)); | ||
| scrub_attachment(&mut attachment, ctx).unwrap(); | ||
|
|
||
| let attrs = &attachment.meta.value().unwrap().attributes; | ||
| insta::assert_json_snapshot!(SerializableAnnotated(attrs), @r#" | ||
| { | ||
| "email_attr": { | ||
| "type": "string", | ||
| "value": "[email]" | ||
| }, | ||
| "_meta": { | ||
| "email_attr": { | ||
| "value": { | ||
| "": { | ||
| "rem": [ | ||
| [ | ||
| "@email:replace", | ||
| "s", | ||
| 0, | ||
| 7 | ||
| ] | ||
| ], | ||
| "len": 20 | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| "#); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_validate_timestamp_start_before_end() { | ||
| validate_timestamps(&SpanV2 { | ||
|
|
||
This file contains hidden or 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.
For me this is actually very confusing and ideally I would like to change it if we can.
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.
Feel free to remove the extra check for span attachments, they will start out as low-volume anyway.