forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use strict protocol check when redirect (go-gitea#29642)
- Loading branch information
1 parent
9730d3a
commit c72e1a7
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package context | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRedirect(t *testing.T) { | ||
req, _ := http.NewRequest("GET", "/", nil) | ||
|
||
cases := []struct { | ||
url string | ||
keep bool | ||
}{ | ||
{"http://test", false}, | ||
{"https://test", false}, | ||
{"//test", false}, | ||
{"/://test", true}, | ||
{"/test", true}, | ||
} | ||
for _, c := range cases { | ||
resp := httptest.NewRecorder() | ||
b, cleanup := NewBaseContext(resp, req) | ||
resp.Header().Add("Set-Cookie", (&http.Cookie{Name: setting.SessionConfig.CookieName, Value: "dummy"}).String()) | ||
b.Redirect(c.url) | ||
cleanup() | ||
has := resp.Header().Get("Set-Cookie") == "i_like_gitea=dummy" | ||
assert.Equal(t, c.keep, has, "url = %q", c.url) | ||
} | ||
|
||
req, _ = http.NewRequest("GET", "/", nil) | ||
resp := httptest.NewRecorder() | ||
req.Header.Add("HX-Request", "true") | ||
b, cleanup := NewBaseContext(resp, req) | ||
b.Redirect("/other") | ||
cleanup() | ||
assert.Equal(t, "/other", resp.Header().Get("HX-Redirect")) | ||
assert.Equal(t, http.StatusNoContent, resp.Code) | ||
} |