搭配內容安全政策使用代碼管理工具

內容安全政策 (CSP) 是廣泛支援的網路安全性標準,可讓開發人員控管應用程式載入的資源,進而防範特定類型的插入式攻擊。本指南將說明如何在使用 CSP 的網站上部署 Google 代碼管理工具。

如要使用 CSP,請啟用容器標記

如要在含有 CSP 的網頁中使用 Google 代碼管理工具,CSP 必須允許執行代碼管理工具容器程式碼。這個程式碼是以插入 gtm.js 指令碼的內嵌 JavaScript 程式碼建構。方法有很多種,例如使用 Nonce 或雜湊。建議的做法是使用 nonce,這是一個不可猜的隨機值,可供伺服器針對每個回應分別產生。在 Content-Security-政策 script-src 指令中提供 Nonce 值:

Content-Security-Policy: script-src 'nonce-{SERVER-GENERATED-NONCE}'; img-src www.googletagmanager.com

接著,使用內嵌代碼管理工具容器程式碼的 Nonce 感知版本。 將內嵌指令碼元素上的 Nonce 屬性設為相同的值:

<!-- Google Tag Manager -->
<script nonce='{SERVER-GENERATED-NONCE}'>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;var n=d.querySelector('[nonce]');
n&&j.setAttribute('nonce',n.nonce||n.getAttribute('nonce'));f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXX');</script>
<!-- End Google Tag Manager -->

接著,代碼管理工具會將 Nonce 套用到其加進網頁的所有指令碼。

還有其他方法可以啟用內嵌指令碼,例如在 CSP 中提供內嵌指令碼的雜湊

如果無法使用建議的 Nonce 或雜湊方法,可以將 'unsafe-inline' 指令加到 CSP 的 script-src 區段,以啟用代碼管理工具內嵌指令碼。

CSP 中必須具有下列指令,才能使用這個方法:

script-src: 'unsafe-inline' https://www.googletagmanager.com
img-src: www.googletagmanager.com

自訂 JavaScript 變數

基於自訂 JavaScript 變數的實作方式,這類變數在 CSP 出現時會先評估為 undefined,除非已在 CSP 的 script-src 區段中提供 'unsafe-eval' 指令。

script-src: 'unsafe-eval'

預覽模式

如要使用 Google 代碼管理工具的預覽模式,CSP 必須包含下列指令:

script-src: https://googletagmanager.com https://tagmanager.google.com
style-src: https://googletagmanager.com https://tagmanager.google.com https://fonts.googleapis.com
img-src: https://googletagmanager.com https://ssl.gstatic.com https://www.gstatic.com
font-src: https://fonts.gstatic.com data:

Google Analytics (分析) 4 (Google Analytics (分析))

如要使用 Google Analytics (分析) 4 (Google Analytics (分析)) 標記,CSP 必須包含下列指令:

script-src:  https://*.googletagmanager.com
img-src:     https://*.google-analytics.com https://*.googletagmanager.com
connect-src: https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com

針對使用 Google 信號的 Google Analytics (分析) 4 (Google Analytics (分析)) 部署,CSP 必須包含下列指令:

script-src:  https://*.googletagmanager.com
img-src:     https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com
             https://*.g.doubleclick.net https://*.google.com https://*.google.<TLD>
connect-src: https://*.google-analytics.com https://*.analytics.google.com
             https://*.googletagmanager.com https://*.g.doubleclick.net https://*.google.com https://*.google.<TLD>

通用 Analytics (分析) (Google Analytics (分析))

如要使用通用 Analytics (分析) (Google Analytics (分析)) 標記,CSP 必須包含下列指令:

script-src: https://www.google-analytics.com https://ssl.google-analytics.com
img-src: https://www.google-analytics.com
connect-src: https://www.google-analytics.com

如要使用 Google Ads 轉換標記,CSP 必須包含下列指令:

針對安全連線:

script-src: https://www.googleadservices.com https://www.google.com
img-src: https://googleads.g.doubleclick.net https://www.google.com https://google.com

不安全連線:

script-src: www.googleadservices.com www.google.com
img-src: googleads.g.doubleclick.net www.google.com google.com

如要使用 Google Ads 再行銷代碼,CSP 必須包含下列指令。

針對安全連線:

script-src: https://www.googleadservices.com https://googleads.g.doubleclick.net https://www.google.com
img-src: https://www.google.com https://google.com
frame-src: https://bid.g.doubleclick.net https://td.doubleclick.net

不安全連線:

script-src: www.googleadservices.com googleads.g.doubleclick.net www.google.com
img-src: www.google.com google.com
frame-src: bid.g.doubleclick.net td.doubleclick.net

泛光燈

Floodlight 使用者可透過下列設定啟用 CSP。將 <FLOODLIGHT-CONFIG-ID> 值替換為特定的 Floodlight 廣告主 ID,或是將 * 替換為任何廣告主 ID:

所有使用者:

img-src: https://<FLOODLIGHT-CONFIG-ID>.fls.doubleclick.net
frame-src: https://td.doubleclick.net

針對代碼管理工具中的「自訂指令碼」信標:

frame-src: https://<FLOODLIGHT-CONFIG-ID>.fls.doubleclick.net

圖片代碼:

img-src: https://ad.doubleclick.net

如果是同意聲明模式

img-src: https://ade.googlesyndication.com