From 3417b2dd0eb9c247ad6baa0b208ea1804a0989bd Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 10 Mar 2023 20:32:10 +0100 Subject: [PATCH] Disallow site creation with insufficient input (#2741) --- lib/plausible/site.ex | 2 +- .../controllers/site_controller_test.exs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/plausible/site.ex b/lib/plausible/site.ex index ca466ce06..a87af1026 100644 --- a/lib/plausible/site.ex +++ b/lib/plausible/site.ex @@ -43,8 +43,8 @@ defmodule Plausible.Site do def changeset(site, attrs \\ %{}) do site |> cast(attrs, [:domain, :timezone]) - |> validate_required([:domain, :timezone]) |> clean_domain() + |> validate_required([:domain, :timezone]) |> validate_format(:domain, ~r/^[-\.\\\/:\p{L}\d]*$/u, message: "only letters, numbers, slashes and period allowed" ) diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs index 647b9df83..eee7f4765 100644 --- a/test/plausible_web/controllers/site_controller_test.exs +++ b/test/plausible_web/controllers/site_controller_test.exs @@ -108,6 +108,18 @@ defmodule PlausibleWeb.SiteControllerTest do assert Repo.get_by(Plausible.Site, domain: "example.com") end + test "fails to create the site if only http:// provided", %{conn: conn} do + conn = + post(conn, "/sites", %{ + "site" => %{ + "domain" => "http://", + "timezone" => "Europe/London" + } + }) + + assert html_response(conn, 200) =~ "can't be blank" + end + test "starts trial if user does not have trial yet", %{conn: conn, user: user} do Plausible.Auth.User.remove_trial_expiry(user) |> Repo.update!()