Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow newlines in email addresses #148

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/recognizer/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ defmodule Recognizer.Accounts.User do
defp validate_email(changeset) do
changeset
|> validate_required([:email])
|> validate_format(:email, ~r/^[^\s]+@[^\s]+\.[\w]+$/,
|> validate_format(:email, ~r/\A[^\s]+@[^\s]+\.[\w]+\z/,
message: "must have the @ sign, no spaces and a top level domain"
)
|> validate_length(:email, max: 160)
Expand Down
12 changes: 12 additions & 0 deletions test/recognizer/accounts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ defmodule Recognizer.AccountsTest do
} = errors_on(changeset)
end

test "validates email does not have a preceding newline" do
{:error, changeset} = Accounts.register_user(%{email: "\[email protected]"})

assert %{email: ["must have the @ sign, no spaces and a top level domain"]} = errors_on(changeset)
end

test "validates email does not have a trailing newline" do
{:error, changeset} = Accounts.register_user(%{email: "[email protected]\n"})

assert %{email: ["must have the @ sign, no spaces and a top level domain"]} = errors_on(changeset)
end

test "validates maximum values for email and password for security" do
too_long = String.duplicate("db", 100)
{:error, changeset} = Accounts.register_user(%{email: too_long, password: too_long})
Expand Down
Loading