[ Web Proxy ]
URL:
Viewing: https://developers.google.com/publisher-tag/guides/content-security-policy [Back]  [Original]

Integrate with a Content Security Policy  |  Google Publisher Tag  |  Google for Developers Skip to main content
Send feedback

Integrate with a Content Security Policy Stay organized with collections Save and categorize content based on your preferences.

outlined_flag

Content Security Policy (CSP) is a means of securing your web page by limiting what resources and scripts are allowed to load and execute. You can enable CSP by setting a Content-Security-Policy header in HTTP responses from your web server.

There are two standard ways to configure CSP:

  1. Specify an allowlist of domains that can inject their resources on the page.

  2. Specify a random nonce, with which resources on the page must be marked in order to load. This approach is known as strict CSP.

Because the domains that Google Publisher Tag (GPT) uses change over time, we only support strict CSP (option 2). This approach removes the need to maintain a rolling list of domains that might become outdated and break your site.

Setting up CSP with GPT

  1. Enable CSP on your web server.

    Follow the steps outlined in adopting strict CSP to set up the CSP header and apply the nonce to every script tag on your page, including gpt.js. GPT specifically supports the following CSP directives:

    Content-Security-Policy:
      object-src 'none';
      script-src 'nonce-{random}' 'unsafe-inline' 'unsafe-eval' 'strict-dynamic' https: http:;
      base-uri 'none';
      report-uri https://your-report-collector.example.com/
    

    You can choose a more permissive policy if it fits your use case. More restrictive policies may break without notice.

  2. Enable cross-domain rendering.

    Ad iframes can load external resources that might not be permitted by the CSP. Since same domain iframes inherit the top level window's CSP, and GPT cannot control the creatives contents, same-domain creatives will generally not work properly with CSP headers.

    To enable cross domain rendering for all creatives, execute googletag.setConfig({ safeFrame: { forceSafeFrame: true } }) before loading any ad slots.

    Note: Certain reservation creatives may depend on being rendered in a same domain iframe and fail to load with safe frame enabled globally. Check your inventory to see if this is the case; one indicator is the creative content checking for the existence of inDapIF or inGptIF variables.
    <!doctype html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Hello GPT</title>
        <script src="https://securepubads.g.doubleclick.net/tag/js/gpt.js" nonce="KC7tcz53FHqumKP1" async></script>
        <script nonce="KC7tcz53FHqumKP1">
          window.googletag = window.googletag || {cmd: []};
          googletag.cmd.push(function() {
            googletag.setConfig({ safeFrame: { forceSafeFrame: true } });
          });
        </script>
      </head>
    

Testing

We recommend that you test your policies first by setting the Content-Security-Policy-Report-Only header instead of Content-Security-Policy. The header reports violations but still allows them on the page.

Send feedback

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-31 UTC.

Need to tell us more? [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-07-31 UTC."],[],["Content Security Policy (CSP) secures web pages by controlling allowed resources. For Google Publisher Tag (GPT), only strict CSP using nonces is supported due to dynamic domain usage. Implementation involves: 1) Enabling CSP on the server by adding a `Content-Security-Policy` header, applying nonces to all script tags, including `gpt.js`, and defining directives like `script-src` and 2) Enabling cross-domain rendering for all ads using `googletag.pubads().setForceSafeFrame(true)`. Testing should use the `Content-Security-Policy-Report-Only` header.\n"]]

Web Proxy Viewer  |  New URL  |  Original Page