From 9b32dace3e27ab63299d1d73dd0c6db557bf8870 Mon Sep 17 00:00:00 2001 From: Vini Brasil Date: Fri, 2 Dec 2022 06:33:24 -0300 Subject: [PATCH] Add Content-Security-Policy to favicon Plug (#2489) --- CHANGELOG.md | 3 ++ lib/plausible_web/plugs/favicon.ex | 52 +++++++++++++++++------ test/plausible_web/plugs/favicon_test.exs | 30 +++++++++---- 3 files changed, 65 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 142d1bc02..9c49508d2 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 ### Fixed diff --git a/lib/plausible_web/plugs/favicon.ex b/lib/plausible_web/plugs/favicon.ex index 057affd19..3ce3bf60d 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) + + ## Placeholder Cases 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 `` 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 `` tags, therefore it is safe to + use `` - 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 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 do end defp 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") + end end diff --git a/test/plausible_web/plugs/favicon_test.exs b/test/plausible_web/plugs/favicon_test.exs index 513d45b4b..1fa89a9f0 100644 --- a/test/plausible_web/plugs/favicon_test.exs +++ b/test/plausible_web/plugs/favicon_test.exs @@ -38,6 +38,26 @@ defmodule PlausibleWeb.FaviconTest do assert 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} do expect( Plausible.HTTPClient.Mock, @@ -79,10 +99,7 @@ defmodule PlausibleWeb.FaviconTest do assert conn.halted assert conn.status == 200 - - assert conn.resp_headers == [ - {"content-type", "should-pass-through"} - ] + assert Plug.Conn.get_resp_header(conn, "content-type") == ["should-pass-through"] end test "overrides content-type header if proxied response starts with 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"] end describe "Fallback to placeholder icon" do