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

feat(csp): Include blocked domain as a tag #4435

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**Features**:

- Scrub non-minidump attachments if there are explicit `$attachment` rules. ([#4415](https://github.com/getsentry/relay/pull/4415))
- Include blocked domain in CSP reports as a tag. ([#4435](https://github.com/getsentry/relay/pull/4435))

**Internal**:

Expand Down
47 changes: 37 additions & 10 deletions relay-event-schema/src/protocol/security_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ impl CspRaw {
}

fn get_tags(&self, effective_directive: CspDirective) -> Tags {
Tags(PairList::from(vec![
let mut tags = vec![
Annotated::new(TagEntry(
Annotated::new("effective-directive".to_string()),
Annotated::new(effective_directive.to_string()),
Expand All @@ -472,7 +472,18 @@ impl CspRaw {
Annotated::new("blocked-uri".to_string()),
Annotated::new(self.sanitized_blocked_uri()),
)),
]))
];

if let Ok(url) = Url::parse(&self.blocked_uri) {
if let ("http" | "https", Some(host)) = (url.scheme(), url.host_str()) {
tags.push(Annotated::new(TagEntry(
Annotated::new("blocked-host".to_string()),
Annotated::new(host.to_owned()),
)));
}
}

Tags(PairList::from(tags))
}

fn get_request(&self) -> Request {
Expand Down Expand Up @@ -1251,7 +1262,7 @@ mod tests {
let mut event = Event::default();
Csp::apply_to_event(json.as_bytes(), &mut event).unwrap();

assert_annotated_snapshot!(Annotated::new(event), @r#"
assert_annotated_snapshot!(Annotated::new(event), @r###"
{
"culprit": "style-src cdn.example.com",
"logentry": {
Expand All @@ -1268,6 +1279,10 @@ mod tests {
[
"blocked-uri",
"http://example.com/lol.css"
],
[
"blocked-host",
"example.com"
]
],
"csp": {
Expand All @@ -1278,7 +1293,7 @@ mod tests {
"violated_directive": "style-src cdn.example.com"
}
}
"#);
"###);
}

#[test]
Expand Down Expand Up @@ -1337,7 +1352,7 @@ mod tests {
let mut event = Event::default();
Csp::apply_to_event(json.as_bytes(), &mut event).unwrap();

assert_annotated_snapshot!(Annotated::new(event), @r#"
assert_annotated_snapshot!(Annotated::new(event), @r###"
{
"culprit": "default-src self",
"logentry": {
Expand All @@ -1360,6 +1375,10 @@ mod tests {
[
"blocked-uri",
"http://evilhackerscripts.com"
],
[
"blocked-host",
"evilhackerscripts.com"
]
],
"csp": {
Expand All @@ -1371,7 +1390,7 @@ mod tests {
"violated_directive": "default-src self"
}
}
"#);
"###);
}

#[test]
Expand All @@ -1396,7 +1415,7 @@ mod tests {
let mut event = Event::default();
Csp::apply_to_event(json.as_bytes(), &mut event).unwrap();

assert_annotated_snapshot!(Annotated::new(event), @r#"
assert_annotated_snapshot!(Annotated::new(event), @r###"
{
"culprit": "script-src",
"logentry": {
Expand All @@ -1419,6 +1438,10 @@ mod tests {
[
"blocked-uri",
"http://baddomain.com/test.js?_=1515535030116"
],
[
"blocked-host",
"baddomain.com"
]
],
"csp": {
Expand All @@ -1436,7 +1459,7 @@ mod tests {
"disposition": "enforce"
}
}
"#);
"###);
}

#[test]
Expand Down Expand Up @@ -1559,7 +1582,7 @@ mod tests {

let mut event = Event::default();
Csp::apply_to_event(json.as_bytes(), &mut event).unwrap();
insta::assert_debug_snapshot!(event.tags, @r#"
insta::assert_debug_snapshot!(event.tags, @r###"
Tags(
PairList(
[
Expand All @@ -1571,10 +1594,14 @@ mod tests {
"blocked-uri",
"https://api.stripe.com/v1/tokens",
),
TagEntry(
"blocked-host",
"api.stripe.com",
),
],
),
)
"#);
"###);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
[
"blocked-uri",
"http://evilhackerscripts.com"
],
[
"blocked-host",
"evilhackerscripts.com"
]
],
"user": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
[
"blocked-uri",
"http://evilhackerscripts.com"
],
[
"blocked-host",
"evilhackerscripts.com"
]
],
"key_id": "123",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
[
"blocked-uri",
"http://localhost:8000/lol.css"
],
[
"blocked-host",
"localhost"
]
],
"user": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
[
"blocked-uri",
"http://notlocalhost:8000/lol.css"
],
[
"blocked-host",
"notlocalhost"
]
],
"user": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
[
"blocked-uri",
"http://notlocalhost:8000/lol.css"
],
[
"blocked-host",
"notlocalhost"
]
],
"user": {
Expand Down
Loading