snapshot of https://github.com/ory/kratos.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
This commit is contained in:
212
selfservice/flow/recovery/flow_test.go
Normal file
212
selfservice/flow/recovery/flow_test.go
Normal file
@@ -0,0 +1,212 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package recovery_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tidwall/gjson"
|
||||
|
||||
"github.com/ory/kratos/pkg"
|
||||
"github.com/ory/kratos/selfservice/flow"
|
||||
"github.com/ory/kratos/selfservice/flow/recovery"
|
||||
"github.com/ory/kratos/selfservice/strategy/code"
|
||||
"github.com/ory/kratos/selfservice/strategy/link"
|
||||
"github.com/ory/kratos/x"
|
||||
"github.com/ory/x/clock"
|
||||
"github.com/ory/x/jsonx"
|
||||
"github.com/ory/x/urlx"
|
||||
)
|
||||
|
||||
func TestFlow(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
conf, reg := pkg.NewVeryFastRegistryWithoutDB(t)
|
||||
|
||||
must := func(r *recovery.Flow, err error) *recovery.Flow {
|
||||
require.NoError(t, err)
|
||||
return r
|
||||
}
|
||||
|
||||
u := &http.Request{URL: urlx.ParseOrPanic("http://foo/bar/baz"), Host: "foo"}
|
||||
for k, tc := range []struct {
|
||||
r *recovery.Flow
|
||||
expectErr bool
|
||||
}{
|
||||
{r: must(recovery.NewFlow(reg, time.Hour, "", u, nil, flow.TypeBrowser))},
|
||||
{r: must(recovery.NewFlow(reg, -time.Hour, "", u, nil, flow.TypeBrowser)), expectErr: true},
|
||||
} {
|
||||
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
|
||||
err := tc.r.Valid(clock.New())
|
||||
if tc.expectErr {
|
||||
require.Error(t, err)
|
||||
return
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
assert.EqualValues(t, flow.StateChooseMethod,
|
||||
must(recovery.NewFlow(reg, time.Hour, "", u, nil, flow.TypeBrowser)).State)
|
||||
|
||||
t.Run("type=return_to", func(t *testing.T) {
|
||||
_, err := recovery.NewFlow(reg, 0, "csrf", &http.Request{URL: &url.URL{Path: "/", RawQuery: "return_to=https://not-allowed/foobar"}, Host: "ory.sh"}, nil, flow.TypeBrowser)
|
||||
require.Error(t, err)
|
||||
|
||||
_, err = recovery.NewFlow(reg, 0, "csrf", &http.Request{URL: &url.URL{Path: "/", RawQuery: "return_to=" + urlx.AppendPaths(conf.SelfPublicURL(ctx), "/self-service/login/browser").String()}, Host: "ory.sh"}, nil, flow.TypeBrowser)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetType(t *testing.T) {
|
||||
for _, ft := range []flow.Type{
|
||||
flow.TypeAPI,
|
||||
flow.TypeBrowser,
|
||||
} {
|
||||
t.Run(fmt.Sprintf("case=%s", ft), func(t *testing.T) {
|
||||
r := &recovery.Flow{Type: ft}
|
||||
assert.Equal(t, ft, r.GetType())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRequestURL(t *testing.T) {
|
||||
expectedURL := "http://foo/bar/baz"
|
||||
f := &recovery.Flow{RequestURL: expectedURL}
|
||||
assert.Equal(t, expectedURL, f.GetRequestURL())
|
||||
}
|
||||
|
||||
func TestNewFlow_capturesCourierBaseURL(t *testing.T) {
|
||||
_, reg := pkg.NewVeryFastRegistryWithoutDB(t)
|
||||
|
||||
t.Run("nothing in context", func(t *testing.T) {
|
||||
r := &http.Request{URL: urlx.ParseOrPanic("http://foo/bar"), Host: "foo"}
|
||||
f, err := recovery.NewFlow(reg, time.Hour, "", r, nil, flow.TypeBrowser)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "", f.GetCourierBaseURL())
|
||||
})
|
||||
|
||||
t.Run("BaseURL in request context is copied onto flow", func(t *testing.T) {
|
||||
base := urlx.ParseOrPanic("https://customer.example.com/")
|
||||
r := (&http.Request{URL: urlx.ParseOrPanic("http://foo/bar"), Host: "foo"}).
|
||||
WithContext(x.WithBaseURL(context.Background(), base))
|
||||
f, err := recovery.NewFlow(reg, time.Hour, "", r, nil, flow.TypeBrowser)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, base.String(), f.GetCourierBaseURL())
|
||||
})
|
||||
|
||||
t.Run("http scheme is preserved (not coerced to https)", func(t *testing.T) {
|
||||
base := urlx.ParseOrPanic("http://localhost:4000")
|
||||
r := (&http.Request{URL: urlx.ParseOrPanic("http://foo/bar"), Host: "foo"}).
|
||||
WithContext(x.WithBaseURL(context.Background(), base))
|
||||
f, err := recovery.NewFlow(reg, time.Hour, "", r, nil, flow.TypeBrowser)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "http://localhost:4000", f.GetCourierBaseURL())
|
||||
})
|
||||
}
|
||||
|
||||
func TestFlowEncodeJSON(t *testing.T) {
|
||||
assert.EqualValues(t, "", gjson.Get(jsonx.TestMarshalJSONString(t, &recovery.Flow{RequestURL: "https://foo.bar?foo=bar"}), "return_to").String())
|
||||
assert.EqualValues(t, "/bar", gjson.Get(jsonx.TestMarshalJSONString(t, &recovery.Flow{RequestURL: "https://foo.bar?return_to=/bar"}), "return_to").String())
|
||||
assert.EqualValues(t, "/bar", gjson.Get(jsonx.TestMarshalJSONString(t, recovery.Flow{RequestURL: "https://foo.bar?return_to=/bar"}), "return_to").String())
|
||||
}
|
||||
|
||||
func TestFromOldFlow(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
conf, reg := pkg.NewVeryFastRegistryWithoutDB(t)
|
||||
r := http.Request{URL: &url.URL{Path: "/", RawQuery: "return_to=" + urlx.AppendPaths(conf.SelfPublicURL(ctx), "/self-service/login/browser").String()}, Host: "ory.sh"}
|
||||
t.Run("strategy=code", func(t *testing.T) {
|
||||
for _, ft := range []flow.Type{
|
||||
flow.TypeAPI,
|
||||
flow.TypeBrowser,
|
||||
} {
|
||||
t.Run(fmt.Sprintf("case=original flow is %s", ft), func(t *testing.T) {
|
||||
f, err := recovery.NewFlow(reg, 0, "csrf", &r, recovery.Strategies{code.NewStrategy(reg)}, ft)
|
||||
require.NoError(t, err)
|
||||
nF, err := recovery.FromOldFlow(reg, time.Duration(time.Hour), f.CSRFToken, &r, nil, *f)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, ft, nF.Type)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("strategy=link", func(t *testing.T) {
|
||||
for _, ft := range []flow.Type{
|
||||
flow.TypeAPI,
|
||||
flow.TypeBrowser,
|
||||
} {
|
||||
t.Run(fmt.Sprintf("case=original flow is %s", ft), func(t *testing.T) {
|
||||
f, err := recovery.NewFlow(reg, 0, "csrf", &r, recovery.Strategies{link.NewStrategy(reg)}, ft)
|
||||
require.NoError(t, err)
|
||||
nF, err := recovery.FromOldFlow(reg, time.Duration(time.Hour), f.CSRFToken, &r, nil, *f)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, flow.TypeBrowser, nF.Type)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestFromOldFlow_capturesCourierBaseURL(t *testing.T) {
|
||||
_, reg := pkg.NewVeryFastRegistryWithoutDB(t)
|
||||
|
||||
t.Run("uses the new request's context, not the old flow's value", func(t *testing.T) {
|
||||
// Old flow was created at customer.example.com.
|
||||
oldR := (&http.Request{URL: urlx.ParseOrPanic("http://foo/bar"), Host: "foo"}).
|
||||
WithContext(x.WithBaseURL(context.Background(), urlx.ParseOrPanic("https://customer.example.com/")))
|
||||
oldF, err := recovery.NewFlow(reg, time.Hour, "csrf", oldR, nil, flow.TypeBrowser)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "https://customer.example.com/", oldF.GetCourierBaseURL())
|
||||
|
||||
// New request comes through a different proxy / CNAME — we should
|
||||
// reflect that, not silently inherit the old flow's captured URL.
|
||||
newR := (&http.Request{URL: urlx.ParseOrPanic("http://foo/bar"), Host: "foo"}).
|
||||
WithContext(x.WithBaseURL(context.Background(), urlx.ParseOrPanic("http://localhost:4000")))
|
||||
newF, err := recovery.FromOldFlow(reg, time.Hour, oldF.CSRFToken, newR, nil, *oldF)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "http://localhost:4000", newF.GetCourierBaseURL())
|
||||
})
|
||||
|
||||
t.Run("empty when neither old nor new have a base URL", func(t *testing.T) {
|
||||
r := &http.Request{URL: urlx.ParseOrPanic("http://foo/bar"), Host: "foo"}
|
||||
oldF, err := recovery.NewFlow(reg, time.Hour, "csrf", r, nil, flow.TypeBrowser)
|
||||
require.NoError(t, err)
|
||||
newF, err := recovery.FromOldFlow(reg, time.Hour, oldF.CSRFToken, r, nil, *oldF)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "", newF.GetCourierBaseURL())
|
||||
})
|
||||
}
|
||||
|
||||
// TestCourierBaseURLStoredInInternalContext locks down the storage
|
||||
// location: the captured base URL must land in InternalContext under
|
||||
// flow.InternalContextKeyCourierBaseURL, not in a dedicated column. If a
|
||||
// future refactor moves it elsewhere, this fails — separately from the
|
||||
// getter contract.
|
||||
func TestCourierBaseURLStoredInInternalContext(t *testing.T) {
|
||||
_, reg := pkg.NewVeryFastRegistryWithoutDB(t)
|
||||
|
||||
r := (&http.Request{URL: urlx.ParseOrPanic("http://foo/bar"), Host: "foo"}).
|
||||
WithContext(x.WithBaseURL(context.Background(), urlx.ParseOrPanic("https://customer.example.com/")))
|
||||
f, err := recovery.NewFlow(reg, time.Hour, "csrf", r, nil, flow.TypeBrowser)
|
||||
require.NoError(t, err)
|
||||
|
||||
got := gjson.GetBytes(f.InternalContext, flow.InternalContextKeyCourierBaseURL).String()
|
||||
assert.Equal(t, "https://customer.example.com/", got)
|
||||
}
|
||||
|
||||
func TestFlowDontOverrideReturnTo(t *testing.T) {
|
||||
f := &recovery.Flow{ReturnTo: "/foo"}
|
||||
f.SetReturnTo()
|
||||
assert.Equal(t, "/foo", f.ReturnTo)
|
||||
|
||||
f = &recovery.Flow{RequestURL: "https://foo.bar?return_to=/bar"}
|
||||
f.SetReturnTo()
|
||||
assert.Equal(t, "/bar", f.ReturnTo)
|
||||
}
|
||||
Reference in New Issue
Block a user