loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request '[TESTS] verify sanitization of names to prevent XSS' (#2435) from earl-warren/forgejo:wip-xss into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2435
Reviewed-by: Gusted <gusted@noreply.codeberg.org>

+108 -1
+1 -1
templates/repo/issue/view_content/comments.tmpl
··· 619 619 {{else}} 620 620 {{$reviewerName = .Review.OriginalAuthor}} 621 621 {{end}} 622 - {{ctx.Locale.Tr "repo.issues.review.dismissed" $reviewerName $createdStr | Safe}} 622 + <span class="dismissed-message">{{ctx.Locale.Tr "repo.issues.review.dismissed" $reviewerName $createdStr | Safe}}</span> 623 623 </span> 624 624 </div> 625 625 {{if .Content}}
+9
tests/integration/fixtures/TestXSSReviewDismissed/comment.yml
··· 1 + - 2 + id: 1000 3 + type: 32 # dismiss review 4 + poster_id: 2 5 + issue_id: 2 # in repo_id 1 6 + content: "XSS time!" 7 + review_id: 1000 8 + created_unix: 1700000000 9 + updated_unix: 1700000000
+8
tests/integration/fixtures/TestXSSReviewDismissed/review.yml
··· 1 + - 2 + id: 1000 3 + type: 1 4 + issue_id: 2 5 + original_author: "Otto <script class='evil'>alert('Oh no!')</script>" 6 + content: "XSS time!" 7 + updated_unix: 1700000000 8 + created_unix: 1700000000
+90
tests/integration/xss_test.go
··· 4 4 package integration 5 5 6 6 import ( 7 + "context" 8 + "fmt" 7 9 "net/http" 10 + "net/url" 11 + "os" 12 + "path/filepath" 8 13 "testing" 14 + "time" 9 15 16 + issues_model "code.gitea.io/gitea/models/issues" 10 17 "code.gitea.io/gitea/models/unittest" 11 18 user_model "code.gitea.io/gitea/models/user" 19 + "code.gitea.io/gitea/modules/git" 12 20 "code.gitea.io/gitea/tests" 13 21 22 + gogit "github.com/go-git/go-git/v5" 23 + "github.com/go-git/go-git/v5/plumbing/object" 14 24 "github.com/stretchr/testify/assert" 25 + "github.com/stretchr/testify/require" 15 26 ) 16 27 17 28 func TestXSSUserFullName(t *testing.T) { ··· 37 48 htmlDoc.doc.Find("div.content").Find(".header.text.center").Text(), 38 49 ) 39 50 } 51 + 52 + func TestXSSWikiLastCommitInfo(t *testing.T) { 53 + onGiteaRun(t, func(t *testing.T, u *url.URL) { 54 + // Prepare the environment. 55 + dstPath := t.TempDir() 56 + r := fmt.Sprintf("%suser2/repo1.wiki.git", u.String()) 57 + u, err := url.Parse(r) 58 + assert.NoError(t, err) 59 + u.User = url.UserPassword("user2", userPassword) 60 + assert.NoError(t, git.CloneWithArgs(context.Background(), git.AllowLFSFiltersArgs(), u.String(), dstPath, git.CloneRepoOptions{})) 61 + 62 + // Use go-git here, because using git wouldn't work, it has code to remove 63 + // `<`, `>` and `\n` in user names. Even though this is permitted and 64 + // wouldn't result in a error by a Git server. 65 + gitRepo, err := gogit.PlainOpen(dstPath) 66 + require.NoError(t, err) 67 + 68 + w, err := gitRepo.Worktree() 69 + require.NoError(t, err) 70 + 71 + filename := filepath.Join(dstPath, "Home.md") 72 + err = os.WriteFile(filename, []byte("Oh, a XSS attack?"), 0o644) 73 + require.NoError(t, err) 74 + 75 + _, err = w.Add("Home.md") 76 + require.NoError(t, err) 77 + 78 + _, err = w.Commit("Yay XSS", &gogit.CommitOptions{ 79 + Author: &object.Signature{ 80 + Name: `Gusted<script class="evil">alert('Oh no!');</script>`, 81 + Email: "valid@example.org", 82 + When: time.Date(2024, time.January, 31, 0, 0, 0, 0, time.UTC), 83 + }, 84 + }) 85 + require.NoError(t, err) 86 + 87 + // Push. 88 + _, _, err = git.NewCommand(git.DefaultContext, "push").AddArguments(git.ToTrustedCmdArgs([]string{"origin", "master"})...).RunStdString(&git.RunOpts{Dir: dstPath}) 89 + require.NoError(t, err) 90 + 91 + // Check on page view. 92 + t.Run("Page view", func(t *testing.T) { 93 + defer tests.PrintCurrentTest(t)() 94 + 95 + req := NewRequest(t, http.MethodGet, "/user2/repo1/wiki/Home") 96 + resp := MakeRequest(t, req, http.StatusOK) 97 + htmlDoc := NewHTMLParser(t, resp.Body) 98 + 99 + htmlDoc.AssertElement(t, "script.evil", false) 100 + assert.Contains(t, htmlDoc.Find(".ui.sub.header").Text(), `Gusted<script class="evil">alert('Oh no!');</script> edited this page 2024-01-31`) 101 + }) 102 + 103 + // Check on revisions page. 104 + t.Run("Revision page", func(t *testing.T) { 105 + defer tests.PrintCurrentTest(t)() 106 + 107 + req := NewRequest(t, http.MethodGet, "/user2/repo1/wiki/Home?action=_revision") 108 + resp := MakeRequest(t, req, http.StatusOK) 109 + htmlDoc := NewHTMLParser(t, resp.Body) 110 + 111 + htmlDoc.AssertElement(t, "script.evil", false) 112 + assert.Contains(t, htmlDoc.Find(".ui.sub.header").Text(), `Gusted<script class="evil">alert('Oh no!');</script> edited this page 2024-01-31`) 113 + }) 114 + }) 115 + } 116 + 117 + func TestXSSReviewDismissed(t *testing.T) { 118 + defer tests.AddFixtures("tests/integration/fixtures/TestXSSReviewDismissed/")() 119 + defer tests.PrepareTestEnv(t)() 120 + 121 + review := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 1000}) 122 + 123 + req := NewRequest(t, http.MethodGet, fmt.Sprintf("/user2/repo1/pulls/%d", +review.IssueID)) 124 + resp := MakeRequest(t, req, http.StatusOK) 125 + htmlDoc := NewHTMLParser(t, resp.Body) 126 + 127 + htmlDoc.AssertElement(t, "script.evil", false) 128 + assert.Contains(t, htmlDoc.Find("#issuecomment-1000 .dismissed-message").Text(), `dismissed Otto <script class='evil'>alert('Oh no!')</script>’s review`) 129 + }