Skip to content

Commit 8e97a93

Browse files
committed
Prevent redirect to Host (2)
Unhelpfully Locations starting with `/\` will be converted by the browser to `//` because ... well I do not fully understand. Certainly the RFCs and MDN do not indicate that this would be expected. Providing "compatibility" with the (mis)behaviour of a certain proprietary OS is my suspicion. However, we clearly have to protect against this. Therefore we should reject redirection locations that match the regular expression: `^/[\\\\/]+` Reference #9678 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 7a550b3 commit 8e97a93

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

modules/context/context.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"net/http"
1818
"net/url"
1919
"path"
20+
"regexp"
2021
"strconv"
2122
"strings"
2223
"time"
@@ -174,13 +175,19 @@ func (ctx *Context) HasValue(name string) bool {
174175
return ok
175176
}
176177

178+
var precedingSlashesRE = regexp.MustCompile(`^/[\\/]+`)
179+
177180
// RedirectToFirst redirects to first not empty URL
178181
func (ctx *Context) RedirectToFirst(location ...string) {
179182
for _, loc := range location {
180183
if len(loc) == 0 {
181184
continue
182185
}
183186

187+
if precedingSlashesRE.MatchString(loc) {
188+
continue
189+
}
190+
184191
u, err := url.Parse(loc)
185192
if err != nil || ((u.Scheme != "" || u.Host != "") && !strings.HasPrefix(strings.ToLower(loc), strings.ToLower(setting.AppURL))) {
186193
continue

0 commit comments

Comments
 (0)