summaryrefslogtreecommitdiffstats
path: root/tests/integration
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2024-09-19 01:02:45 +0800
committerGitHub <noreply@github.com>2024-09-18 17:02:45 +0000
commit2891edbbcbc59ae5928404d45910a2dc2e365c87 (patch)
treedb17b02a3e5f0733be0a974060f51a138695823d /tests/integration
parent8dbe83d20524c21329bfe43466f0f233c01e9c8a (diff)
downloadgitea-2891edbbcbc59ae5928404d45910a2dc2e365c87.tar.gz
gitea-2891edbbcbc59ae5928404d45910a2dc2e365c87.zip
Refactor CSRF protector (#32057) (#32069)
#32057 improves the CSRF handling and is worth to backport
Diffstat (limited to 'tests/integration')
-rw-r--r--tests/integration/attachment_test.go3
-rw-r--r--tests/integration/csrf_test.go26
-rw-r--r--tests/integration/repo_branch_test.go12
3 files changed, 8 insertions, 33 deletions
diff --git a/tests/integration/attachment_test.go b/tests/integration/attachment_test.go
index 8206d8f4dc..40969d26f2 100644
--- a/tests/integration/attachment_test.go
+++ b/tests/integration/attachment_test.go
@@ -59,7 +59,8 @@ func createAttachment(t *testing.T, session *TestSession, repoURL, filename stri
func TestCreateAnonymousAttachment(t *testing.T) {
defer tests.PrepareTestEnv(t)()
session := emptyTestSession(t)
- createAttachment(t, session, "user2/repo1", "image.png", generateImg(), http.StatusSeeOther)
+ // this test is not right because it just doesn't pass the CSRF validation
+ createAttachment(t, session, "user2/repo1", "image.png", generateImg(), http.StatusBadRequest)
}
func TestCreateIssueAttachment(t *testing.T) {
diff --git a/tests/integration/csrf_test.go b/tests/integration/csrf_test.go
index a789859889..fcb9661b8a 100644
--- a/tests/integration/csrf_test.go
+++ b/tests/integration/csrf_test.go
@@ -5,12 +5,10 @@ package integration
import (
"net/http"
- "strings"
"testing"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
- "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert"
@@ -25,28 +23,12 @@ func TestCsrfProtection(t *testing.T) {
req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
"_csrf": "fake_csrf",
})
- session.MakeRequest(t, req, http.StatusSeeOther)
-
- resp := session.MakeRequest(t, req, http.StatusSeeOther)
- loc := resp.Header().Get("Location")
- assert.Equal(t, setting.AppSubURL+"/", loc)
- resp = session.MakeRequest(t, NewRequest(t, "GET", loc), http.StatusOK)
- htmlDoc := NewHTMLParser(t, resp.Body)
- assert.Equal(t, "Bad Request: invalid CSRF token",
- strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
- )
+ resp := session.MakeRequest(t, req, http.StatusBadRequest)
+ assert.Contains(t, resp.Body.String(), "Invalid CSRF token")
// test web form csrf via header. TODO: should use an UI api to test
req = NewRequest(t, "POST", "/user/settings")
req.Header.Add("X-Csrf-Token", "fake_csrf")
- session.MakeRequest(t, req, http.StatusSeeOther)
-
- resp = session.MakeRequest(t, req, http.StatusSeeOther)
- loc = resp.Header().Get("Location")
- assert.Equal(t, setting.AppSubURL+"/", loc)
- resp = session.MakeRequest(t, NewRequest(t, "GET", loc), http.StatusOK)
- htmlDoc = NewHTMLParser(t, resp.Body)
- assert.Equal(t, "Bad Request: invalid CSRF token",
- strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
- )
+ resp = session.MakeRequest(t, req, http.StatusBadRequest)
+ assert.Contains(t, resp.Body.String(), "Invalid CSRF token")
}
diff --git a/tests/integration/repo_branch_test.go b/tests/integration/repo_branch_test.go
index d1bc9198c3..f5217374b0 100644
--- a/tests/integration/repo_branch_test.go
+++ b/tests/integration/repo_branch_test.go
@@ -17,7 +17,6 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/models/unittest"
- "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/translation"
@@ -146,15 +145,8 @@ func TestCreateBranchInvalidCSRF(t *testing.T) {
"_csrf": "fake_csrf",
"new_branch_name": "test",
})
- resp := session.MakeRequest(t, req, http.StatusSeeOther)
- loc := resp.Header().Get("Location")
- assert.Equal(t, setting.AppSubURL+"/", loc)
- resp = session.MakeRequest(t, NewRequest(t, "GET", loc), http.StatusOK)
- htmlDoc := NewHTMLParser(t, resp.Body)
- assert.Equal(t,
- "Bad Request: invalid CSRF token",
- strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
- )
+ resp := session.MakeRequest(t, req, http.StatusBadRequest)
+ assert.Contains(t, resp.Body.String(), "Invalid CSRF token")
}
func prepareBranch(t *testing.T, session *TestSession, repo *repo_model.Repository) {