mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Multiple Escaping Improvements (#17551)
There are multiple places where Gitea does not properly escape URLs that it is building and there are multiple places where it builds urls when there is already a simpler function available to use this. This is an extensive PR attempting to fix these issues. 1. The first commit in this PR looks through all href, src and links in the Gitea codebase and has attempted to catch all the places where there is potentially incomplete escaping. 2. Whilst doing this we will prefer to use functions that create URLs over recreating them by hand. 3. All uses of strings should be directly escaped - even if they are not currently expected to contain escaping characters. The main benefit to doing this will be that we can consider relaxing the constraints on user names and reponames in future. 4. The next commit looks at escaping in the wiki and re-considers the urls that are used there. Using the improved escaping here wiki files containing '/'. (This implementation will currently still place all of the wiki files the root directory of the repo but this would not be difficult to change.) 5. The title generation in feeds is now properly escaped. 6. EscapePound is no longer needed - urls should be PathEscaped / QueryEscaped as necessary but then re-escaped with Escape when creating html with locales Signed-off-by: Andrew Thornton <art27@cantab.net> Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
@@ -70,6 +70,16 @@ type Context struct {
|
||||
Org *Organization
|
||||
}
|
||||
|
||||
// TrHTMLEscapeArgs runs Tr but pre-escapes all arguments with html.EscapeString.
|
||||
// This is useful if the locale message is intended to only produce HTML content.
|
||||
func (ctx *Context) TrHTMLEscapeArgs(msg string, args ...string) string {
|
||||
trArgs := make([]interface{}, len(args))
|
||||
for i, arg := range args {
|
||||
trArgs[i] = html.EscapeString(arg)
|
||||
}
|
||||
return ctx.Tr(msg, trArgs...)
|
||||
}
|
||||
|
||||
// GetData returns the data
|
||||
func (ctx *Context) GetData() map[string]interface{} {
|
||||
return ctx.Data
|
||||
@@ -120,9 +130,9 @@ func RedirectToUser(ctx *Context, userName string, redirectUserID int64) {
|
||||
}
|
||||
|
||||
redirectPath := strings.Replace(
|
||||
ctx.Req.URL.Path,
|
||||
userName,
|
||||
user.Name,
|
||||
ctx.Req.URL.EscapedPath(),
|
||||
url.PathEscape(userName),
|
||||
url.PathEscape(user.Name),
|
||||
1,
|
||||
)
|
||||
if ctx.Req.URL.RawQuery != "" {
|
||||
|
@@ -41,10 +41,10 @@ var IssueTemplateDirCandidates = []string{
|
||||
|
||||
// PullRequest contains information to make a pull request
|
||||
type PullRequest struct {
|
||||
BaseRepo *models.Repository
|
||||
Allowed bool
|
||||
SameRepo bool
|
||||
HeadInfo string // [<user>:]<branch>
|
||||
BaseRepo *models.Repository
|
||||
Allowed bool
|
||||
SameRepo bool
|
||||
HeadInfoSubURL string // [<user>:]<branch> url segment
|
||||
}
|
||||
|
||||
// Repository contains information to operate a repository
|
||||
@@ -189,11 +189,11 @@ func (r *Repository) GetCommitGraphsCount(hidePRRefs bool, branches []string, fi
|
||||
func (r *Repository) BranchNameSubURL() string {
|
||||
switch {
|
||||
case r.IsViewBranch:
|
||||
return "branch/" + r.BranchName
|
||||
return "branch/" + util.PathEscapeSegments(r.BranchName)
|
||||
case r.IsViewTag:
|
||||
return "tag/" + r.BranchName
|
||||
return "tag/" + util.PathEscapeSegments(r.BranchName)
|
||||
case r.IsViewCommit:
|
||||
return "commit/" + r.BranchName
|
||||
return "commit/" + util.PathEscapeSegments(r.BranchName)
|
||||
}
|
||||
log.Error("Unknown view type for repo: %v", r)
|
||||
return ""
|
||||
@@ -321,9 +321,9 @@ func RedirectToRepo(ctx *Context, redirectRepoID int64) {
|
||||
}
|
||||
|
||||
redirectPath := strings.Replace(
|
||||
ctx.Req.URL.Path,
|
||||
fmt.Sprintf("%s/%s", ownerName, previousRepoName),
|
||||
repo.FullName(),
|
||||
ctx.Req.URL.EscapedPath(),
|
||||
url.PathEscape(ownerName)+"/"+url.PathEscape(previousRepoName),
|
||||
url.PathEscape(repo.OwnerName)+"/"+url.PathEscape(repo.Name),
|
||||
1,
|
||||
)
|
||||
if ctx.Req.URL.RawQuery != "" {
|
||||
@@ -588,7 +588,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
|
||||
ctx.Data["BaseRepo"] = repo.BaseRepo
|
||||
ctx.Repo.PullRequest.BaseRepo = repo.BaseRepo
|
||||
ctx.Repo.PullRequest.Allowed = canPush
|
||||
ctx.Repo.PullRequest.HeadInfo = ctx.Repo.Owner.Name + ":" + ctx.Repo.BranchName
|
||||
ctx.Repo.PullRequest.HeadInfoSubURL = url.PathEscape(ctx.Repo.Owner.Name) + ":" + util.PathEscapeSegments(ctx.Repo.BranchName)
|
||||
} else if repo.AllowsPulls() {
|
||||
// Or, this is repository accepts pull requests between branches.
|
||||
canCompare = true
|
||||
@@ -596,7 +596,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
|
||||
ctx.Repo.PullRequest.BaseRepo = repo
|
||||
ctx.Repo.PullRequest.Allowed = canPush
|
||||
ctx.Repo.PullRequest.SameRepo = true
|
||||
ctx.Repo.PullRequest.HeadInfo = ctx.Repo.BranchName
|
||||
ctx.Repo.PullRequest.HeadInfoSubURL = util.PathEscapeSegments(ctx.Repo.BranchName)
|
||||
}
|
||||
ctx.Data["CanCompareOrPull"] = canCompare
|
||||
ctx.Data["PullRequestCtx"] = ctx.Repo.PullRequest
|
||||
@@ -621,7 +621,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
|
||||
|
||||
if ctx.FormString("go-get") == "1" {
|
||||
ctx.Data["GoGetImport"] = ComposeGoGetImport(owner.Name, repo.Name)
|
||||
prefix := setting.AppURL + path.Join(owner.Name, repo.Name, "src", "branch", ctx.Repo.BranchName)
|
||||
prefix := repo.HTMLURL() + "/src/branch/" + util.PathEscapeSegments(ctx.Repo.BranchName)
|
||||
ctx.Data["GoDocDirectory"] = prefix + "{/dir}"
|
||||
ctx.Data["GoDocFile"] = prefix + "{/dir}/{file}#L{line}"
|
||||
}
|
||||
@@ -810,7 +810,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
|
||||
if isRenamedBranch && has {
|
||||
renamedBranchName := ctx.Data["RenamedBranchName"].(string)
|
||||
ctx.Flash.Info(ctx.Tr("repo.branch.renamed", refName, renamedBranchName))
|
||||
link := strings.Replace(ctx.Req.RequestURI, refName, renamedBranchName, 1)
|
||||
link := setting.AppSubURL + strings.Replace(ctx.Req.URL.EscapedPath(), util.PathEscapeSegments(refName), util.PathEscapeSegments(renamedBranchName), 1)
|
||||
ctx.Redirect(link)
|
||||
return
|
||||
}
|
||||
@@ -845,7 +845,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
|
||||
// If short commit ID add canonical link header
|
||||
if len(refName) < 40 {
|
||||
ctx.Header().Set("Link", fmt.Sprintf("<%s>; rel=\"canonical\"",
|
||||
util.URLJoin(setting.AppURL, strings.Replace(ctx.Req.URL.RequestURI(), refName, ctx.Repo.Commit.ID.String(), 1))))
|
||||
util.URLJoin(setting.AppURL, strings.Replace(ctx.Req.URL.RequestURI(), util.PathEscapeSegments(refName), url.PathEscape(ctx.Repo.Commit.ID.String()), 1))))
|
||||
}
|
||||
} else {
|
||||
if len(ignoreNotExistErr) > 0 && ignoreNotExistErr[0] {
|
||||
@@ -857,11 +857,13 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
|
||||
|
||||
if refType == RepoRefLegacy {
|
||||
// redirect from old URL scheme to new URL scheme
|
||||
prefix := strings.TrimPrefix(setting.AppSubURL+strings.TrimSuffix(ctx.Req.URL.Path, ctx.Params("*")), ctx.Repo.RepoLink)
|
||||
|
||||
ctx.Redirect(path.Join(
|
||||
setting.AppSubURL,
|
||||
strings.TrimSuffix(ctx.Req.URL.Path, ctx.Params("*")),
|
||||
ctx.Repo.RepoLink,
|
||||
util.PathEscapeSegments(prefix),
|
||||
ctx.Repo.BranchNameSubURL(),
|
||||
ctx.Repo.TreePath))
|
||||
util.PathEscapeSegments(ctx.Repo.TreePath)))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
@@ -126,7 +127,7 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string]
|
||||
for i := 0; i < commit.ParentCount(); i++ {
|
||||
sha, _ := commit.ParentID(i)
|
||||
apiParents[i] = &api.CommitMeta{
|
||||
URL: repo.APIURL() + "/git/commits/" + sha.String(),
|
||||
URL: repo.APIURL() + "/git/commits/" + url.PathEscape(sha.String()),
|
||||
SHA: sha.String(),
|
||||
}
|
||||
}
|
||||
@@ -147,13 +148,13 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string]
|
||||
|
||||
return &api.Commit{
|
||||
CommitMeta: &api.CommitMeta{
|
||||
URL: repo.APIURL() + "/git/commits/" + commit.ID.String(),
|
||||
URL: repo.APIURL() + "/git/commits/" + url.PathEscape(commit.ID.String()),
|
||||
SHA: commit.ID.String(),
|
||||
Created: commit.Committer.When,
|
||||
},
|
||||
HTMLURL: repo.HTMLURL() + "/commit/" + commit.ID.String(),
|
||||
HTMLURL: repo.HTMLURL() + "/commit/" + url.PathEscape(commit.ID.String()),
|
||||
RepoCommit: &api.RepoCommit{
|
||||
URL: repo.APIURL() + "/git/commits/" + commit.ID.String(),
|
||||
URL: repo.APIURL() + "/git/commits/" + url.PathEscape(commit.ID.String()),
|
||||
Author: &api.CommitUser{
|
||||
Identity: api.Identity{
|
||||
Name: commit.Author.Name,
|
||||
@@ -170,7 +171,7 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string]
|
||||
},
|
||||
Message: commit.Message(),
|
||||
Tree: &api.CommitMeta{
|
||||
URL: repo.APIURL() + "/git/trees/" + commit.ID.String(),
|
||||
URL: repo.APIURL() + "/git/trees/" + url.PathEscape(commit.ID.String()),
|
||||
SHA: commit.ID.String(),
|
||||
Created: commit.Committer.When,
|
||||
},
|
||||
|
@@ -6,6 +6,7 @@ package convert
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
@@ -191,7 +192,7 @@ func ToLabel(label *models.Label, repo *models.Repository, org *models.User) *ap
|
||||
}
|
||||
} else { // BelongsToOrg
|
||||
if org != nil {
|
||||
result.URL = fmt.Sprintf("%sapi/v1/orgs/%s/labels/%d", setting.AppURL, org.Name, label.ID)
|
||||
result.URL = fmt.Sprintf("%sapi/v1/orgs/%s/labels/%d", setting.AppURL, url.PathEscape(org.Name), label.ID)
|
||||
} else {
|
||||
log.Error("ToLabel did not get org to calculate url for label with id '%d'", label.ID)
|
||||
}
|
||||
|
@@ -5,6 +5,8 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
@@ -58,7 +60,7 @@ func ToNotificationThread(n *models.Notification) *api.NotificationThread {
|
||||
}
|
||||
}
|
||||
case models.NotificationSourceCommit:
|
||||
url := n.Repository.HTMLURL() + "/commit/" + n.CommitID
|
||||
url := n.Repository.HTMLURL() + "/commit/" + url.PathEscape(n.CommitID)
|
||||
result.Subject = &api.NotificationSubject{
|
||||
Type: api.NotifySubjectCommit,
|
||||
Title: n.CommitID,
|
||||
|
@@ -11,6 +11,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
// ObjectCache provides thread-safe cache operations.
|
||||
@@ -92,7 +94,7 @@ func RefEndName(refStr string) string {
|
||||
|
||||
// RefURL returns the absolute URL for a ref in a repository
|
||||
func RefURL(repoURL, ref string) string {
|
||||
refName := RefEndName(ref)
|
||||
refName := util.PathEscapeSegments(RefEndName(ref))
|
||||
switch {
|
||||
case strings.HasPrefix(ref, BranchPrefix):
|
||||
return repoURL + "/src/branch/" + refName
|
||||
|
@@ -7,6 +7,7 @@ package repofiles
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -175,7 +176,7 @@ func UpdateIssuesCommit(doer *models.User, repo *models.Repository, commits []*r
|
||||
continue
|
||||
}
|
||||
|
||||
message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, repo.Link(), c.Sha1, html.EscapeString(strings.SplitN(c.Message, "\n", 2)[0]))
|
||||
message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, html.EscapeString(repo.Link()), html.EscapeString(url.PathEscape(c.Sha1)), html.EscapeString(strings.SplitN(c.Message, "\n", 2)[0]))
|
||||
if err = models.CreateRefComment(doer, refRepo, refIssue, message, c.Sha1); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -5,6 +5,8 @@
|
||||
package repofiles
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
@@ -31,7 +33,7 @@ func GetBlobBySHA(repo *models.Repository, sha string) (*api.GitBlobResponse, er
|
||||
}
|
||||
return &api.GitBlobResponse{
|
||||
SHA: gitBlob.ID.String(),
|
||||
URL: repo.APIURL() + "/git/blobs/" + gitBlob.ID.String(),
|
||||
URL: repo.APIURL() + "/git/blobs/" + url.PathEscape(gitBlob.ID.String()),
|
||||
Size: gitBlob.Size(),
|
||||
Encoding: "base64",
|
||||
Content: content,
|
||||
|
@@ -36,19 +36,19 @@ func GetFileCommitResponse(repo *models.Repository, commit *git.Commit) (*api.Fi
|
||||
if commit == nil {
|
||||
return nil, fmt.Errorf("commit cannot be nil")
|
||||
}
|
||||
commitURL, _ := url.Parse(repo.APIURL() + "/git/commits/" + commit.ID.String())
|
||||
commitTreeURL, _ := url.Parse(repo.APIURL() + "/git/trees/" + commit.Tree.ID.String())
|
||||
commitURL, _ := url.Parse(repo.APIURL() + "/git/commits/" + url.PathEscape(commit.ID.String()))
|
||||
commitTreeURL, _ := url.Parse(repo.APIURL() + "/git/trees/" + url.PathEscape(commit.Tree.ID.String()))
|
||||
parents := make([]*api.CommitMeta, commit.ParentCount())
|
||||
for i := 0; i <= commit.ParentCount(); i++ {
|
||||
if parent, err := commit.Parent(i); err == nil && parent != nil {
|
||||
parentCommitURL, _ := url.Parse(repo.APIURL() + "/git/commits/" + parent.ID.String())
|
||||
parentCommitURL, _ := url.Parse(repo.APIURL() + "/git/commits/" + url.PathEscape(parent.ID.String()))
|
||||
parents[i] = &api.CommitMeta{
|
||||
SHA: parent.ID.String(),
|
||||
URL: parentCommitURL.String(),
|
||||
}
|
||||
}
|
||||
}
|
||||
commitHTMLURL, _ := url.Parse(repo.HTMLURL() + "/commit/" + commit.ID.String())
|
||||
commitHTMLURL, _ := url.Parse(repo.HTMLURL() + "/commit/" + url.PathEscape(commit.ID.String()))
|
||||
fileCommit := &api.FileCommitResponse{
|
||||
CommitMeta: api.CommitMeta{
|
||||
SHA: commit.ID.String(),
|
||||
|
@@ -6,6 +6,7 @@ package repofiles
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
@@ -28,7 +29,7 @@ func GetTreeBySHA(repo *models.Repository, sha string, page, perPage int, recurs
|
||||
}
|
||||
tree := new(api.GitTreeResponse)
|
||||
tree.SHA = gitTree.ResolvedID.String()
|
||||
tree.URL = repo.APIURL() + "/git/trees/" + tree.SHA
|
||||
tree.URL = repo.APIURL() + "/git/trees/" + url.PathEscape(tree.SHA)
|
||||
var entries git.Entries
|
||||
if recursive {
|
||||
entries, err = gitTree.ListEntriesRecursive()
|
||||
|
@@ -6,6 +6,7 @@ package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
@@ -81,7 +82,7 @@ func (pc *PushCommits) toAPIPayloadCommit(repoPath, repoLink string, commit *Pus
|
||||
return &api.PayloadCommit{
|
||||
ID: commit.Sha1,
|
||||
Message: commit.Message,
|
||||
URL: fmt.Sprintf("%s/commit/%s", repoLink, commit.Sha1),
|
||||
URL: fmt.Sprintf("%s/commit/%s", repoLink, url.PathEscape(commit.Sha1)),
|
||||
Author: &api.PayloadUser{
|
||||
Name: commit.AuthorName,
|
||||
Email: commit.AuthorEmail,
|
||||
|
@@ -139,17 +139,14 @@ func NewFuncMap() []template.FuncMap {
|
||||
}
|
||||
return str[start:end]
|
||||
},
|
||||
"EllipsisString": base.EllipsisString,
|
||||
"DiffTypeToStr": DiffTypeToStr,
|
||||
"DiffLineTypeToStr": DiffLineTypeToStr,
|
||||
"Sha1": Sha1,
|
||||
"ShortSha": base.ShortSha,
|
||||
"MD5": base.EncodeMD5,
|
||||
"ActionContent2Commits": ActionContent2Commits,
|
||||
"PathEscape": url.PathEscape,
|
||||
"EscapePound": func(str string) string {
|
||||
return strings.NewReplacer("%", "%25", "#", "%23", " ", "%20", "?", "%3F").Replace(str)
|
||||
},
|
||||
"EllipsisString": base.EllipsisString,
|
||||
"DiffTypeToStr": DiffTypeToStr,
|
||||
"DiffLineTypeToStr": DiffLineTypeToStr,
|
||||
"Sha1": Sha1,
|
||||
"ShortSha": base.ShortSha,
|
||||
"MD5": base.EncodeMD5,
|
||||
"ActionContent2Commits": ActionContent2Commits,
|
||||
"PathEscape": url.PathEscape,
|
||||
"PathEscapeSegments": util.PathEscapeSegments,
|
||||
"URLJoin": util.URLJoin,
|
||||
"RenderCommitMessage": RenderCommitMessage,
|
||||
@@ -742,7 +739,7 @@ func ReactionToEmoji(reaction string) template.HTML {
|
||||
if val != nil {
|
||||
return template.HTML(val.Emoji)
|
||||
}
|
||||
return template.HTML(fmt.Sprintf(`<img alt=":%s:" src="%s/assets/img/emoji/%s.png"></img>`, reaction, setting.StaticURLPrefix, reaction))
|
||||
return template.HTML(fmt.Sprintf(`<img alt=":%s:" src="%s/assets/img/emoji/%s.png"></img>`, reaction, setting.StaticURLPrefix, url.PathEscape(reaction)))
|
||||
}
|
||||
|
||||
// RenderNote renders the contents of a git-notes file as a commit message.
|
||||
|
@@ -6,6 +6,7 @@ package upload
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -83,7 +84,7 @@ func AddUploadContext(ctx *context.Context, uploadType string) {
|
||||
ctx.Data["UploadUrl"] = ctx.Repo.RepoLink + "/issues/attachments"
|
||||
ctx.Data["UploadRemoveUrl"] = ctx.Repo.RepoLink + "/issues/attachments/remove"
|
||||
if len(ctx.Params(":index")) > 0 {
|
||||
ctx.Data["UploadLinkUrl"] = ctx.Repo.RepoLink + "/issues/" + ctx.Params(":index") + "/attachments"
|
||||
ctx.Data["UploadLinkUrl"] = ctx.Repo.RepoLink + "/issues/" + url.PathEscape(ctx.Params(":index")) + "/attachments"
|
||||
} else {
|
||||
ctx.Data["UploadLinkUrl"] = ctx.Repo.RepoLink + "/issues/attachments"
|
||||
}
|
||||
|
Reference in New Issue
Block a user