2017-07-12 10:58:52 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-07-12 10:58:52 -04:00
|
|
|
|
2022-09-02 15:18:23 -04:00
|
|
|
package integration
|
2017-07-12 10:58:52 -04:00
|
|
|
|
|
|
|
import (
|
2024-02-22 17:37:21 -05:00
|
|
|
"context"
|
2024-02-24 02:27:47 -05:00
|
|
|
"html"
|
2017-07-12 10:58:52 -04:00
|
|
|
"net/http"
|
2024-02-22 17:37:21 -05:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-07-12 10:58:52 -04:00
|
|
|
"testing"
|
2024-02-22 17:37:21 -05:00
|
|
|
"time"
|
2017-07-12 10:58:52 -04:00
|
|
|
|
2021-11-16 03:53:21 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-02-22 17:37:21 -05:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2022-09-02 15:18:23 -04:00
|
|
|
"code.gitea.io/gitea/tests"
|
2017-07-12 10:58:52 -04:00
|
|
|
|
2024-02-22 17:37:21 -05:00
|
|
|
gogit "github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
2017-07-12 10:58:52 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestXSSUserFullName(t *testing.T) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2022-08-15 22:22:25 -04:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2024-02-24 02:27:47 -05:00
|
|
|
const fullName = `name & <script class="evil">alert('xss');</script>`
|
2017-07-12 10:58:52 -04:00
|
|
|
|
|
|
|
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 01:20:30 -05:00
|
|
|
"language": "en-US",
|
2017-07-12 10:58:52 -04:00
|
|
|
})
|
2022-03-23 00:54:07 -04:00
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
2017-07-12 10:58:52 -04: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())
|
2024-02-24 02:27:47 -05:00
|
|
|
htmlCode, err := htmlDoc.doc.Find("div.content").Find(".header.text.center").Html()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, html.EscapeString(fullName), htmlCode)
|
2017-07-12 10:58:52 -04:00
|
|
|
}
|
2024-02-22 17:37:21 -05:00
|
|
|
|
|
|
|
func TestXSSWikiLastCommitInfo(t *testing.T) {
|
|
|
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
|
|
|
dstPath := t.TempDir()
|
2024-02-24 02:27:47 -05:00
|
|
|
cloneWikiURL, err := url.Parse(u.String() + "user2/repo1.wiki.git")
|
2024-02-22 17:37:21 -05:00
|
|
|
assert.NoError(t, err)
|
2024-02-24 02:27:47 -05:00
|
|
|
cloneWikiURL.User = url.UserPassword("user2", userPassword)
|
|
|
|
assert.NoError(t, git.CloneWithArgs(context.Background(), git.AllowLFSFiltersArgs(), cloneWikiURL.String(), dstPath, git.CloneRepoOptions{}))
|
2024-02-22 17:37:21 -05: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)
|
2024-02-24 02:27:47 -05:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
2024-02-22 17:37:21 -05:00
|
|
|
}
|
|
|
|
w, err := gitRepo.Worktree()
|
2024-02-24 02:27:47 -05:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
2024-02-22 17:37:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
filename := filepath.Join(dstPath, "Home.md")
|
2024-02-24 02:27:47 -05:00
|
|
|
err = os.WriteFile(filename, []byte("dummy content"), 0o644)
|
2024-02-22 17:37:21 -05:00
|
|
|
if !assert.NoError(t, err) {
|
2024-02-24 02:27:47 -05:00
|
|
|
return
|
2024-02-22 17:37:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Add("Home.md")
|
|
|
|
if !assert.NoError(t, err) {
|
2024-02-24 02:27:47 -05:00
|
|
|
return
|
2024-02-22 17:37:21 -05:00
|
|
|
}
|
|
|
|
|
2024-02-24 02:27:47 -05:00
|
|
|
_, err = w.Commit("dummy message", &gogit.CommitOptions{
|
2024-02-22 17:37:21 -05:00
|
|
|
Author: &object.Signature{
|
2024-02-24 02:27:47 -05:00
|
|
|
Name: `foo<script class="evil">alert('xss');</script>bar`,
|
2024-02-22 17:37:21 -05:00
|
|
|
Email: "valid@example.org",
|
2024-02-24 02:27:47 -05:00
|
|
|
When: time.Date(2001, time.January, 31, 0, 0, 0, 0, time.UTC),
|
2024-02-22 17:37:21 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if !assert.NoError(t, err) {
|
2024-02-24 02:27:47 -05:00
|
|
|
return
|
2024-02-22 17:37:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push.
|
2024-02-24 02:27:47 -05:00
|
|
|
_, _, err = git.NewCommand(git.DefaultContext, "push").AddArguments("origin", "master").RunStdString(&git.RunOpts{Dir: dstPath})
|
2024-02-22 17:37:21 -05:00
|
|
|
assert.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)
|
2024-02-24 02:27:47 -05:00
|
|
|
htmlCode, err := htmlDoc.Find(".ui.sub.header").Html()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, `foo<script class="evil">alert('xss');</script>bar edited this page <relative-time class="time-since" prefix="" tense="past" datetime="2001-01-31T00:00:00Z" data-tooltip-content="" data-tooltip-interactive="true">2001-01-31 00:00:00 +00:00</relative-time>`, strings.TrimSpace(htmlCode))
|
2024-02-22 17:37:21 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
// 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)
|
2024-02-24 02:27:47 -05:00
|
|
|
htmlCode, err := htmlDoc.Find(".ui.sub.header").Html()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, `foo<script class="evil">alert('xss');</script>bar edited this page <relative-time class="time-since" prefix="" tense="past" datetime="2001-01-31T00:00:00Z" data-tooltip-content="" data-tooltip-interactive="true">2001-01-31 00:00:00 +00:00</relative-time>`, strings.TrimSpace(htmlCode))
|
2024-02-22 17:37:21 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|