-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Ensure GetCSRF
doesn't return an empty token
#32130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
2cbc7d6
74cf5a8
3b07e1f
10b2d63
9338b41
60b4259
58bc786
4c4233d
0bea254
7d4d180
58630f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ func generateImg() bytes.Buffer { | |
return buff | ||
} | ||
|
||
func createAttachment(t *testing.T, session *TestSession, repoURL, filename string, buff bytes.Buffer, expectedStatus int) string { | ||
func createAttachment(t *testing.T, session *TestSession, csrf string, repoURL, filename string, buff bytes.Buffer, expectedStatus int) string { | ||
body := &bytes.Buffer{} | ||
|
||
// Setup multi-part | ||
|
@@ -41,8 +41,6 @@ func createAttachment(t *testing.T, session *TestSession, repoURL, filename stri | |
err = writer.Close() | ||
assert.NoError(t, err) | ||
|
||
csrf := GetCSRF(t, session, repoURL) | ||
|
||
req := NewRequestWithBody(t, "POST", repoURL+"/issues/attachments", body) | ||
req.Header.Add("X-Csrf-Token", csrf) | ||
req.Header.Add("Content-Type", writer.FormDataContentType()) | ||
|
@@ -59,15 +57,14 @@ func createAttachment(t *testing.T, session *TestSession, repoURL, filename stri | |
func TestCreateAnonymousAttachment(t *testing.T) { | ||
defer tests.PrepareTestEnv(t)() | ||
session := emptyTestSession(t) | ||
// this test is not right because it just doesn't pass the CSRF validation | ||
createAttachment(t, session, "user2/repo1", "image.png", generateImg(), http.StatusBadRequest) | ||
createAttachment(t, session, GetCSRF(t, session, "/user/login"), "user2/repo1", "image.png", generateImg(), http.StatusSeeOther) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if someone looking at this code in two months knows why an attachment test calls a login endpoint... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It reads as this: when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the CSRF token is not action related why can't we have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is user-related. And I guess just no one has interest to work on #32130 (comment) , and no one has the explicitly said that "that FIXME could be removed and Gitea will always use a common CSRF token for current user" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what @KN4CK3R wants to say is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know it's user-related, that's the reason why there is the
If we decide to use action-related CSRF tokens the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would slow down many tests by using the extra request. In most cases, the test writer clearly knows what CSRF token they would like to use: anonymous, or a signed-in user. If you prefer to do so, I think it needs 2 functions:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (while these improvements could be in a separate PR but not in this PR's scope) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
func TestCreateIssueAttachment(t *testing.T) { | ||
defer tests.PrepareTestEnv(t)() | ||
const repoURL = "user2/repo1" | ||
session := loginUser(t, "user2") | ||
uuid := createAttachment(t, session, repoURL, "image.png", generateImg(), http.StatusOK) | ||
uuid := createAttachment(t, session, GetCSRF(t, session, repoURL), repoURL, "image.png", generateImg(), http.StatusOK) | ||
|
||
req := NewRequest(t, "GET", repoURL+"/issues/new") | ||
resp := session.MakeRequest(t, req, http.StatusOK) | ||
|
Uh oh!
There was an error while loading. Please reload this page.