Skip to content

Commit a88564b

Browse files
committed
Check that hashes are commits before making them links
Signed-off-by: Gary Kim <[email protected]>
1 parent 195cb4f commit a88564b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

models/repo.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,9 @@ func (repo *Repository) mustOwnerName(e Engine) string {
508508
func (repo *Repository) ComposeMetas() map[string]string {
509509
if repo.ExternalMetas == nil {
510510
repo.ExternalMetas = map[string]string{
511-
"user": repo.MustOwner().Name,
512-
"repo": repo.Name,
511+
"user": repo.MustOwner().Name,
512+
"repo": repo.Name,
513+
"repoPath": repo.RepoPath(),
513514
}
514515
unit, err := repo.GetUnit(UnitTypeExternalTracker)
515516
if err != nil {

modules/markup/html.go

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414

1515
"code.gitea.io/gitea/modules/base"
16+
"code.gitea.io/gitea/modules/git"
1617
"code.gitea.io/gitea/modules/setting"
1718
"code.gitea.io/gitea/modules/util"
1819

@@ -657,6 +658,14 @@ func sha1CurrentPatternProcessor(ctx *postProcessCtx, node *html.Node) {
657658
// but that is not always the case.
658659
// Although unlikely, deadbeef and 1234567 are valid short forms of SHA1 hash
659660
// as used by git and github for linking and thus we have to do similar.
661+
// Because of this, we check to make sure that a matched hash is actually
662+
// a commit in the repository before making it a link.
663+
if ctx.metas["repoPath"] != "" {
664+
if _, err := git.NewCommand("log", "-1", hash).RunInDirBytes(ctx.metas["repoPath"]); err != nil {
665+
return
666+
}
667+
}
668+
660669
replaceContent(node, m[2], m[3],
661670
createCodeLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "commit", hash), base.ShortSha(hash)))
662671
}

0 commit comments

Comments
 (0)