From d71e40f16f573611b6b1bdba9fb3acefc5757214 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 18 Nov 2020 10:33:53 +0200 Subject: [PATCH 1/2] Validate domain format on site creation --- lib/plausible/site/schema.ex | 1 + .../controllers/site_controller_test.exs | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/plausible/site/schema.ex b/lib/plausible/site/schema.ex index b438cf4a1..8fe6582c7 100644 --- a/lib/plausible/site/schema.ex +++ b/lib/plausible/site/schema.ex @@ -22,6 +22,7 @@ defmodule Plausible.Site do site |> cast(attrs, [:domain, :timezone]) |> validate_required([:domain, :timezone]) + |> validate_format(:domain, ~r/^[a-zA-z0-9\-\.\/\:]*$/, message: "only letters, numbers, slashes and period allowed") |> unique_constraint(: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 8132cacf0..b3d674c35 100644 --- a/test/plausible_web/controllers/site_controller_test.exs +++ b/test/plausible_web/controllers/site_controller_test.exs @@ -52,6 +52,18 @@ defmodule PlausibleWeb.SiteControllerTest do assert html_response(conn, 200) =~ "can't be blank" end + test "only alphanumeric characters and slash allowed in domain", %{conn: conn} do + conn = + post(conn, "/sites", %{ + "site" => %{ + "timezone" => "Europe/London", + "domain" => "!@£.com" + } + }) + + assert html_response(conn, 200) =~ "only letters, numbers, slashes and period allowed" + end + test "renders form again when it is a duplicate domain", %{conn: conn} do insert(:site, domain: "example.com") From 1548b41ac3ee9becdf6e6e7ead9ceafba926443a Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Thu, 19 Nov 2020 10:53:43 +0200 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index be6311c77..5bbfb1d82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. - Ignore automated browsers (Phantom, Selenium, Headless Chrome, etc) - Display domain's favicon on the home page - Ignore consecutive pageviews on same pathname plausible/analytics#417 +- Validate domain format on site creation plausible/analytics#427 ### Fixed - Do not error when activating an already activated account plausible/analytics#370