Skip to content

Commit 64c6e2f

Browse files
Merge pull request #11 from silverstripeltd/feature/gtm-missing-directive-updates
Google Services fragment updates
2 parents d705f32 + 88902ee commit 64c6e2f

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public function configure(): void
8080
}
8181
```
8282

83+
## Google Tag Manager / Adservices whitelist
84+
Google uses localised regional domains for visitors for image tracker loading, which can pile up report violations with `google.com|.co.nz|.com.au` etc in your reporting tool.
85+
To resolve this and rather than specifying all of Google's listed support domains (see https://www.google.com/supported_domains)
86+
A white list config can be set to the GTM fragment to whitelist all `https:` URLs on the `img-src` directive, for example:
87+
```yaml
88+
Silverstripe\CSP\Fragments\GoogleTagManager:
89+
whitelist_google_regional_domains: true
90+
```
91+
> See also ImagesOverHTTPs::class for more basic cover of https images.
92+
8393
## SRI
8494
We also support SRI in this module, you can enable this via yaml:
8595
```yaml

src/Fragments/GoogleMaps.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\ContentSecurity\Fragments;
4+
5+
use Silverstripe\CSP\Directive;
6+
use Silverstripe\CSP\Fragments\Fragment;
7+
use Silverstripe\CSP\Policies\Policy;
8+
9+
/*
10+
* Allows execution of Google Maps API related resources
11+
* Nonce on the https://maps.google.com/maps/api/js URL is required before using this fragment.
12+
*
13+
* https://content-security-policy.com/examples/google-maps/
14+
*/
15+
class GoogleMaps implements Fragment
16+
{
17+
public static function addTo(Policy $policy): void
18+
{
19+
$policy
20+
->addDirective(Directive::CONNECT, 'https://maps.googleapis.com')
21+
->addDirective(Directive::IMG,
22+
[
23+
'https://maps.gstatic.com',
24+
'https://*.googleapis.com',
25+
'https://*.ggpht.com'
26+
]
27+
);
28+
}
29+
}

src/Fragments/GoogleTagManager.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Silverstripe\CSP\Fragments;
44

5+
use SilverStripe\Core\Config\Configurable;
56
use Silverstripe\CSP\Directive;
67
use Silverstripe\CSP\Keyword;
78
use Silverstripe\CSP\Policies\Policy;
@@ -12,6 +13,10 @@
1213
*/
1314
class GoogleTagManager implements Fragment
1415
{
16+
use Configurable;
17+
18+
private static bool $whitelist_google_regional_domains = false;
19+
1520
public static function addTo(Policy $policy): void
1621
{
1722
self::undocumented($policy);
@@ -25,14 +30,33 @@ public static function addTo(Policy $policy): void
2530
}
2631

2732
/*
28-
* These were ones not in the docs and had issues popping up
33+
* CSP reported directive URIs that were not covered in the google docs
34+
* and were continually over reporting CSP URI infringements.
35+
*
36+
* https://developers.google.com/web/fundamentals/security/csp#implementation_details
2937
*/
3038
public static function undocumented(Policy $policy): void
3139
{
3240
$policy
33-
->addDirective(Directive::FRAME, '*.doubleclick.net')
34-
->addDirective(Directive::CONNECT, '*.doubleclick.net')
35-
->addDirective(Directive::IMG, '*.doubleclick.net');
41+
->addDirective(Directive::FRAME,
42+
[
43+
'https://*.doubleclick.net',
44+
'https://stats.g.doubleclick.net',
45+
'http://bid.g.doubleclick.net',
46+
]
47+
)
48+
->addDirective(Directive::CONNECT, [
49+
'https://adservice.google.com',
50+
'https://www.google.com',
51+
'https://*.doubleclick.net',
52+
]);
53+
54+
// Google uses localised regional endpoint domains for their services
55+
// if seeing regional google domain report violations
56+
// setting this config will whitelist all img-src to allow 'https:'.
57+
if (self::config()->get('whitelist_google_regional_domains') === true) {
58+
$policy->addDirective(Directive::IMG, Scheme::HTTPS);
59+
}
3660
}
3761

3862
/*

0 commit comments

Comments
 (0)