@@ -3,15 +3,18 @@ package api
33import (
44 "bytes"
55 "encoding/json"
6+ "fmt"
67 "net/http"
78 "net/http/httptest"
89 "testing"
10+ "time"
911
1012 "github.com/stretchr/testify/assert"
1113 "github.com/stretchr/testify/require"
1214 "github.com/stretchr/testify/suite"
1315 "github.com/supabase/auth/internal/api/apierrors"
1416 "github.com/supabase/auth/internal/conf"
17+ mail "github.com/supabase/auth/internal/mailer"
1518 "github.com/supabase/auth/internal/models"
1619)
1720
@@ -310,3 +313,121 @@ func (ts *OtpTestSuite) TestSubsequentOtp() {
310313 require .Empty (ts .T (), user .RecoverySentAt )
311314 require .Empty (ts .T (), user .EmailConfirmedAt )
312315}
316+
317+ func (ts * OtpTestSuite ) TestOtpClearsUnconfirmedPassword () {
318+ ts .Config .SMTP .MaxFrequency = 0
319+ ts .Config .Mailer .Autoconfirm = false
320+ defer func () { ts .Config .Mailer .Autoconfirm = false }()
321+
322+ userEmail := "victim@example.com"
323+ attackerPassword := "attacker-password-123"
324+
325+ // attacker signs up with the victim's email and leaves it unconfirmed
326+ var buffer bytes.Buffer
327+ require .NoError (ts .T (), json .NewEncoder (& buffer ).Encode (map [string ]interface {}{
328+ "email" : userEmail ,
329+ "password" : attackerPassword ,
330+ }))
331+ req := httptest .NewRequest (http .MethodPost , "/signup" , & buffer )
332+ req .Header .Set ("Content-Type" , "application/json" )
333+ w := httptest .NewRecorder ()
334+ ts .API .handler .ServeHTTP (w , req )
335+ require .Equal (ts .T (), http .StatusOK , w .Code )
336+
337+ user , err := models .FindUserByEmailAndAudience (ts .API .db , userEmail , ts .Config .JWT .Aud )
338+ require .NoError (ts .T (), err )
339+ require .False (ts .T (), user .IsConfirmed ())
340+ require .True (ts .T (), user .HasPassword ())
341+
342+ // email owner requests a magic link
343+ require .NoError (ts .T (), json .NewEncoder (& buffer ).Encode (map [string ]interface {}{
344+ "email" : userEmail ,
345+ }))
346+ req = httptest .NewRequest (http .MethodPost , "/otp" , & buffer )
347+ req .Header .Set ("Content-Type" , "application/json" )
348+ w = httptest .NewRecorder ()
349+ ts .API .handler .ServeHTTP (w , req )
350+ require .Equal (ts .T (), http .StatusOK , w .Code )
351+
352+ user , err = models .FindUserByEmailAndAudience (ts .API .db , userEmail , ts .Config .JWT .Aud )
353+ require .NoError (ts .T (), err )
354+ require .False (ts .T (), user .HasPassword ())
355+ require .NotEmpty (ts .T (), user .ConfirmationToken )
356+
357+ // email owner confirms through the link
358+ reqURL := fmt .Sprintf ("http://localhost/verify?type=%s&token=%s" , mail .SignupVerification , user .ConfirmationToken )
359+ req = httptest .NewRequest (http .MethodGet , reqURL , nil )
360+ w = httptest .NewRecorder ()
361+ ts .API .handler .ServeHTTP (w , req )
362+ require .Equal (ts .T (), http .StatusSeeOther , w .Code )
363+
364+ user , err = models .FindUserByEmailAndAudience (ts .API .db , userEmail , ts .Config .JWT .Aud )
365+ require .NoError (ts .T (), err )
366+ require .True (ts .T (), user .IsConfirmed ())
367+ require .False (ts .T (), user .HasPassword ())
368+
369+ // attacker can no longer sign in with their password
370+ require .NoError (ts .T (), json .NewEncoder (& buffer ).Encode (map [string ]interface {}{
371+ "email" : userEmail ,
372+ "password" : attackerPassword ,
373+ }))
374+ req = httptest .NewRequest (http .MethodPost , "/token?grant_type=password" , & buffer )
375+ req .Header .Set ("Content-Type" , "application/json" )
376+ w = httptest .NewRecorder ()
377+ ts .API .handler .ServeHTTP (w , req )
378+ require .Equal (ts .T (), http .StatusBadRequest , w .Code )
379+ }
380+
381+ func (ts * OtpTestSuite ) TestOtpClearsUnconfirmedPasswordWithAutoconfirm () {
382+ ts .Config .SMTP .MaxFrequency = 0
383+ ts .Config .Mailer .Autoconfirm = true
384+ defer func () { ts .Config .Mailer .Autoconfirm = false }()
385+
386+ userEmail := "victim@example.com"
387+
388+ // unconfirmed user created before autoconfirm was enabled
389+ u , err := models .NewUser ("" , userEmail , "attacker-password-123" , ts .Config .JWT .Aud , nil )
390+ require .NoError (ts .T (), err )
391+ require .NoError (ts .T (), ts .API .db .Create (u ))
392+ require .True (ts .T (), u .HasPassword ())
393+
394+ var buffer bytes.Buffer
395+ require .NoError (ts .T (), json .NewEncoder (& buffer ).Encode (map [string ]interface {}{
396+ "email" : userEmail ,
397+ }))
398+ req := httptest .NewRequest (http .MethodPost , "/otp" , & buffer )
399+ req .Header .Set ("Content-Type" , "application/json" )
400+ w := httptest .NewRecorder ()
401+ ts .API .handler .ServeHTTP (w , req )
402+ require .Equal (ts .T (), http .StatusOK , w .Code )
403+
404+ user , err := models .FindUserByEmailAndAudience (ts .API .db , userEmail , ts .Config .JWT .Aud )
405+ require .NoError (ts .T (), err )
406+ require .True (ts .T (), user .IsConfirmed ())
407+ require .False (ts .T (), user .HasPassword ())
408+ }
409+
410+ func (ts * OtpTestSuite ) TestOtpKeepsConfirmedPassword () {
411+ ts .Config .SMTP .MaxFrequency = 0
412+
413+ userEmail := "confirmed@example.com"
414+ u , err := models .NewUser ("" , userEmail , "password-123" , ts .Config .JWT .Aud , nil )
415+ require .NoError (ts .T (), err )
416+ now := time .Now ()
417+ u .EmailConfirmedAt = & now
418+ require .NoError (ts .T (), ts .API .db .Create (u ))
419+
420+ var buffer bytes.Buffer
421+ require .NoError (ts .T (), json .NewEncoder (& buffer ).Encode (map [string ]interface {}{
422+ "email" : userEmail ,
423+ }))
424+ req := httptest .NewRequest (http .MethodPost , "/otp" , & buffer )
425+ req .Header .Set ("Content-Type" , "application/json" )
426+ w := httptest .NewRecorder ()
427+ ts .API .handler .ServeHTTP (w , req )
428+ require .Equal (ts .T (), http .StatusOK , w .Code )
429+
430+ user , err := models .FindUserByEmailAndAudience (ts .API .db , userEmail , ts .Config .JWT .Aud )
431+ require .NoError (ts .T (), err )
432+ require .True (ts .T (), user .HasPassword ())
433+ }
0 commit comments