Skip to content

Commit

Permalink
fix: regenerate csrf if verification flow expired
Browse files Browse the repository at this point in the history
  • Loading branch information
splaunov committed Sep 2, 2022
1 parent aee2b1e commit 82de086
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion selfservice/flow/verification/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type (
errorx.ManagementProvider
x.WriterProvider
x.LoggingProvider
x.CSRFProvider
x.CSRFTokenGeneratorProvider
config.Provider
FlowPersistenceProvider
Expand Down Expand Up @@ -68,7 +69,7 @@ func (s *ErrorHandler) WriteFlowError(
if e := new(flow.ExpiredError); errors.As(err, &e) {
// create new flow because the old one is not valid
a, err := FromOldFlow(s.d.Config(r.Context()), s.d.Config(r.Context()).SelfServiceFlowVerificationRequestLifespan(),
s.d.GenerateCSRFToken(r), r, s.d.VerificationStrategies(r.Context()), f)
s.d.CSRFHandler().RegenerateToken(w, r), r, s.d.VerificationStrategies(r.Context()), f)
if err != nil {
// failed to create a new session and redirect to it, handle that error as a new one
s.WriteFlowError(w, r, f, group, err)
Expand Down
6 changes: 4 additions & 2 deletions selfservice/strategy/link/strategy_verification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func TestVerification(t *testing.T) {
assert.Equal(t, "The verification token is invalid or has already been used. Please retry the flow.", sr.Ui.Messages[0].Text)
})

t.Run("description=should not be able to use an outdated link", func(t *testing.T) {
t.Run("description=should not be able to request link with an outdated flow", func(t *testing.T) {
conf.MustSet(config.ViperKeySelfServiceVerificationRequestLifespan, time.Millisecond*200)
t.Cleanup(func() {
conf.MustSet(config.ViperKeySelfServiceVerificationRequestLifespan, time.Minute)
Expand All @@ -226,7 +226,7 @@ func TestVerification(t *testing.T) {
assert.Contains(t, res.Request.URL.String(), conf.SelfServiceFlowVerificationUI().String())
})

t.Run("description=should not be able to use an outdated flow", func(t *testing.T) {
t.Run("description=should not be able to use link with an outdated flow", func(t *testing.T) {
conf.MustSet(config.ViperKeySelfServiceVerificationRequestLifespan, time.Millisecond*200)
t.Cleanup(func() {
conf.MustSet(config.ViperKeySelfServiceVerificationRequestLifespan, time.Minute)
Expand All @@ -244,6 +244,8 @@ func TestVerification(t *testing.T) {

time.Sleep(time.Millisecond * 201)

//Clear cookies as link might be opened in another browser
c = testhelpers.NewClientWithCookies(t)
res, err := c.Get(verificationLink)
require.NoError(t, err)

Expand Down

0 comments on commit 82de086

Please sign in to comment.