From 8ba851d27f6e479108b804d57301f71ac434f5eb Mon Sep 17 00:00:00 2001 From: hq1 Date: Tue, 12 Dec 2023 15:43:11 +0100 Subject: [PATCH] Protect against custom props payloads breaking event insertions (#3617) * Protect against custom props payloads breaking event insertions * Use two traversals instead of three * Reorganize invalid props filtering --- lib/plausible/clickhouse_event_v2.ex | 3 +-- lib/plausible/ingestion/event.ex | 7 ++++-- lib/plausible/ingestion/request.ex | 10 +++++++- .../api/external_controller_test.exs | 24 +++++++++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/lib/plausible/clickhouse_event_v2.ex b/lib/plausible/clickhouse_event_v2.ex index 727541aa0..7252f3410 100644 --- a/lib/plausible/clickhouse_event_v2.ex +++ b/lib/plausible/clickhouse_event_v2.ex @@ -78,8 +78,7 @@ defmodule Plausible.ClickhouseEventV2 do :revenue_source_currency, :revenue_reporting_amount, :revenue_reporting_currency - ], - empty_values: [nil, ""] + ] ) |> validate_required([:name, :site_id, :hostname, :pathname, :user_id, :timestamp]) end diff --git a/lib/plausible/ingestion/event.ex b/lib/plausible/ingestion/event.ex index fd74f7b43..950579acf 100644 --- a/lib/plausible/ingestion/event.ex +++ b/lib/plausible/ingestion/event.ex @@ -213,9 +213,12 @@ defmodule Plausible.Ingestion.Event do end defp put_props(%__MODULE__{request: %{props: %{} = props}} = event) do + # defensive: ensuring the keys/values are always in the same order + {keys, values} = Enum.unzip(props) + update_attrs(event, %{ - "meta.key": Map.keys(props), - "meta.value": Enum.map(props, fn {_, v} -> to_string(v) end) + "meta.key": keys, + "meta.value": values }) end diff --git a/lib/plausible/ingestion/request.ex b/lib/plausible/ingestion/request.ex index 15d122652..26689c56c 100644 --- a/lib/plausible/ingestion/request.ex +++ b/lib/plausible/ingestion/request.ex @@ -206,7 +206,7 @@ defmodule Plausible.Ingestion.Request do props = (request_body["m"] || request_body["meta"] || request_body["p"] || request_body["props"]) |> Plausible.Helpers.JSON.decode_or_fallback() - |> Enum.reject(fn {_k, v} -> is_nil(v) || is_list(v) || is_map(v) || v == "" end) + |> Enum.reduce([], &filter_bad_props/2) |> Enum.take(@max_props) |> Map.new() @@ -215,6 +215,14 @@ defmodule Plausible.Ingestion.Request do |> validate_props() end + defp filter_bad_props({k, v}, acc) do + cond do + Enum.any?([k, v], &(is_list(&1) or is_map(&1))) -> acc + Enum.any?([k, v], &(String.trim_leading(to_string(&1)) == "")) -> acc + true -> [{to_string(k), to_string(v)} | acc] + end + end + @max_prop_key_length Plausible.Props.max_prop_key_length() @max_prop_value_length Plausible.Props.max_prop_value_length() defp validate_props(changeset) do diff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exs index 33ac7c534..fdeb64612 100644 --- a/test/plausible_web/controllers/api/external_controller_test.exs +++ b/test/plausible_web/controllers/api/external_controller_test.exs @@ -566,6 +566,30 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do assert Map.get(event, :"meta.value") == ["true", "12"] end + test "filters out bad props", %{conn: conn, site: site} do + params = %{ + name: "Signup", + url: "http://gigride.live/", + domain: site.domain, + props: %{ + false: nil, + nil: false, + good: true, + " ": " ", + " ": "value", + key: " " + } + } + + conn + |> post("/api/event", params) + + event = get_event(site) + + assert Map.get(event, :"meta.key") == ["good"] + assert Map.get(event, :"meta.value") == ["true"] + end + test "ignores malformed custom props", %{conn: conn, site: site} do params = %{ name: "Signup",