towerops/lib/towerops_web/plugs/security_headers.ex
Graham McIntire a1e601563e fix: allow leaflet CDN and OSM tiles in CSP
The /sites-map page loads leaflet from unpkg.com (CSS + JS) and fetches
tiles from *.tile.openstreetmap.org. Update Content-Security-Policy to
permit those origins so the map can render.

Adds:
- script-src https://unpkg.com (leaflet.js)
- style-src https://unpkg.com (leaflet.css)
- connect-src https://*.tile.openstreetmap.org (tile fetches)
2026-05-01 17:03:14 -05:00

40 lines
1.3 KiB
Elixir

defmodule ToweropsWeb.Plugs.SecurityHeaders do
@moduledoc """
Adds security headers to all responses.
Headers added:
- Content-Security-Policy: Restricts resource loading to prevent XSS
- X-Frame-Options: Prevents clickjacking attacks
- X-Content-Type-Options: Prevents MIME type sniffing
- Referrer-Policy: Controls referrer information leakage
- Permissions-Policy: Disables browsing-topics API
"""
import Plug.Conn
def init(opts), do: opts
def call(conn, _opts) do
conn
|> put_resp_header("permissions-policy", "browsing-topics=()")
|> put_resp_header("content-security-policy", csp_header())
|> put_resp_header("x-frame-options", "DENY")
|> put_resp_header("x-content-type-options", "nosniff")
|> put_resp_header("referrer-policy", "strict-origin-when-cross-origin")
end
defp csp_header do
Enum.join(
[
"default-src 'self' data:",
"script-src 'self' 'unsafe-inline' https://a.w5isp.com https://unpkg.com",
"style-src 'self' 'unsafe-inline' 'unsafe-hashes' https://unpkg.com",
"img-src 'self' data: https:",
"font-src 'self' data:",
"connect-src 'self' ws: wss: https://a.w5isp.com https://*.tile.openstreetmap.org",
"frame-ancestors 'none'"
],
"; "
)
end
end