Skip to content

Commit f812298

Browse files
committedNov 21, 2016
Redirect to server root if login flow is started with no destination
1 parent ca3d987 commit f812298

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed
 

‎pkg/auth/server/login/login.go

+7
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ func (l *Login) handleLoginForm(w http.ResponseWriter, req *http.Request) {
125125
if then := req.URL.Query().Get("then"); then != "" {
126126
// TODO: sanitize 'then'
127127
form.Values.Then = then
128+
} else {
129+
http.Redirect(w, req, "/", http.StatusFound)
130+
return
128131
}
129132

130133
form.ErrorCode = req.URL.Query().Get("reason")
@@ -152,6 +155,10 @@ func (l *Login) handleLogin(w http.ResponseWriter, req *http.Request) {
152155
return
153156
}
154157
then := req.FormValue("then")
158+
if len(then) == 0 {
159+
http.Redirect(w, req, "/", http.StatusFound)
160+
return
161+
}
155162
username, password := req.FormValue("username"), req.FormValue("password")
156163
if username == "" {
157164
failed(errorCodeUserRequired, w, req)

‎pkg/auth/server/login/login_test.go

+27-6
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ func TestLogin(t *testing.T) {
5353
"display form": {
5454
CSRF: &csrf.FakeCSRF{Token: "test"},
5555
Auth: &testAuth{},
56-
Path: "/login",
56+
Path: "/login?then=%2F",
5757

5858
ExpectStatusCode: 200,
5959
ExpectContains: []string{
6060
`action="/login"`,
6161
`name="csrf" value="test"`,
62+
`name="then" value="/"`,
6263
},
6364
},
6465
"display form with errors": {
@@ -74,6 +75,21 @@ func TestLogin(t *testing.T) {
7475
`danger`,
7576
},
7677
},
78+
"redirect when GET has no then param": {
79+
CSRF: &csrf.FakeCSRF{Token: "test"},
80+
Auth: &testAuth{},
81+
Path: "/login",
82+
83+
ExpectStatusCode: 302,
84+
ExpectRedirect: "/",
85+
},
86+
"redirect when POST is missing then param": {
87+
CSRF: &csrf.FakeCSRF{Token: "test"},
88+
Auth: &testAuth{},
89+
Path: "/login",
90+
PostValues: url.Values{"csrf": []string{"test"}},
91+
ExpectRedirect: "/",
92+
},
7793
"redirect when POST fails CSRF": {
7894
CSRF: &csrf.FakeCSRF{Token: "test"},
7995
Auth: &testAuth{},
@@ -94,8 +110,9 @@ func TestLogin(t *testing.T) {
94110
Path: "/login",
95111
PostValues: url.Values{
96112
"csrf": []string{"test"},
113+
"then": []string{"anotherurl"},
97114
},
98-
ExpectRedirect: "/login?reason=user_required",
115+
ExpectRedirect: "/login?reason=user_required&then=anotherurl",
99116
},
100117
"redirect when not authenticated": {
101118
CSRF: &csrf.FakeCSRF{Token: "test"},
@@ -104,8 +121,9 @@ func TestLogin(t *testing.T) {
104121
PostValues: url.Values{
105122
"csrf": []string{"test"},
106123
"username": []string{"user"},
124+
"then": []string{"anotherurl"},
107125
},
108-
ExpectRedirect: "/login?reason=access_denied",
126+
ExpectRedirect: "/login?reason=access_denied&then=anotherurl",
109127
},
110128
"redirect on auth error": {
111129
CSRF: &csrf.FakeCSRF{Token: "test"},
@@ -114,8 +132,9 @@ func TestLogin(t *testing.T) {
114132
PostValues: url.Values{
115133
"csrf": []string{"test"},
116134
"username": []string{"user"},
135+
"then": []string{"anotherurl"},
117136
},
118-
ExpectRedirect: "/login?reason=authentication_error",
137+
ExpectRedirect: "/login?reason=authentication_error&then=anotherurl",
119138
},
120139
"redirect on lookup error": {
121140
CSRF: &csrf.FakeCSRF{Token: "test"},
@@ -124,8 +143,9 @@ func TestLogin(t *testing.T) {
124143
PostValues: url.Values{
125144
"csrf": []string{"test"},
126145
"username": []string{"user"},
146+
"then": []string{"anotherurl"},
127147
},
128-
ExpectRedirect: "/login?reason=mapping_lookup_error",
148+
ExpectRedirect: "/login?reason=mapping_lookup_error&then=anotherurl",
129149
},
130150
"redirect on claim error": {
131151
CSRF: &csrf.FakeCSRF{Token: "test"},
@@ -134,8 +154,9 @@ func TestLogin(t *testing.T) {
134154
PostValues: url.Values{
135155
"csrf": []string{"test"},
136156
"username": []string{"user"},
157+
"then": []string{"anotherurl"},
137158
},
138-
ExpectRedirect: "/login?reason=mapping_claim_error",
159+
ExpectRedirect: "/login?reason=mapping_claim_error&then=anotherurl",
139160
},
140161
"redirect preserving then param": {
141162
CSRF: &csrf.FakeCSRF{Token: "test"},

‎test/integration/web_console_access_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func TestAccessOriginWebConsole(t *testing.T) {
9898
}{
9999
"": {http.StatusFound, masterOptions.AssetConfig.PublicURL},
100100
"healthz": {http.StatusOK, ""},
101-
"login": {http.StatusOK, ""},
101+
"login?then=%2F": {http.StatusOK, ""},
102102
"oauth/token/request": {http.StatusFound, masterOptions.AssetConfig.MasterPublicURL + "/oauth/authorize"},
103103
"console": {http.StatusMovedPermanently, "/console/"},
104104
"console/": {http.StatusOK, ""},
@@ -137,7 +137,7 @@ func TestAccessDisabledWebConsole(t *testing.T) {
137137
location string
138138
}{
139139
"healthz": {http.StatusOK, ""},
140-
"login": {http.StatusOK, ""},
140+
"login?then=%2F": {http.StatusOK, ""},
141141
"oauth/token/request": {http.StatusFound, masterOptions.AssetConfig.MasterPublicURL + "/oauth/authorize"},
142142
"console": {http.StatusForbidden, ""},
143143
"console/": {http.StatusForbidden, ""},
@@ -220,8 +220,8 @@ func TestAccessOriginWebConsoleMultipleIdentityProviders(t *testing.T) {
220220

221221
// Expect the providerSelectionURL to redirect to the loginURL
222222
urlMap[providerSelectionURL] = urlResults{http.StatusFound, loginURL}
223-
// Expect the loginURL to be valid
224-
urlMap[loginURL] = urlResults{http.StatusOK, ""}
223+
// Expect the loginURL to be valid (requires a 'then' param)
224+
urlMap[loginURL+"?then=%2F"] = urlResults{http.StatusOK, ""}
225225

226226
// escape the query param the way the template will
227227
templateIDPParam := templateEscapeHref(t, idpQueryParam)

0 commit comments

Comments
 (0)
Please sign in to comment.