Skip to content

Commit 4703e52

Browse files
GiteaBotwolfogre
andauthored
Ensure GetCSRF doesn't return an empty token (#32130) (#32157)
Backport #32130 by @wolfogre Since page templates keep changing, some pages that contained forms with CSRF token no longer have them. It leads to some calls of `GetCSRF` returning an empty string, which fails the tests. Like https://github.com/go-gitea/gitea/blob/3269b04d61ffe6a7ce462cd05ee150e4491124e8/tests/integration/attachment_test.go#L62-L63 The test did try to get the CSRF token and provided it, but it was empty. Co-authored-by: Jason Song <[email protected]>
1 parent 9fc3915 commit 4703e52

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

tests/integration/attachment_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func generateImg() bytes.Buffer {
2929
return buff
3030
}
3131

32-
func createAttachment(t *testing.T, session *TestSession, repoURL, filename string, buff bytes.Buffer, expectedStatus int) string {
32+
func createAttachment(t *testing.T, session *TestSession, csrf, repoURL, filename string, buff bytes.Buffer, expectedStatus int) string {
3333
body := &bytes.Buffer{}
3434

3535
// Setup multi-part
@@ -41,8 +41,6 @@ func createAttachment(t *testing.T, session *TestSession, repoURL, filename stri
4141
err = writer.Close()
4242
assert.NoError(t, err)
4343

44-
csrf := GetCSRF(t, session, repoURL)
45-
4644
req := NewRequestWithBody(t, "POST", repoURL+"/issues/attachments", body)
4745
req.Header.Add("X-Csrf-Token", csrf)
4846
req.Header.Add("Content-Type", writer.FormDataContentType())
@@ -59,15 +57,14 @@ func createAttachment(t *testing.T, session *TestSession, repoURL, filename stri
5957
func TestCreateAnonymousAttachment(t *testing.T) {
6058
defer tests.PrepareTestEnv(t)()
6159
session := emptyTestSession(t)
62-
// this test is not right because it just doesn't pass the CSRF validation
63-
createAttachment(t, session, "user2/repo1", "image.png", generateImg(), http.StatusBadRequest)
60+
createAttachment(t, session, GetCSRF(t, session, "/user/login"), "user2/repo1", "image.png", generateImg(), http.StatusSeeOther)
6461
}
6562

6663
func TestCreateIssueAttachment(t *testing.T) {
6764
defer tests.PrepareTestEnv(t)()
6865
const repoURL = "user2/repo1"
6966
session := loginUser(t, "user2")
70-
uuid := createAttachment(t, session, repoURL, "image.png", generateImg(), http.StatusOK)
67+
uuid := createAttachment(t, session, GetCSRF(t, session, repoURL), repoURL, "image.png", generateImg(), http.StatusOK)
7168

7269
req := NewRequest(t, "GET", repoURL+"/issues/new")
7370
resp := session.MakeRequest(t, req, http.StatusOK)

tests/integration/integration_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737

3838
"github.com/PuerkitoBio/goquery"
3939
"github.com/stretchr/testify/assert"
40+
"github.com/stretchr/testify/require"
4041
"github.com/xeipuuv/gojsonschema"
4142
)
4243

@@ -486,12 +487,16 @@ func VerifyJSONSchema(t testing.TB, resp *httptest.ResponseRecorder, schemaFile
486487
}
487488

488489
// GetCSRF returns CSRF token from body
490+
// If it fails, it means the CSRF token is not found in the response body returned by the url with the given session.
491+
// In this case, you should find a better url to get it.
489492
func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
490493
t.Helper()
491494
req := NewRequest(t, "GET", urlStr)
492495
resp := session.MakeRequest(t, req, http.StatusOK)
493496
doc := NewHTMLParser(t, resp.Body)
494-
return doc.GetCSRF()
497+
csrf := doc.GetCSRF()
498+
require.NotEmpty(t, csrf)
499+
return csrf
495500
}
496501

497502
// GetCSRFFrom returns CSRF token from body

tests/integration/org_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@ func TestTeamSearch(t *testing.T) {
204204
var results TeamSearchResults
205205

206206
session := loginUser(t, user.Name)
207-
csrf := GetCSRF(t, session, "/"+org.Name)
208207
req := NewRequestf(t, "GET", "/org/%s/teams/-/search?q=%s", org.Name, "_team")
209-
req.Header.Add("X-Csrf-Token", csrf)
210208
resp := session.MakeRequest(t, req, http.StatusOK)
211209
DecodeJSON(t, resp, &results)
212210
assert.NotEmpty(t, results.Data)
@@ -217,8 +215,6 @@ func TestTeamSearch(t *testing.T) {
217215
// no access if not organization member
218216
user5 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
219217
session = loginUser(t, user5.Name)
220-
csrf = GetCSRF(t, session, "/"+org.Name)
221218
req = NewRequestf(t, "GET", "/org/%s/teams/-/search?q=%s", org.Name, "team")
222-
req.Header.Add("X-Csrf-Token", csrf)
223219
session.MakeRequest(t, req, http.StatusNotFound)
224220
}

0 commit comments

Comments
 (0)