Skip to content

Commit 1f44faf

Browse files
committed
security considerations section for docs
1 parent 4b2ed5b commit 1f44faf

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

lib/mix/tasks/phx.gen.auth.ex

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,32 @@ defmodule Mix.Tasks.Phx.Gen.Auth do
2626
to authentication views without necessarily triggering a new HTTP request
2727
each time (which would result in a full page load).
2828
29+
## Security considerations
30+
31+
By default, `mix phx.gen.auth` generates an authentication solution that allows registration
32+
using email and magic links only. Users must verify their email address after registration
33+
by clicking a magic link, which both logs them in and confirms their email.
34+
This email confirmation is crucial for preventing session fixation attacks.
35+
36+
If you allow users to immediately log in after registering, either by registering with a password,
37+
or by directly logging them in after only providing an email address, the following attack is possible:
38+
39+
1. An attacker registers a new account with the email address of their target, anticipating
40+
that the target creates an account at a later point in time.
41+
2. The attacker sets a password (either when registering, or in the settings).
42+
3. The target registers an account and sees that their email address is already in use.
43+
4. The target logs in by magic link, but does not change the existing password.
44+
5. The attacker maintains access using the password they previously set.
45+
46+
This is why the default implementation raises whenever a user tries to log in by magic link
47+
when there is already a password set. You can safely remove this check if you do not allow
48+
unconfirmed users to log in. In that case, registering with email and password is still secure.
49+
50+
If you want to allow unconfirmed users to log in, you need to ensure that they need to reset
51+
their password when they first sign in by magic link. To do this, you can add a required password
52+
field to the confirmation LiveView / template and re-use the password change functionality from
53+
the settings page.
54+
2955
## Password hashing
3056
3157
The password hashing mechanism defaults to `bcrypt` for

priv/templates/phx.gen.auth/context_functions.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@
213213
214214
3. The <%= schema.singular %> has not confirmed their email but a password is set.
215215
This cannot happen in the default implementation but may be the
216-
source of security pitfalls. See the "Mixing magic link and password
217-
registration" section of `mix help phx.gen.auth`.
216+
source of security pitfalls. See the "Security considerations" section of
217+
`mix help phx.gen.auth`.
218218
"""
219219
def login_<%= schema.singular %>_by_magic_link(token) do
220220
{:ok, query} = <%= inspect schema.alias %>Token.verify_magic_link_token_query(token)
@@ -227,7 +227,7 @@
227227
228228
This cannot happen with the default implementation, which indicates that you
229229
might have adapted the code to a different use case. Please make sure to read the
230-
"Mixing magic link and password registration" section of `mix help phx.gen.auth`.
230+
"Security considerations" section of `mix help phx.gen.auth`.
231231
"""
232232

233233
{%<%= inspect schema.alias %>{confirmed_at: nil} = <%= schema.singular %>, _token} ->

0 commit comments

Comments
 (0)