Ignores invalid query param part

This commit is contained in:
Uku Taht 2021-10-25 10:02:38 +02:00
parent 6dd9b0bb2f
commit 710f6f568a
2 changed files with 28 additions and 1 deletions

View File

@ -83,7 +83,7 @@ defmodule PlausibleWeb.Api.ExternalController do
else
uri = params["url"] && URI.parse(params["url"])
host = if uri && uri.host == "", do: "(none)", else: uri && uri.host
query = if uri && uri.query, do: URI.decode_query(uri.query), else: %{}
query = decode_query_params(uri)
ref = parse_referrer(uri, params["referrer"])
country_code = visitor_country(conn)
@ -347,4 +347,15 @@ defmodule PlausibleWeb.Api.ExternalController do
do: true
defp right_uri?(_), do: false
defp decode_query_params(nil), do: nil
defp decode_query_params(%URI{query: nil}), do: nil
defp decode_query_params(%URI{query: query_part}) do
try do
URI.decode_query(query_part)
rescue
_ -> nil
end
end
end

View File

@ -688,6 +688,22 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
assert pageview.utm_source == "%balle%"
end
test "ignores invalid query param part", %{conn: conn} do
params = %{
n: "pageview",
u:
"https://test.com/?utm_source=Bing%20%7C%20Text%20%7C%20Leads%20%7C%20EIGEN%20NAAM-most%20broad%20(Various%20search%20term%20matches)%20%7C%20Afweging,%20Consumptie%20%7C%20T%3A%",
d: "invalid-query-test.com"
}
conn = post(conn, "/api/event", params)
assert conn.status == 202
pageview = get_event("invalid-query-test.com")
assert pageview.utm_source == ""
end
test "can use double quotes in query params", %{conn: conn} do
q = URI.encode_query(%{"utm_source" => "Something \"quoted\""})