Skip to content

Commit 3f68dbb

Browse files
committed
explorer: MenuFormParser middleware should enforce relative redirect
1 parent af39ed4 commit 3f68dbb

2 files changed

Lines changed: 93 additions & 2 deletions

File tree

‎explorer/explorermiddleware.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"database/sql"
1010
"fmt"
1111
"net/http"
12+
"net/url"
1213
"strconv"
1314
"time"
1415

@@ -215,12 +216,19 @@ func MenuFormParser(next http.Handler) http.Handler {
215216
cookie.Value = "0"
216217
cookie.MaxAge = -1
217218
}
218-
http.SetCookie(w, cookie)
219+
220+
// Redirect to the specified relative path.
219221
requestURI := r.FormValue(requestURIFormKey)
220222
if requestURI == "" {
221223
requestURI = "/"
222224
}
223-
http.Redirect(w, r, requestURI, http.StatusFound)
225+
URL, err := url.Parse(requestURI)
226+
if err != nil {
227+
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
228+
return
229+
}
230+
http.SetCookie(w, cookie)
231+
http.Redirect(w, r, URL.EscapedPath(), http.StatusFound)
224232
return
225233
}
226234
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) 2019, The Decred developers
2+
// See LICENSE for details.
3+
4+
package explorer
5+
6+
import (
7+
"io"
8+
"net/http"
9+
"net/http/httptest"
10+
"net/url"
11+
"strings"
12+
"testing"
13+
)
14+
15+
func TestMenuFormParser(t *testing.T) {
16+
// Dummy hander for use with the MenuFormParser middleware.
17+
blah := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18+
w.WriteHeader(http.StatusOK)
19+
io.WriteString(w, "blah")
20+
})
21+
// The handler wrapping blah with MenuFormParser.
22+
handler := MenuFormParser(blah)
23+
24+
// TEST with no form data. Expected response body: "blah"
25+
r := httptest.NewRequest("POST", "/set", nil)
26+
27+
w := httptest.NewRecorder()
28+
handler.ServeHTTP(w, r)
29+
//resp := w.Result()
30+
31+
if w.Body.String() != "blah" {
32+
t.Errorf("MenuFormParser failed to call dummy handler's ServeHTTP.")
33+
}
34+
35+
// TEST with form with requestURIFormKey ("requestURI"). Expect 302 redirect
36+
// to the relative URL (path, no scheme, no host, no query, no fragment).
37+
form := url.Values{}
38+
form.Add(darkModeFormKey, "1")
39+
form.Add(requestURIFormKey, "https://explorer.decred.org/blocks?junk=1#fraggle")
40+
41+
r = httptest.NewRequest("POST", "/set", strings.NewReader(form.Encode()))
42+
r.Header.Add("Content-Type", "application/x-www-form-urlencoded") // Required!
43+
// darkCookie := &http.Cookie{
44+
// Name: darkModeCoookie,
45+
// Value: "1",
46+
// MaxAge: 0,
47+
// }
48+
// r.AddCookie(darkCookie)
49+
50+
w = httptest.NewRecorder()
51+
handler.ServeHTTP(w, r)
52+
resp := w.Result()
53+
54+
if w.Body.String() != "" {
55+
t.Errorf("MenuFormParser failed to respond with an empty body. Got: %v.",
56+
w.Body.String())
57+
}
58+
59+
if resp.StatusCode != http.StatusFound {
60+
t.Errorf("MenuFormParser failed to respond with status 302 FOUND. Got: %v.",
61+
resp.StatusCode)
62+
}
63+
64+
loc, err := resp.Location()
65+
if err != nil {
66+
t.Errorf(`Location header not found or invalid: "%v"`, err)
67+
}
68+
if loc.String() != "/blocks" {
69+
t.Errorf(`Location header not set to "/blocks", got "%s".`, loc)
70+
}
71+
if loc.IsAbs() {
72+
t.Errorf(`Location should have been relative, was absolute: "%s"`, loc)
73+
}
74+
if len(loc.Query()) > 0 {
75+
t.Errorf(`Location included a query, should have been an escaped path. Loc: "%s"`, loc)
76+
}
77+
if loc.String() != loc.EscapedPath() {
78+
t.Errorf(`Location should have been JUST an escaped path, got "%s"`, loc)
79+
}
80+
if loc.Host != "" {
81+
t.Errorf("Location should not have a host, but it was %s", loc.Host)
82+
}
83+
}

0 commit comments

Comments
 (0)
⌂ 换网址