Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Improve domain cleanup on site creation (#3393)

Adrian Gruntkowskid608fec903bd
diff --git a/lib/plausible/site.ex b/lib/plausible/site.ex
index 4195947a84..f80ec958f4 100644
--- a/lib/plausible/site.ex
+++ b/lib/plausible/site.ex
@@ -221,12 +221,12 @@ defmodule Plausible.Site do
defp clean_domain(changeset) do
clean_domain =
(get_field(changeset, :domain) || "")
+ |> String.downcase()
|> String.trim()
|> String.replace_leading("http://", "")
|> String.replace_leading("https://", "")
+ |> String.trim("/")
|> String.replace_leading("www.", "")
- |> String.replace_trailing("/", "")
- |> String.downcase()
change(changeset, %{domain: clean_domain})
end
diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs
index 1a689b983c..da09960890 100644
--- a/test/plausible_web/controllers/site_controller_test.exs
+++ b/test/plausible_web/controllers/site_controller_test.exs
@@ -235,17 +235,19 @@ defmodule PlausibleWeb.SiteControllerTest do
assert Repo.get_by(Plausible.Site, domain: "example.com")
end
- test "cleans up the url", %{conn: conn} do
- conn =
- post(conn, "/sites", %{
- "site" => %{
- "domain" => "https://www.Example.com/",
- "timezone" => "Europe/London"
- }
- })
-
- assert redirected_to(conn) == "/example.com/snippet"
- assert Repo.get_by(Plausible.Site, domain: "example.com")
+ for url <- ["https://Example.com/", "HTTPS://EXAMPLE.COM/", "/Example.com/", "//Example.com/"] do
+ test "cleans up an url like #{url}", %{conn: conn} do
+ conn =
+ post(conn, "/sites", %{
+ "site" => %{
+ "domain" => unquote(url),
+ "timezone" => "Europe/London"
+ }
+ })
+
+ assert redirected_to(conn) == "/example.com/snippet"
+ assert Repo.get_by(Plausible.Site, domain: "example.com")
+ end
end
test "renders form again when domain is missing", %{conn: conn} do