towerops/lib/towerops_web/plugs/security_headers.ex
Graham McIntire 833de33622 fix: vendor Leaflet 1.9.4 locally instead of fetching from unpkg
unpkg.com fetches were intermittently failing in production, leaving
the /sites-map page without a working map. Vendoring Leaflet (JS, CSS,
marker/layer images) under priv/static/vendor/leaflet/ removes the
runtime CDN dependency, the matching CSP relaxation, and the SRI hash
churn on future updates.

Add 'vendor' to ToweropsWeb.static_paths/0 so Plug.Static serves the
new tree, and revert script-src/style-src to no longer list unpkg.com.
connect-src still allows *.tile.openstreetmap.org for tile fetches.
2026-05-01 17:06:54 -05:00

40 lines
1.2 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",
"style-src 'self' 'unsafe-inline' 'unsafe-hashes'",
"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