Skip to content

Commit 6833548

Browse files
committed
feat(csp): Include blocked domain as a tag
1 parent 9a4ec66 commit 6833548

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
**Features**:
66

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

910
**Internal**:
1011

relay-event-schema/src/protocol/security_report.rs

+37-10
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ impl CspRaw {
463463
}
464464

465465
fn get_tags(&self, effective_directive: CspDirective) -> Tags {
466-
Tags(PairList::from(vec![
466+
let mut tags = vec![
467467
Annotated::new(TagEntry(
468468
Annotated::new("effective-directive".to_string()),
469469
Annotated::new(effective_directive.to_string()),
@@ -472,7 +472,18 @@ impl CspRaw {
472472
Annotated::new("blocked-uri".to_string()),
473473
Annotated::new(self.sanitized_blocked_uri()),
474474
)),
475-
]))
475+
];
476+
477+
if let Ok(url) = Url::parse(&self.blocked_uri) {
478+
if let ("http" | "https", Some(host)) = (url.scheme(), url.host_str()) {
479+
tags.push(Annotated::new(TagEntry(
480+
Annotated::new("blocked-host".to_string()),
481+
Annotated::new(host.to_owned()),
482+
)));
483+
}
484+
}
485+
486+
Tags(PairList::from(tags))
476487
}
477488

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

1254-
assert_annotated_snapshot!(Annotated::new(event), @r#"
1265+
assert_annotated_snapshot!(Annotated::new(event), @r###"
12551266
{
12561267
"culprit": "style-src cdn.example.com",
12571268
"logentry": {
@@ -1268,6 +1279,10 @@ mod tests {
12681279
[
12691280
"blocked-uri",
12701281
"http://example.com/lol.css"
1282+
],
1283+
[
1284+
"blocked-host",
1285+
"example.com"
12711286
]
12721287
],
12731288
"csp": {
@@ -1278,7 +1293,7 @@ mod tests {
12781293
"violated_directive": "style-src cdn.example.com"
12791294
}
12801295
}
1281-
"#);
1296+
"###);
12821297
}
12831298

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

1340-
assert_annotated_snapshot!(Annotated::new(event), @r#"
1355+
assert_annotated_snapshot!(Annotated::new(event), @r###"
13411356
{
13421357
"culprit": "default-src self",
13431358
"logentry": {
@@ -1360,6 +1375,10 @@ mod tests {
13601375
[
13611376
"blocked-uri",
13621377
"http://evilhackerscripts.com"
1378+
],
1379+
[
1380+
"blocked-host",
1381+
"evilhackerscripts.com"
13631382
]
13641383
],
13651384
"csp": {
@@ -1371,7 +1390,7 @@ mod tests {
13711390
"violated_directive": "default-src self"
13721391
}
13731392
}
1374-
"#);
1393+
"###);
13751394
}
13761395

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

1399-
assert_annotated_snapshot!(Annotated::new(event), @r#"
1418+
assert_annotated_snapshot!(Annotated::new(event), @r###"
14001419
{
14011420
"culprit": "script-src",
14021421
"logentry": {
@@ -1419,6 +1438,10 @@ mod tests {
14191438
[
14201439
"blocked-uri",
14211440
"http://baddomain.com/test.js?_=1515535030116"
1441+
],
1442+
[
1443+
"blocked-host",
1444+
"baddomain.com"
14221445
]
14231446
],
14241447
"csp": {
@@ -1436,7 +1459,7 @@ mod tests {
14361459
"disposition": "enforce"
14371460
}
14381461
}
1439-
"#);
1462+
"###);
14401463
}
14411464

14421465
#[test]
@@ -1559,7 +1582,7 @@ mod tests {
15591582

15601583
let mut event = Event::default();
15611584
Csp::apply_to_event(json.as_bytes(), &mut event).unwrap();
1562-
insta::assert_debug_snapshot!(event.tags, @r#"
1585+
insta::assert_debug_snapshot!(event.tags, @r###"
15631586
Tags(
15641587
PairList(
15651588
[
@@ -1571,10 +1594,14 @@ mod tests {
15711594
"blocked-uri",
15721595
"https://api.stripe.com/v1/tokens",
15731596
),
1597+
TagEntry(
1598+
"blocked-host",
1599+
"api.stripe.com",
1600+
),
15741601
],
15751602
),
15761603
)
1577-
"#);
1604+
"###);
15781605
}
15791606

15801607
#[test]

tests/integration/fixtures/security_report/csp.no_processing.output.json

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
[
5959
"blocked-uri",
6060
"http://evilhackerscripts.com"
61+
],
62+
[
63+
"blocked-host",
64+
"evilhackerscripts.com"
6165
]
6266
],
6367
"user": {

tests/integration/fixtures/security_report/csp.normalized.output.json

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
[
5050
"blocked-uri",
5151
"http://evilhackerscripts.com"
52+
],
53+
[
54+
"blocked-host",
55+
"evilhackerscripts.com"
5256
]
5357
],
5458
"key_id": "123",

0 commit comments

Comments
 (0)