From 34a6b984c7c8cc5366c23a01328eb114982225b6 Mon Sep 17 00:00:00 2001 From: Harry Vangberg Date: Fri, 19 May 2023 13:26:13 +0200 Subject: [PATCH] Escape domain when creating URL for favicon (#2942) * Escape domain when constructing favicon URL A domain may include a slash, and in that case the domain must be escaped, before it is used as an attribute for the image tag. * match with 'conn.request_path' instead + test --------- Co-authored-by: Robert Joonas --- CHANGELOG.md | 1 + lib/plausible_web/plugs/favicon.ex | 6 +++--- test/plausible_web/plugs/favicon_test.exs | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3b37ff6e..0f56d780f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ All notable changes to this project will be documented in this file. - Add error message in case a transfer to an invited (but not joined) user is requested plausible/analytics#2651 - Fix bug with [showing property breakdown with a prop filter](https://github.com/plausible/analytics/issues/1789) - Fix bug when combining goal and prop filters plausible/analytics#2654 +- Fix broken favicons when domain includes a slash ### Changed - Treat page filter as entry page filter for `bounce_rate` diff --git a/lib/plausible_web/plugs/favicon.ex b/lib/plausible_web/plugs/favicon.ex index 3ce3bf60d..e8dc9a5e2 100644 --- a/lib/plausible_web/plugs/favicon.ex +++ b/lib/plausible_web/plugs/favicon.ex @@ -80,11 +80,11 @@ defmodule PlausibleWeb.Favicon do """ def call(conn, favicon_domains: favicon_domains) do - case conn.path_info do - ["favicon", "sources", "placeholder"] -> + case conn.request_path do + "/favicon/sources/placeholder" -> send_placeholder(conn) - ["favicon", "sources", source] -> + "/favicon/sources/" <> source -> clean_source = URI.decode_www_form(source) domain = Map.get(favicon_domains, clean_source, clean_source) diff --git a/test/plausible_web/plugs/favicon_test.exs b/test/plausible_web/plugs/favicon_test.exs index a62e4232a..45f93d457 100644 --- a/test/plausible_web/plugs/favicon_test.exs +++ b/test/plausible_web/plugs/favicon_test.exs @@ -38,6 +38,24 @@ defmodule PlausibleWeb.FaviconTest do assert conn.resp_body == "favicon response body" end + test "requests favicon from DDG when domain contains a forward slash", %{plug_opts: plug_opts} do + expect( + Plausible.HTTPClient.Mock, + :get, + fn "https://icons.duckduckgo.com/ip3/site.com/subfolder.ico" -> + {:ok, %Finch.Response{status: 200, body: "favicon response body"}} + end + ) + + conn = + conn(:get, "/favicon/sources/site.com/subfolder") + |> Favicon.call(plug_opts) + + assert conn.halted + assert conn.status == 200 + 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,