Merge pull request #465 from samcharles93/alert-autofix-5
Potential fix for code scanning alert no. 5: Open URL redirect
This commit is contained in:
commit
865905a95a
2 files changed
+36
-4
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -145,16 +146,20 @@ func (s *Server) requireToken(h http.Handler) http.Handler {
|
||||
// this path is echoed back from the incoming request URL, an attacker can
|
||||
// choose it, so it must never reach http.Redirect verbatim.
|
||||
func sameOriginPath(path, rawQuery string) string {
|
||||
// URL.Path has already been decoded by net/http. Treat backslashes as
|
||||
// separators before checking for a protocol-relative path, because browsers
|
||||
// do the same when interpreting a redirect Location.
|
||||
path = strings.ReplaceAll(path, "\\", "/")
|
||||
for strings.HasPrefix(path, "//") {
|
||||
path = path[1:]
|
||||
}
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
if rawQuery != "" {
|
||||
return path + "?" + rawQuery
|
||||
}
|
||||
return path
|
||||
|
||||
// Re-encoding through URL.Path preserves decoded path characters such as
|
||||
// '%', '?' and '#' as path data instead of reparsing them as URL syntax.
|
||||
return (&url.URL{Path: path, RawQuery: rawQuery}).RequestURI()
|
||||
}
|
||||
|
||||
func tokenEqual(got, want string) bool {
|
||||
|
||||
@@ -26,6 +26,33 @@ func TestRequireToken_RedirectRejectsProtocolRelativePath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSameOriginPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
rawQuery string
|
||||
want string
|
||||
}{
|
||||
{name: "protocol relative", path: "//evil.example/x", want: "/evil.example/x"},
|
||||
{name: "backslash protocol relative", path: "/\\\\evil.example/x", want: "/evil.example/x"},
|
||||
{name: "double backslash protocol relative", path: "\\\\evil.example\\x", want: "/evil.example/x"},
|
||||
{name: "ordinary path and query", path: "/dashboard", rawQuery: "foo=bar", want: "/dashboard?foo=bar"},
|
||||
{name: "encoded percent", path: "/foo%bar", want: "/foo%25bar"},
|
||||
{name: "encoded question mark", path: "/foo?bar", want: "/foo%3Fbar"},
|
||||
{name: "encoded hash", path: "/foo#bar", want: "/foo%23bar"},
|
||||
{name: "encoded slash", path: "/foo/bar", want: "/foo/bar"},
|
||||
{name: "encoded backslash", path: "/foo\\bar", want: "/foo/bar"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := sameOriginPath(tt.path, tt.rawQuery); got != tt.want {
|
||||
t.Fatalf("sameOriginPath(%q, %q) = %q, want %q", tt.path, tt.rawQuery, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireToken_RedirectStripsTokenFromCleanURL(t *testing.T) {
|
||||
s := &Server{Token: "secret-token"}
|
||||
h := s.requireToken(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in new issue
Block a user