Add Content-Security-Policy to favicon Plug (#2489)

pull/2490/head r2022.12.02-27
Vini Brasil 4 years ago committed by GitHub
parent fd6ebf7598
commit 9b32dace3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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

@ -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 `<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) do
case conn.path_info do
@ -73,8 +94,9 @@ defmodule PlausibleWeb.Favicon do
conn
|> 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

@ -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 <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"]
end
describe "Fallback to placeholder icon" do

Loading…
Cancel
Save