2017-07-12 16:58:52 +02:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-07-12 16:58:52 +02:00
|
|
|
|
2022-09-02 21:18:23 +02:00
|
|
|
package integration
|
2017-07-12 16:58:52 +02:00
|
|
|
|
|
|
|
import (
|
2024-01-17 16:16:46 +01:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2017-07-12 16:58:52 +02:00
|
|
|
"net/http"
|
2024-01-17 16:16:46 +01:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-07-12 16:58:52 +02:00
|
|
|
"testing"
|
2024-01-17 16:16:46 +01:00
|
|
|
"time"
|
2017-07-12 16:58:52 +02:00
|
|
|
|
2024-01-18 00:18:39 +01:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-16 09:53:21 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-01-17 16:16:46 +01:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2022-09-02 21:18:23 +02:00
|
|
|
"code.gitea.io/gitea/tests"
|
2017-07-12 16:58:52 +02:00
|
|
|
|
2024-01-17 16:16:46 +01:00
|
|
|
gogit "github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
2017-07-12 16:58:52 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-01-17 16:16:46 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-07-12 16:58:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestXSSUserFullName(t *testing.T) {
|
2022-09-02 21:18:23 +02:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2022-08-16 04:22:25 +02:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2017-07-12 16:58:52 +02:00
|
|
|
const fullName = `name & <script class="evil">alert('Oh no!');</script>`
|
|
|
|
|
|
|
|
session := loginUser(t, user.Name)
|
|
|
|
req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
|
|
|
|
"_csrf": GetCSRF(t, session, "/user/settings"),
|
|
|
|
"name": user.Name,
|
|
|
|
"full_name": fullName,
|
|
|
|
"email": user.Email,
|
2020-12-04 07:20:30 +01:00
|
|
|
"language": "en-US",
|
2017-07-12 16:58:52 +02:00
|
|
|
})
|
2022-03-23 05:54:07 +01:00
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
2017-07-12 16:58:52 +02:00
|
|
|
|
|
|
|
req = NewRequestf(t, "GET", "/%s", user.Name)
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
assert.EqualValues(t, 0, htmlDoc.doc.Find("script.evil").Length())
|
|
|
|
assert.EqualValues(t, fullName,
|
|
|
|
htmlDoc.doc.Find("div.content").Find(".header.text.center").Text(),
|
|
|
|
)
|
|
|
|
}
|
2024-01-17 16:16:46 +01:00
|
|
|
|
|
|
|
func TestXSSWikiLastCommitInfo(t *testing.T) {
|
|
|
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
|
|
|
// Prepare the environment.
|
|
|
|
dstPath := t.TempDir()
|
|
|
|
r := fmt.Sprintf("%suser2/repo1.wiki.git", u.String())
|
|
|
|
u, err := url.Parse(r)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2024-01-17 16:16:46 +01:00
|
|
|
u.User = url.UserPassword("user2", userPassword)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, git.CloneWithArgs(context.Background(), git.AllowLFSFiltersArgs(), u.String(), dstPath, git.CloneRepoOptions{}))
|
2024-01-17 16:16:46 +01:00
|
|
|
|
|
|
|
// Use go-git here, because using git wouldn't work, it has code to remove
|
|
|
|
// `<`, `>` and `\n` in user names. Even though this is permitted and
|
|
|
|
// wouldn't result in a error by a Git server.
|
|
|
|
gitRepo, err := gogit.PlainOpen(dstPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
w, err := gitRepo.Worktree()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
filename := filepath.Join(dstPath, "Home.md")
|
|
|
|
err = os.WriteFile(filename, []byte("Oh, a XSS attack?"), 0o644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = w.Add("Home.md")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = w.Commit("Yay XSS", &gogit.CommitOptions{
|
|
|
|
Author: &object.Signature{
|
|
|
|
Name: `Gusted<script class="evil">alert('Oh no!');</script>`,
|
|
|
|
Email: "valid@example.org",
|
|
|
|
When: time.Date(2024, time.January, 31, 0, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Push.
|
|
|
|
_, _, err = git.NewCommand(git.DefaultContext, "push").AddArguments(git.ToTrustedCmdArgs([]string{"origin", "master"})...).RunStdString(&git.RunOpts{Dir: dstPath})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check on page view.
|
|
|
|
t.Run("Page view", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
req := NewRequest(t, http.MethodGet, "/user2/repo1/wiki/Home")
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
|
|
|
|
htmlDoc.AssertElement(t, "script.evil", false)
|
|
|
|
assert.Contains(t, htmlDoc.Find(".ui.sub.header").Text(), `Gusted<script class="evil">alert('Oh no!');</script> edited this page 2024-01-31`)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Check on revisions page.
|
|
|
|
t.Run("Revision page", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
req := NewRequest(t, http.MethodGet, "/user2/repo1/wiki/Home?action=_revision")
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
|
|
|
|
htmlDoc.AssertElement(t, "script.evil", false)
|
|
|
|
assert.Contains(t, htmlDoc.Find(".ui.sub.header").Text(), `Gusted<script class="evil">alert('Oh no!');</script> edited this page 2024-01-31`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2024-01-18 00:18:39 +01:00
|
|
|
|
|
|
|
func TestXSSReviewDismissed(t *testing.T) {
|
|
|
|
defer tests.AddFixtures("tests/integration/fixtures/TestXSSReviewDismissed/")()
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
review := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 1000})
|
|
|
|
|
|
|
|
req := NewRequest(t, http.MethodGet, fmt.Sprintf("/user2/repo1/pulls/%d", +review.IssueID))
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
|
|
|
|
htmlDoc.AssertElement(t, "script.evil", false)
|
2024-02-27 14:39:59 +01:00
|
|
|
assert.Contains(t, htmlDoc.Find("#issuecomment-1000 .dismissed-message").Text(), `dismissed Otto <script class='evil'>alert('Oh no!')</script>'s review`)
|
2024-01-18 00:18:39 +01:00
|
|
|
}
|