Skip to content

Commit 5e2b7e7

Browse files
Dynamically add sentry and logrocket domains to CSP whitelist (#156)
Co-authored-by: Benjamin Rosenberger <[email protected]>
1 parent 2549c84 commit 5e2b7e7

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace JustBetter\Sentry\Model\Collector;
4+
5+
use JustBetter\Sentry\Helper\Data as DataHelper;
6+
use Magento\Csp\Api\PolicyCollectorInterface;
7+
use Magento\Csp\Model\Policy\FetchPolicy;
8+
9+
class SentryRelatedCspCollector implements PolicyCollectorInterface
10+
{
11+
/**
12+
* @param DataHelper $dataHelper
13+
*/
14+
public function __construct(
15+
private DataHelper $dataHelper
16+
) {
17+
}
18+
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function collect(array $defaultPolicies = []): array
23+
{
24+
$policies = $defaultPolicies;
25+
if (!$this->dataHelper->isActive()) {
26+
return $policies;
27+
}
28+
29+
if ($this->dataHelper->useScriptTag() || $this->dataHelper->useSessionReplay()) {
30+
$policies[] = new FetchPolicy(
31+
'script-src',
32+
false,
33+
['https://browser.sentry-cdn.com']
34+
);
35+
$policies[] = new FetchPolicy(
36+
'connect-src',
37+
false,
38+
['https://*.ingest.sentry.io']
39+
);
40+
}
41+
42+
if ($this->dataHelper->useLogrocket()) {
43+
$policies[] = new FetchPolicy(
44+
'script-src',
45+
false,
46+
['https://cdn.lr-ingest.io']
47+
);
48+
}
49+
50+
return $policies;
51+
}
52+
}

etc/csp_whitelist.xml

-15
This file was deleted.

etc/di.xml

+9
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@
3434
<plugin name="LogrocketCustomerInfo" type="JustBetter\Sentry\Plugin\LogrocketCustomerInfo"
3535
sortOrder="10" disabled="false"/>
3636
</type>
37+
38+
<type name="Magento\Csp\Model\CompositePolicyCollector">
39+
<arguments>
40+
<argument name="collectors" xsi:type="array">
41+
<item name="sentry" xsi:type="object" sortOrder="99">JustBetter\Sentry\Model\Collector\SentryRelatedCspCollector\Proxy</item>
42+
</argument>
43+
</arguments>
44+
</type>
45+
3746
</config>

0 commit comments

Comments
 (0)