From d104abb53de6273344e6bde9ce878f2faf9d67a4 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 28 Sep 2022 14:55:46 +0300 Subject: [PATCH] Add fallback for favicon (#2279) * Add fallback for favicon * Add Favicon tests * Changelog * Move placeholder icon to priv folder --- CHANGELOG.md | 1 + lib/plausible_web/plugs/favicon.ex | 75 ++++++++++++++-------- priv/placeholder_favicon.ico | Bin 0 -> 15406 bytes test/plausible_web/plugs/favicon_test.exs | 61 ++++++++++++++++++ 4 files changed, 110 insertions(+), 27 deletions(-) create mode 100644 priv/placeholder_favicon.ico diff --git a/CHANGELOG.md b/CHANGELOG.md index 144394dc1..6ebf84140 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ All notable changes to this project will be documented in this file. - `DISABLE_REGISTRATIONS` configuration parameter can now accept `invite_only` to allow invited users to register an account while keeping regular registrations disabled plausible/analytics#1841 - New and improved Session tracking module for higher throughput and lower latency. [PR#1934](https://github.com/plausible/analytics#1934) - 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) ## v1.4.1 diff --git a/lib/plausible_web/plugs/favicon.ex b/lib/plausible_web/plugs/favicon.ex index 02bce94b0..63ab9251c 100644 --- a/lib/plausible_web/plugs/favicon.ex +++ b/lib/plausible_web/plugs/favicon.ex @@ -1,34 +1,37 @@ defmodule PlausibleWeb.Favicon do @referer_domains_file "priv/referer_favicon_domains.json" @moduledoc """ - A Plug that fetches favicon images from DuckDuckGo and returns them - to the Plausible frontend. + A Plug that fetches favicon images from DuckDuckGo and returns them + to the Plausible frontend. - The proxying is there so we can reduce the number of third-party domains that - the browser clients need to connect to. Our goal is to have 0 third-party domain - connections on the website for privacy reasons. + The proxying is there so we can reduce the number of third-party domains that + the browser clients need to connect to. Our goal is to have 0 third-party domain + connections on the website for privacy reasons. - This module also maps between categorized sources and their respective URLs for favicons. - What does that mean exactly? During ingestion we use `PlausibleWeb.RefInspector.parse/1` to - categorize our referrer sources like so: + This module also maps between categorized sources and their respective URLs for favicons. + What does that mean exactly? During ingestion we use `PlausibleWeb.RefInspector.parse/1` to + categorize our referrer sources like so: - google.com -> Google - google.co.uk -> Google - google.com.au -> Google + google.com -> Google + google.co.uk -> Google + google.com.au -> Google - So when we show Google as a source in the dashboard, the request to this plug will come as: - https://plausible/io/favicon/sources/Google + So when we show Google as a source in the dashboard, the request to this plug will come as: + https://plausible/io/favicon/sources/Google - Now, when we want to show a favicon for Google, we need to convert Google -> google.com or - some other hostname owned by Google: - https://icons.duckduckgo.com/ip3/google.com.ico + Now, when we want to show a favicon for Google, we need to convert Google -> google.com or + some other hostname owned by Google: + https://icons.duckduckgo.com/ip3/google.com.ico - The mapping from source category -> source hostname is stored in "#{@referer_domains_file}" and - managed by `Mix.Tasks.GenerateReferrerFavicons.run/1` + The mapping from source category -> source hostname is stored in "#{@referer_domains_file}" and + managed by `Mix.Tasks.GenerateReferrerFavicons.run/1` """ import Plug.Conn alias Plausible.HTTPClient + @placeholder_icon_location "priv/placeholder_favicon.ico" + @placeholder_icon File.read!(@placeholder_icon_location) + def init(_) do domains = File.read!(Application.app_dir(:plausible, @referer_domains_file)) @@ -37,9 +40,19 @@ defmodule PlausibleWeb.Favicon do [favicon_domains: domains] end + @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) + + 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/ """ def call(conn, favicon_domains: favicon_domains) do case conn.path_info do @@ -48,14 +61,14 @@ defmodule PlausibleWeb.Favicon do domain = Map.get(favicon_domains, clean_source, clean_source) case HTTPClient.impl().get("https://icons.duckduckgo.com/ip3/#{domain}.ico") do - {:ok, res} -> + {:ok, %Finch.Response{body: body, headers: headers}} when body != @ddg_broken_icon -> conn - |> forward_headers(res) - |> send_resp(200, res.body) + |> forward_headers(headers) + |> send_resp(200, body) |> halt _ -> - send_resp(conn, 503, "") |> halt + send_placeholder(conn) end _ -> @@ -63,9 +76,17 @@ defmodule PlausibleWeb.Favicon do end end + defp send_placeholder(conn) do + conn + |> put_resp_content_type("image/x-icon") + |> put_resp_header("cache-control", "public, max-age=2592000") + |> send_resp(200, @placeholder_icon) + |> halt + end + @forwarded_headers ["content-type", "cache-control", "expires"] - defp forward_headers(conn, response) do - headers = Enum.filter(response.headers, fn {k, _} -> k in @forwarded_headers end) - %Plug.Conn{conn | resp_headers: headers} + defp forward_headers(conn, headers) do + headers_to_forward = Enum.filter(headers, fn {k, _} -> k in @forwarded_headers end) + %Plug.Conn{conn | resp_headers: headers_to_forward} end end diff --git a/priv/placeholder_favicon.ico b/priv/placeholder_favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..35aa65d28acb14ed2f1b2b2467d74e244b00bc3d GIT binary patch literal 15406 zcmeHOeUKbQ6`zwRpJE~e#4k`3_yZ-Dh$5Eqp(s=lRG^f?67ho%HGonT(Lcgc+-<+! zOD?*_-S+Ff_U@nx ze^pmEJKNpwqu+b|`gO184S0Ka3m1CcfOnX8@dD2~&hxy(4hudXwaD|uOWor5yEuKpMoFy z{>a`@yDZc-l*4{MnHrNvS}eYoQ2fEY({P(V(s>Mn@K0`Q*T~mLH2iXLu|fE~;r9r) zbQ*&Y{>J?$DuPwjz~tocL8Ra_21^%`8a}@9MFNCk(9uWuaYVd3j3Wf=Vx}0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@c`o6N~cSY&YTGW{Ys2L9kZC z`+FTX3zGAmq^!_zed%kr*46RQgg4Hhpo2wNLt#PpGP-fcBpKc(Q9LZA84 z#6)2)gD{d2|9JiCn$MVP2M@IZ_Jb-vZalM1srd}wKby8|F>{V&e9p*@i7Wk5klfztuaR5 zw#4gbkPD>S5}%iFT;lUtK09j4Ahr|A^Rsy8wabi&Me7mwVS9IV)h6Ff9q<{Sh86wJ zLt8%6LMzy3u?)OUaL&PZmi&>zYqR)gTQVB1;eC*McJ-J}^B`CC;}0&M>69CDY~_7c zp|F7O@R1r?#{0P}{`oAOQ1V^7ZGj9t53-{zv5fu1C70j3yfWREqJ0+6HQPoO@4ROt zl%-g#eYmFN+4E&fp$zH!ERof0HEyBa3hCagjb^g%7me}X} z^#`jQEabOU^(ZQyq563(;pq(kI#e%VRaU77K8IP=B=A8k%AS`-Hw@{xkpL2 zB@?{AlMobXSVcUPS10y;91VQ``ydkQZDaF z)!~x~oy{lsZ2xRgKEUVx?B99x#5ofwSe;SV+~ALoWUiIHfb~EXz5^$IKnM>I!eil{ zaf{nmD7f1qd<&uVUT+@oy$;@Yw$35zMm|0@%QbtBnP#nct3i3vlrAncs&qQv9pD-z z$CjDq0;G!3o%zNb^PM46g){T$ATKPRHC2iy_%S{6`f94B)A|0H%Z+D{-`A^fX1o4j zx>hMJ&&yjLFmKGGMR+E(pT|hI#Mv3;Ge!@!-sOvYOP5yU;j0$}>*(y}#>x8$5&tzM zF7Ac5yvVTz<<4hrz@bA(g})Dp^d77s&vSh&FHgN9Z$85@GHYxXKlyhysq3V;*RPDs z>D_nshmQG-yITXR)9$`Gy*u7hbD>>UOW^e5#*i34E z;_fw9hL4^t9=oA?KGTfraT`y2cCk%b#qZ8Y+ktlT({3_;&3`^|f13RkbF%F791 zdoF%sGKFi(m3e;SHrL?%oj3 + res = %Finch.Response{status: 503, body: "bad gateway"} + {:error, Plausible.HTTPClient.Non200Error.new(res)} + end + ) + + conn = + conn(:get, "/favicon/sources/plausible.io") + |> Favicon.call(plug_opts) + + assert conn.halted + assert conn.status == 200 + assert conn.resp_body == @placholder_icon + end + + test "falls back to placeholder in case of a network error", %{plug_opts: plug_opts} do + expect( + Plausible.HTTPClient.Mock, + :get, + fn "https://icons.duckduckgo.com/ip3/plausible.io.ico" -> + {:error, %Mint.TransportError{reason: :closed}} + end + ) + + conn = + conn(:get, "/favicon/sources/plausible.io") + |> Favicon.call(plug_opts) + + assert conn.halted + assert conn.status == 200 + assert conn.resp_body == @placholder_icon + end + + test "falls back to placeholder when DDG returns a broken image response", %{ + 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: <<137, 80, 78, 71, 13, 10, 26, 10>>}} + end + ) + + conn = + conn(:get, "/favicon/sources/plausible.io") + |> Favicon.call(plug_opts) + + assert conn.halted + assert conn.status == 200 + assert conn.resp_body == @placholder_icon + end + end end