Leaflet and Stadia Maps Behind a Strict CSP

Two fixes that made the photo world map work in production: the tile host in img-src and a Referrer-Policy of origin on the tiles.

Article Created Updated Also in German: Leaflet und Stadia Maps hinter einer strikten CSP

Reviewed by adrian on

For the photo section I wanted a world map with every place a photo was taken, from the EXIF coordinates. The parts were unexciting: Leaflet with leaflet.markercluster, tiles from Stadia Maps (why Stadia), markers from the sidecar JSON files next to each photo.

Locally it worked on the first try. In production I got a grey rectangle with markers floating on top. Two separate problems, both caused by the security headers in Caddy.

The setup

  • Site: a static Astro build behind Caddy (April 2026).
  • Map: Leaflet and leaflet.markercluster, started by a client script on page load.
  • Tiles: tiles-eu.stadiamaps.com, style alidade_smooth.

Problem 1: img-src 'self' blocks the tiles

The CSP in Caddy was deliberately strict:

Content-Security-Policy "default-src 'none'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self'; font-src 'self'; connect-src 'self'; ..."

Leaflet loads tiles as plain <img> elements, so img-src 'self' data: blocks every tile that does not come from my own domain. The fix is the Stadia host in img-src:

- img-src 'self' data:;
+ img-src 'self' data: https://tiles-eu.stadiamaps.com;

connect-src stays as it is: Leaflet uses <img> tags for tiles, not fetch or XHR. I rolled it out with a backup, a check and a reload:

sudo cp /etc/caddy/sites/adrian-altner.de.caddy \
        /etc/caddy/sites/adrian-altner.de.caddy.pre-csp-tiles
sudo vim /etc/caddy/sites/adrian-altner.de.caddy   # patch the img-src line
sudo caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile
sudo systemctl reload caddy

caddy validate parses the whole configuration without touching the running service, so a typo fails here and not during the reload.

Problem 2: Referrer-Policy no-referrer gives 401

After the reload the map loaded, but every tile showed a QR code and "401 Error · Invalid Authentication". Stadia Maps authenticates by domain and reads the Referer header for it. My global Referrer-Policy "no-referrer" removed exactly that header.

I did not want to weaken the global policy, so the exception goes on the tile images only. The referrerpolicy attribute on an <img> overrides the document's policy, and Leaflet's TileLayer sets it since version 1.9:

L.tileLayer(
    'https://tiles-eu.stadiamaps.com/tiles/alidade_smooth/{z}/{x}/{y}{r}.png',
    {
        attribution: '…',
        maxZoom: 20,
        referrerPolicy: 'origin',
    },
).addTo(map);

'origin' sends only https://adrian-altner.de, without path or query. That is enough for Stadia to check the domain.

Takeaways

  • A third-party map needs its host in img-src. WebGL libraries such as MapLibre load tiles with fetch and need the host in connect-src; their web worker needs worker-src (blob: when the library comes from a CDN).1
  • A global no-referrer silently breaks every service that checks the domain by Referer (Stadia Maps, Mapbox, Google Maps). An override on the element is the clean middle ground.
  • Stadia Maps and many others let localhost through without authentication, so this bug only shows in production. Test anything CSP-related against the real server.

Based on

More on this

Topic

  • Web Development How this site and others are made and found: static pages, search engines, the IndieWeb and maps in the browser.

Builds on

  • Security Headers in Caddy A strict Content Security Policy, HSTS and no-referrer in a Caddy site block, checked with Webbkoll, securityheaders.com and the Mozilla Observatory.

Uses

  • Caddy Web server and reverse proxy that obtains and renews TLS certificates on its own, configured in a short Caddyfile.
  • Leaflet Small JavaScript library for interactive maps in the browser, with image tiles, markers and plugins such as marker clustering.
  • Stadia Maps Map tile service built on OpenStreetMap data, with an EU endpoint and authentication by domain instead of an API key.

Mentioned

Show in the graph view