-
-
Notifications
You must be signed in to change notification settings - Fork 928
/
http_secure_redirect_test.go
303 lines (266 loc) · 12.8 KB
/
http_secure_redirect_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package x_test
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/julienschmidt/httprouter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ory/herodot"
"github.com/ory/x/urlx"
"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/internal"
"github.com/ory/kratos/x"
)
func TestSecureContentNegotiationRedirection(t *testing.T) {
conf, _ := internal.NewFastRegistryWithMocks(t)
var jsonActual = json.RawMessage(`{"foo":"bar"}` + "\n")
writer := herodot.NewJSONWriter(nil)
router := httprouter.New()
router.GET("/redir", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
require.NoError(t, x.SecureContentNegotiationRedirection(w, r, jsonActual, x.RequestURL(r).String(), writer, conf))
})
router.GET("/default-return-to", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.WriteHeader(http.StatusNoContent)
})
router.GET("/return-to", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.WriteHeader(http.StatusNoContent)
})
ts := httptest.NewServer(router)
defer ts.Close()
ctx := context.Background()
defaultReturnTo := ts.URL + "/default-return-to"
conf.MustSet(ctx, config.ViperKeySelfServiceBrowserDefaultReturnTo, defaultReturnTo)
conf.MustSet(ctx, config.ViperKeyPublicBaseURL, ts.URL)
conf.MustSet(ctx, config.ViperKeyURLsAllowedReturnToDomains, []string{ts.URL})
run := func(t *testing.T, href string, contentType string) (*http.Response, string) {
req, err := http.NewRequest("GET", href, nil)
require.NoError(t, err)
req.Header.Add("Accept", contentType)
res, err := ts.Client().Do(req)
require.NoError(t, err)
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
require.NoError(t, res.Body.Close())
return res, string(body)
}
t.Run("case=html browser causes redirect", func(t *testing.T) {
res, _ := run(t, ts.URL+"/redir", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
assert.EqualValues(t, res.Request.URL.String(), defaultReturnTo)
})
t.Run("case=html browser causes redirect with redirect_to", func(t *testing.T) {
res, _ := run(t, ts.URL+"/redir?return_to=/redirect-to", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
assert.EqualValues(t, res.Request.URL.String(), ts.URL+"/redirect-to")
})
t.Run("case=html browser causes redirect with redirect_to", func(t *testing.T) {
res, body := run(t, ts.URL+"/redir?return_to=/redirect-to", "application/json")
assert.EqualValues(t, res.Request.URL.String(), ts.URL+"/redir?return_to=/redirect-to")
assert.EqualValues(t, body, jsonActual)
})
}
func TestSecureRedirectToIsAllowedHost(t *testing.T) {
type testCase struct {
allowedURL string
redirectURL string
valid bool
}
tests := map[string]testCase{
"case=Domain is allowed": {allowedURL: "https://foo.bar", redirectURL: "https://foo.bar/redir", valid: true},
"case=Domain prefix is allowed": {allowedURL: "https://*.bar", redirectURL: "https://foo.bar/redir", valid: true},
"case=Subdomain prefix is allowed": {allowedURL: "https://*.foo.bar", redirectURL: "https://auth.foo.bar/redir", valid: true},
"case=Domain is not allowed": {allowedURL: "https://foo.baz", redirectURL: "https://foo.bar/redir", valid: false},
"case=Domain wildcard is not allowed": {allowedURL: "https://*.foo.baz", redirectURL: "https://foo.bar/redir", valid: false},
"case=Subdomain is not allowed": {allowedURL: "https://*.foo.baz", redirectURL: "https://auth.foo.bar/redir", valid: false},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
allowedURL, err := url.Parse(tc.allowedURL)
require.NoError(t, err)
redirectURL, err := url.Parse(tc.redirectURL)
require.NoError(t, err)
assert.Equal(t, x.SecureRedirectToIsAllowedHost(redirectURL, *allowedURL), tc.valid)
})
}
}
func TestTakeOverReturnToParameter(t *testing.T) {
type testCase struct {
fromUrl string
toURL string
expectedOutputUrl string
}
tests := map[string]testCase{
"case=return_to is taken over": {fromUrl: "https://original.bar?return_to=https://allowed.domain", toURL: "https://output.bar", expectedOutputUrl: "https://output.bar?return_to=https%3A%2F%2Fallowed.domain"},
"case=only return_to is taken over when multiple query parameters are set": {fromUrl: "https://original.bar?return_to=https://allowed.domain&flow=12312", toURL: "https://output.bar", expectedOutputUrl: "https://output.bar?return_to=https%3A%2F%2Fallowed.domain"},
"case=output query parameters are preserved": {fromUrl: "https://original.bar?return_to=https://allowed.domain", toURL: "https://output.bar?flow=123321", expectedOutputUrl: "https://output.bar?flow=123321&return_to=https%3A%2F%2Fallowed.domain"},
"case=when original return_to is empty do nothing": {fromUrl: "https://original.bar?return_to=", toURL: "https://output.bar?flow=123123", expectedOutputUrl: "https://output.bar?flow=123123"},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
output, err := x.TakeOverReturnToParameter(tc.fromUrl, tc.toURL)
require.NoError(t, err)
assert.Equal(t, output, tc.expectedOutputUrl)
})
}
}
func TestSecureRedirectTo(t *testing.T) {
newServer := func(t *testing.T, isTLS bool, isRelative bool, expectErr bool, opts func(ts *httptest.Server) []x.SecureRedirectOption) *httptest.Server {
var ts *httptest.Server
f := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if opts == nil {
opts = func(ts *httptest.Server) []x.SecureRedirectOption {
return nil
}
}
defaultReturnTo := "/default-return-to"
if !isRelative {
defaultReturnTo = ts.URL + defaultReturnTo
}
ru, err := x.SecureRedirectTo(r, urlx.ParseOrPanic(defaultReturnTo), opts(ts)...)
if expectErr {
require.Error(t, err)
_, _ = w.Write([]byte("error"))
return
}
require.NoError(t, err)
_, _ = w.Write([]byte(ru.String()))
})
if isTLS {
ts = httptest.NewTLSServer(f)
} else {
ts = httptest.NewServer(f)
}
t.Cleanup(ts.Close)
return ts
}
makeRequest := func(t *testing.T, ts *httptest.Server, path string) (*http.Response, string) {
res, err := ts.Client().Get(ts.URL + "/" + path)
require.NoError(t, err)
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
require.NoError(t, res.Body.Close())
return res, string(body)
}
t.Run("case=return to a relative path with anchor works", func(t *testing.T) {
s := newServer(t, false, true, false, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{x.SecureRedirectAllowURLs([]url.URL{*urlx.ParseOrPanic("/foo")})}
})
_, body := makeRequest(t, s, "?return_to=/foo/kratos%23abcd")
assert.Equal(t, body, "/foo/kratos#abcd")
})
t.Run("case=return to default URL if nothing is allowed", func(t *testing.T) {
s := newServer(t, false, false, false, nil)
_, body := makeRequest(t, s, "?return_to=/foo")
assert.EqualValues(t, body, s.URL+"/default-return-to")
})
t.Run("case=return to foo with server baseURL if allowed", func(t *testing.T) {
s := newServer(t, false, false, false, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{x.SecureRedirectAllowURLs([]url.URL{*urlx.ParseOrPanic(ts.URL)})}
})
_, body := makeRequest(t, s, "?return_to=/foo")
assert.Equal(t, body, s.URL+"/foo")
})
t.Run("case=return to a relative path works", func(t *testing.T) {
s := newServer(t, false, true, false, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{x.SecureRedirectAllowURLs([]url.URL{*urlx.ParseOrPanic("/foo")})}
})
_, body := makeRequest(t, s, "?return_to=/foo/kratos")
assert.Equal(t, body, "/foo/kratos")
})
t.Run("case=return to a fully qualified domain is forbidden if allowlist is relative", func(t *testing.T) {
s := newServer(t, false, true, true, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{x.SecureRedirectAllowURLs([]url.URL{*urlx.ParseOrPanic("/foo")})}
})
_, body := makeRequest(t, s, "?return_to=https://www.ory.sh/foo/kratos")
assert.Equal(t, body, "error")
})
t.Run("case=return to another domain works", func(t *testing.T) {
s := newServer(t, false, false, false, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{x.SecureRedirectAllowURLs([]url.URL{*urlx.ParseOrPanic("https://www.ory.sh/foo")})}
})
_, body := makeRequest(t, s, "?return_to=https://www.ory.sh/foo/kratos")
assert.Equal(t, body, "https://www.ory.sh/foo/kratos")
})
t.Run("case=return to another domain fails if host mismatches", func(t *testing.T) {
s := newServer(t, false, false, true, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{x.SecureRedirectAllowURLs([]url.URL{*urlx.ParseOrPanic("https://www.not-ory.sh/")})}
})
_, body := makeRequest(t, s, "?return_to=https://www.ory.sh/kratos")
assert.Equal(t, body, "error")
})
t.Run("case=return to another domain fails if path mismatches", func(t *testing.T) {
s := newServer(t, false, false, true, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{x.SecureRedirectAllowURLs([]url.URL{*urlx.ParseOrPanic("https://www.ory.sh/not-kratos")})}
})
_, body := makeRequest(t, s, "?return_to=https://www.ory.sh/kratos")
assert.Equal(t, body, "error")
})
t.Run("case=return to another domain fails if scheme mismatches", func(t *testing.T) {
s := newServer(t, false, false, true, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{x.SecureRedirectAllowURLs([]url.URL{*urlx.ParseOrPanic("http://www.ory.sh/")})}
})
_, body := makeRequest(t, s, "?return_to=https://www.ory.sh/kratos")
assert.Equal(t, body, "error")
})
t.Run("case=should work with self-service modifier", func(t *testing.T) {
s := newServer(t, false, false, false, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{x.SecureRedirectAllowSelfServiceURLs(urlx.ParseOrPanic(ts.URL))}
})
_, body := makeRequest(t, s, "?return_to=/self-service/foo")
assert.Equal(t, body, s.URL+"/self-service/foo")
})
t.Run("case=should work with default return to", func(t *testing.T) {
s := newServer(t, false, false, false, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{x.SecureRedirectOverrideDefaultReturnTo(urlx.ParseOrPanic(ts.URL + "/another-default"))}
})
_, body := makeRequest(t, s, "")
assert.Equal(t, body, s.URL+"/another-default")
})
t.Run("case=should override return_to", func(t *testing.T) {
s := newServer(t, false, false, false, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{
x.SecureRedirectAllowURLs([]url.URL{*urlx.ParseOrPanic(ts.URL)}),
x.SecureRedirectUseSourceURL("https://foo/bar?return_to=/override"),
}
})
_, body := makeRequest(t, s, "?return_to=/original")
assert.Equal(t, body, s.URL+"/override")
})
t.Run("case=should work with subdomain wildcard", func(t *testing.T) {
s := newServer(t, false, false, false, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{x.SecureRedirectAllowURLs([]url.URL{*urlx.ParseOrPanic("https://*.ory.sh/")})}
})
_, body := makeRequest(t, s, "?return_to=https://www.ory.sh/kratos")
assert.Equal(t, body, "https://www.ory.sh/kratos")
_, body = makeRequest(t, s, "?return_to=https://even.deeper.nested.ory.sh/kratos")
assert.Equal(t, body, "https://even.deeper.nested.ory.sh/kratos")
})
t.Run("case=should fallback to default return_to scheme", func(t *testing.T) {
s := newServer(t, false, false, false, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{
x.SecureRedirectAllowURLs([]url.URL{*urlx.ParseOrPanic("https://www.ory.sh")}),
x.SecureRedirectOverrideDefaultReturnTo(urlx.ParseOrPanic("https://www.ory.sh/docs")),
}
})
_, body := makeRequest(t, s, "?return_to=//www.ory.sh/kratos")
assert.Equal(t, body, "https://www.ory.sh/kratos")
})
t.Run("case=should fallback to default return_to host", func(t *testing.T) {
s := newServer(t, false, false, false, func(ts *httptest.Server) []x.SecureRedirectOption {
return []x.SecureRedirectOption{
x.SecureRedirectAllowURLs([]url.URL{
*urlx.ParseOrPanic("https://www.ory.sh"),
*urlx.ParseOrPanic("http://www.ory.sh"),
}),
x.SecureRedirectOverrideDefaultReturnTo(urlx.ParseOrPanic("https://www.ory.sh/docs")),
}
})
_, body := makeRequest(t, s, "?return_to=http:///kratos")
assert.Equal(t, body, "http://www.ory.sh/kratos")
})
}