Skip to content

Commit

Permalink
feat(csp): Include blocked domain as a tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde committed Jan 9, 2025
1 parent 9a4ec66 commit d61a4d1
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
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://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

0 comments on commit d61a4d1

Please sign in to comment.