Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Add Content-Security-Policy to favicon Plug (#2489)
Vini Brasil9b32dace3e27
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 142d1bc02f..9c49508d2c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -74,6 +74,9 @@ All notable changes to this project will be documented in this file.- Do not display ZZ country code in countries report [PR#1934](https://github.com/plausible/analytics#2223)- Add fallback icon for when DDG favicon cannot be fetched [PR#2279](https://github.com/plausible/analytics#2279)+### Security+- Add Content-Security-Policy header to favicon path+## v1.4.1 - 2021-11-29### Fixeddiff --git a/lib/plausible_web/plugs/favicon.ex b/lib/plausible_web/plugs/favicon.exindex 057affd19d..3ce3bf60da 100644--- a/lib/plausible_web/plugs/favicon.ex+++ b/lib/plausible_web/plugs/favicon.ex@@ -42,21 +42,42 @@ defmodule PlausibleWeb.Favicon do@ddg_broken_icon <<137, 80, 78, 71, 13, 10, 26, 10>>@doc """- Proxies HTTP request to DuckDuckGo favicon service. Swallows hop-by-hop HTTP headers that- should not be forwarded as defined in RFC 2616 (https://www.rfc-editor.org/rfc/rfc2616#section-13.5.1)+ Proxies HTTP request to DuckDuckGo favicon service. Swallows hop-by-hop HTTP+ headers that should not be forwarded as defined in [RFC 2616](https://www.rfc-editor.org/rfc/rfc2616#section-13.5.1)++ ## PlaceholderCases where we show a placeholder icon instead:- * In case of network error to DuckDuckGo- * In case of non-2xx status code from DuckDuckGo- * In case of broken image response body from DuckDuckGo- I'm not sure why DDG sometimes returns a broken PNG image in their response but we filter that out.- When the icon request fails, we show a placeholder favicon instead. The placeholder is an emoji- from https://favicon.io/emoji-favicons/+ 1. In case of network error to DuckDuckGo+ 2. In case of non-2xx status code from DuckDuckGo+ 3. In case of broken image response body from DuckDuckGo++ I'm not sure why DDG sometimes returns a broken PNG image in their response+ but we filter that out. When the icon request fails, we show a placeholder+ favicon instead. The placeholder is an emoji from+ [https://favicon.io/emoji-favicons/](https://favicon.io/emoji-favicons/)++ DuckDuckGo favicon service has some issues with [SVG favicons](https://css-tricks.com/svg-favicons-and-all-the-fun-things-we-can-do-with-them/).+ For some reason, they return them with `content-type=image/x-icon` whereas SVG+ icons should be returned with `content-type=image/svg+xml`. This Plug detects+ when the response body starts with `<svg` and will override the `Content-Type`+ to correct it.++ ## Preventing XSS vulnerabilities++ SVGs may contain `<script>` tags, and as these SVGs come from external+ sources, we need to prevent untrusted code from running on the browser.++ - This Plug sets a strict `Content-Security-Policy` header telling the browser+ not to run scripts.++ - This Plug sets `Content-Disposition=attachment` to prevent the SVG from+ rendering when navigating to `/favicon/sources/:domain` directly.++ - Browsers do not execute scripts from `<img>` tags, therefore it is safe to+ use `<img src="https://plausible.io/favicon/sources/dummy.site"></img>`- DuckDuckGo favicon service has some issues with [svg favicons](https://css-tricks.com/svg-favicons-and-all-the-fun-things-we-can-do-with-them/).- For some reason, they return them with `content-type=image/x-icon` whereas SVG icons should be returned with `content-type=image/svg+xml`.- This plug detects when the response body starts with <svg and will override the content-type to correct it."""def call(conn, favicon_domains: favicon_domains) docase conn.path_info do@@ -73,8 +94,9 @@ defmodule PlausibleWeb.Favicon doconn|> forward_headers(headers)|> maybe_override_content_type(body)+ |> prevent_javascript_execution()|> send_resp(200, body)- |> halt+ |> halt()_ ->send_placeholder(conn)@@ -104,4 +126,10 @@ defmodule PlausibleWeb.Favicon doenddefp maybe_override_content_type(conn, _), do: conn++ defp prevent_javascript_execution(conn) do+ conn+ |> put_resp_header("content-security-policy", "script-src 'none'")+ |> put_resp_header("content-disposition", "attachment")+ endenddiff --git a/test/plausible_web/plugs/favicon_test.exs b/test/plausible_web/plugs/favicon_test.exsindex 513d45b4b9..1fa89a9f02 100644--- a/test/plausible_web/plugs/favicon_test.exs+++ b/test/plausible_web/plugs/favicon_test.exs@@ -38,6 +38,26 @@ defmodule PlausibleWeb.FaviconTest doassert conn.resp_body == "favicon response body"end+ test "sets content-disposition and content-security-policy", %{plug_opts: plug_opts} do+ expect(+ Plausible.HTTPClient.Mock,+ :get,+ fn "https://icons.duckduckgo.com/ip3/plausible.io.ico" ->+ {:ok, %Finch.Response{status: 200, body: "favicon response body"}}+ end+ )++ conn =+ conn(:get, "/favicon/sources/plausible.io")+ |> Favicon.call(plug_opts)++ assert conn.halted+ assert conn.status == 200+ assert conn.resp_body == "favicon response body"+ assert Plug.Conn.get_resp_header(conn, "content-security-policy") == ["script-src 'none'"]+ assert Plug.Conn.get_resp_header(conn, "content-disposition") == ["attachment"]+ end+test "maps a categorized source to URL for favicon", %{plug_opts: plug_opts} doexpect(Plausible.HTTPClient.Mock,@@ -79,10 +99,7 @@ defmodule PlausibleWeb.FaviconTest doassert conn.haltedassert conn.status == 200-- assert conn.resp_headers == [- {"content-type", "should-pass-through"}- ]+ assert Plug.Conn.get_resp_header(conn, "content-type") == ["should-pass-through"]endtest "overrides content-type header if proxied response starts with <svg", %{@@ -106,10 +123,7 @@ defmodule PlausibleWeb.FaviconTest do|> Favicon.call(plug_opts)assert conn.halted-- assert conn.resp_headers == [- {"content-type", "image/svg+xml; charset=utf-8"}- ]+ assert Plug.Conn.get_resp_header(conn, "content-type") == ["image/svg+xml; charset=utf-8"]enddescribe "Fallback to placeholder icon" do