Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Fix event name validation (#2975)
Uku Tahtfc918081b591
diff --git a/lib/plausible/ingestion/request.ex b/lib/plausible/ingestion/request.exindex ccf322571a..7028bf77eb 100644--- a/lib/plausible/ingestion/request.ex+++ b/lib/plausible/ingestion/request.ex@@ -1,3 +1,20 @@+defmodule Plausible.Ecto.EventName do+ @moduledoc """+ Custom type for event name. Accepts Strings and Integers and stores them as String. Returns+ cast error if any other type is provided. Accepting integers is important for 404 tracking.+ """++ use Ecto.Type+ def type, do: :string++ def cast(val) when is_binary(val), do: {:ok, val}+ def cast(val) when is_integer(val), do: {:ok, Integer.to_string(val)}++ def cast(_), do: :error+ def load(val), do: {:ok, val}+ def dump(val), do: {:ok, val}+end+defmodule Plausible.Ingestion.Request do@moduledoc """The %Plausible.Ingestion.Request{} struct stores all needed fields@@ -13,12 +30,12 @@ defmodule Plausible.Ingestion.Request doembedded_schema dofield :remote_ip, :stringfield :user_agent, :string- field :event_name, :string+ field :event_name, Plausible.Ecto.EventNamefield :uri, :mapfield :hostname, :stringfield :referrer, :stringfield :domains, {:array, :string}- field :hash_mode, :string+ field :hash_mode, :integerfield :pathname, :stringfield :props, :mapfield :query_params, :map@@ -88,10 +105,13 @@ defmodule Plausible.Ingestion.Request doenddefp put_request_params(changeset, %{} = request_body) do- Changeset.change(+ Changeset.cast(changeset,- event_name: request_body["n"] || request_body["name"],- hash_mode: request_body["h"] || request_body["hashMode"]+ %{+ event_name: request_body["n"] || request_body["name"],+ hash_mode: request_body["h"] || request_body["hashMode"]+ },+ [:event_name, :hash_mode])enddiff --git a/test/plausible/ingestion/request_test.exs b/test/plausible/ingestion/request_test.exsindex 7de51d1eeb..6d5cea5016 100644--- a/test/plausible/ingestion/request_test.exs+++ b/test/plausible/ingestion/request_test.exs@@ -100,6 +100,18 @@ defmodule Plausible.Ingestion.RequestTest doassert request.uri.host == "dummy.site"end+ test "request can be built with numeric event name" do+ payload = %{+ n: 404,+ d: "dummy.site",+ u: "http://dummy.site/index"+ }++ conn = build_conn(:post, "/api/events", payload)+ assert {:ok, request} = Request.build(conn)+ assert request.event_name == "404"+ end+test "hostname is (none) if host-less uri provided" dopayload = %{name: "pageview",@@ -237,6 +249,30 @@ defmodule Plausible.Ingestion.RequestTest doassert {"should be at most %{count} character(s)", _} = changeset.errors[:event_name]end+ test "returns validation error when event name is blank" do+ payload = %{+ name: nil,+ domain: "dummy.site",+ url: "https://dummy.site/"+ }++ conn = build_conn(:post, "/api/events", payload)+ assert {:error, changeset} = Request.build(conn)+ assert {"can't be blank", _} = changeset.errors[:event_name]+ end++ test "returns validation error when event name cannot be cast to string" do+ payload = %{+ name: ["list", "of", "things"],+ domain: "dummy.site",+ url: "https://dummy.site/"+ }++ conn = build_conn(:post, "/api/events", payload)+ assert {:error, changeset} = Request.build(conn)+ assert {"is invalid", _} = changeset.errors[:event_name]+ end+test "truncates referrer when too long" dopayload = %{name: "pageview",