@@ -255,96 +255,3 @@ func TestSafeRedirect_Redirect(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSafeRedirect_RedirectFromQuery(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
allowedHost string
|
||||
queryParam string
|
||||
queryValue string
|
||||
fallbackURL string
|
||||
expectedStatus int
|
||||
expectedURL string
|
||||
}{
|
||||
{
|
||||
name: "safe continue param",
|
||||
allowedHost: "example.com",
|
||||
queryParam: "continue",
|
||||
queryValue: "/dashboard",
|
||||
fallbackURL: "/home",
|
||||
expectedStatus: http.StatusFound,
|
||||
expectedURL: "/dashboard",
|
||||
},
|
||||
{
|
||||
name: "unsafe continue param",
|
||||
allowedHost: "example.com",
|
||||
queryParam: "continue",
|
||||
queryValue: "https://evil.com/phishing",
|
||||
fallbackURL: "/home",
|
||||
expectedStatus: http.StatusFound,
|
||||
expectedURL: "/home",
|
||||
},
|
||||
{
|
||||
name: "missing continue param",
|
||||
allowedHost: "example.com",
|
||||
queryParam: "continue",
|
||||
queryValue: "",
|
||||
fallbackURL: "/home",
|
||||
expectedStatus: http.StatusFound,
|
||||
expectedURL: "/home",
|
||||
},
|
||||
{
|
||||
name: "different query param name",
|
||||
allowedHost: "example.com",
|
||||
queryParam: "next",
|
||||
queryValue: "/profile",
|
||||
fallbackURL: "/home",
|
||||
expectedStatus: http.StatusFound,
|
||||
expectedURL: "/profile",
|
||||
},
|
||||
{
|
||||
name: "double slash attack in query",
|
||||
allowedHost: "example.com",
|
||||
queryParam: "continue",
|
||||
queryValue: "//evil.com/phishing",
|
||||
fallbackURL: "/home",
|
||||
expectedStatus: http.StatusFound,
|
||||
expectedURL: "/home",
|
||||
},
|
||||
{
|
||||
name: "slash-backslash attack in query",
|
||||
allowedHost: "example.com",
|
||||
queryParam: "continue",
|
||||
queryValue: "/\\evil.com/phishing",
|
||||
fallbackURL: "/home",
|
||||
expectedStatus: http.StatusFound,
|
||||
expectedURL: "/home",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
sr := saferedirect.SafeRedirect{
|
||||
AllowedHost: tt.allowedHost,
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
url := "http://test.com"
|
||||
if tt.queryValue != "" {
|
||||
url = "http://test.com?" + tt.queryParam + "=" + tt.queryValue
|
||||
}
|
||||
r := httptest.NewRequest("GET", url, nil)
|
||||
|
||||
sr.RedirectFromQuery(w, r, tt.queryParam, tt.fallbackURL, tt.expectedStatus)
|
||||
|
||||
if w.Code != tt.expectedStatus {
|
||||
t.Errorf("RedirectFromQuery() status = %v, want %v", w.Code, tt.expectedStatus)
|
||||
}
|
||||
|
||||
location := w.Header().Get("Location")
|
||||
if location != tt.expectedURL {
|
||||
t.Errorf("RedirectFromQuery() location = %v, want %v", location, tt.expectedURL)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user