Knowledge base / Network & CSP

Content-Security-Policy settings for Google Maps

A Content-Security-Policy tells the browser which hosts a page may load scripts, images and data from. If Google's hosts aren't on the list, the browser blocks the Maps requests itself. Google never sees them, so you get a CSP violation in the console instead of a Maps error code.

How to tell this is your problem

  • The console shows Refused to load or Refused to connect messages naming maps.googleapis.com, maps.gstatic.com or another Google host.
  • The map broke after a security review, a new security plugin, a hosting change or a CDN rule that adds headers.
  • Map controls and the Google logo render but tiles are grey, or markers are missing.
  • There is no Maps error code such as InvalidKeyMapError. The request never reached Google.

Typical messages in Chrome look like this:

Refused to load the script 'https://maps.googleapis.com/maps/api/js?key=…' because it violates the following Content Security Policy directive: "script-src 'self'".
Refused to load the image 'https://maps.gstatic.com/…' because it violates the following Content Security Policy directive: "img-src 'self' data:".
Refused to connect to 'https://maps.googleapis.com/…' because it violates the following Content Security Policy directive: "connect-src 'self'".

The directive named at the end of each message tells you which line of your policy to change.

Why it happens

The Maps JavaScript API isn't one file. The loader script fetches more scripts, images, fonts and data from several Google hosts, and it uses workers and blob: URLs internally. Each needs permission in a different directive. Miss one and you get a partly rendered map:

Missing fromWhat breaks
script-srcNothing loads. No map, no Maps error.
img-srcGrey tiles, missing markers and icons.
connect-srcMap data and service calls such as Places fail.
worker-srcParts of the map that run in a web worker fail.
font-src / style-srcControls and info windows fall back to other fonts.
frame-srcEmbedded google.com/maps/embed iframes don't load.

Policies can arrive from several places at once: a Content-Security-Policy response header from your server, CDN or security plugin, and a <meta http-equiv="Content-Security-Policy"> tag in the page. When there are several, the browser enforces all of them, so a request must pass every policy.

The policy Google recommends

Google's documentation recommends a strict, nonce-based policy. Your server generates a fresh random nonce on every response and adds it to your script and style tags. The API copies the nonce from the first such element on the page to the elements it adds itself. With 'strict-dynamic', a script with a valid nonce may load further scripts, so you don't need to list Google's script hosts at all:

Content-Security-Policy (strict, from Google's docs)
script-src 'nonce-{script value}' 'strict-dynamic' https: 'unsafe-eval' blob:;
img-src 'self' https://*.googleapis.com https://*.gstatic.com *.google.com *.googleusercontent.com data:;
frame-src *.google.com;
connect-src 'self' https://*.googleapis.com *.google.com https://*.gstatic.com data: blob:;
font-src https://fonts.gstatic.com;
style-src 'nonce-{style value}' https://fonts.googleapis.com;
worker-src blob:;

Browsers that understand 'strict-dynamic' ignore the https: source. It's only there as a fallback for older browsers.

If you can't use nonces

Some hosting set-ups and caching layers can't generate a nonce per response. Google also documents an allowlist policy, though it describes it as not recommended because 'unsafe-inline' and wide Google wildcards weaken protection against cross-site scripting:

Content-Security-Policy (allowlist)
script-src 'self' 'unsafe-inline' 'unsafe-eval' https://*.googleapis.com https://*.gstatic.com *.google.com https://*.ggpht.com *.googleusercontent.com blob:;
img-src 'self' https://*.googleapis.com https://*.gstatic.com *.google.com *.googleusercontent.com data:;
frame-src *.google.com;
connect-src 'self' https://*.googleapis.com *.google.com https://*.gstatic.com data: blob:;
font-src https://fonts.gstatic.com;
style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;
worker-src blob:;

Merge these sources into your existing directives. Don't paste them as a second header, or the stricter of the two policies still blocks Maps. If you load the API from a regional Google domain, add that domain to script-src too.

The fix, step by step

  1. 01Open the page with the console open and list every CSP violation. Note the blocked URL and the directive named in each.
  2. 02Find where the policy comes from. In the network tab, click the page's own document request and look for Content-Security-Policy in the response headers. Also search the page source for http-equiv="Content-Security-Policy".
  3. 03Find the setting that produces it: your server configuration, your host's or CDN's header rules, or a security plugin's settings.
  4. 04Add the Google sources above to each directive, merging with what's already there.
  5. 05Deploy the change as Content-Security-Policy-Report-Only first. The browser then logs violations without blocking anything, so you can see what's still missing on real pages.
  6. 06When the console is clean in report-only mode, switch the header back to Content-Security-Policy to enforce it.
Test every map feature

A policy can look fine on a plain map and still block something else. Test markers, info windows, Street View, address autocomplete, directions and any embedded iframe maps before you enforce.

WordPress and platform notes

On WordPress, CSP headers usually come from a security plugin, a headers plugin or your host's control panel. If a caching plugin serves the same HTML to everyone, a per-request nonce won't work with it, and the allowlist policy is the practical option. Hosted platforms such as Shopify, Wix and Squarespace manage most headers themselves, so a CSP problem there is more likely to come from an app or custom code. See map blocks on hosted platforms.

Confirm it is fixed

Reload the page in a private window with the console open. There should be no CSP violations, the Maps requests in the network tab should return successfully, and tiles should load at every zoom level. A scan records the console errors it sees and any Maps requests that fail at the network layer, so it will show if a violation remains on a page you didn't test by hand.

Our free CSP generator merges these sources into your existing policy for you.

Common questions

Why do I need 'unsafe-eval' for Google Maps?

Google includes 'unsafe-eval' in both of its recommended policies for the Maps JavaScript API. If your policy forbids it, test carefully, because parts of the map may fail.

Does the Maps Embed API iframe need the same policy?

No. An iframe only needs frame-src (or child-src) to allow google.com. Everything inside the frame runs under Google's own policy, not yours.

Can I list maps.googleapis.com only, without wildcards?

Maps loads resources from several Google hosts, and the set can change. Google's documented allowlist uses wildcards for that reason. Listing single hostnames tends to break when Google moves a resource.

Is a CSP problem the same for every visitor?

Yes. A CSP comes from your site, so it blocks the map for everyone. If only some visitors see a broken map, look at extensions and networks instead: see why the map works for you but not your customers.

Check whether this is your error

A scan tells you in about twelve seconds, from a clean session.

Related articles