Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check commit message hashes before making links #7713

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
e887f92
Check commit message hashes before making links
gary-kim Aug 2, 2019
d7788bf
Use gogit to check if commit exists
gary-kim Aug 2, 2019
582a180
Make code cleaner
gary-kim Aug 2, 2019
5852974
Use rev-parse to check if commit exists
gary-kim Aug 5, 2019
026c84a
Add and modify tests for checking hashes in html link rendering
gary-kim Aug 5, 2019
663bc8e
Merge branch 'master' into enh/noid/commit-message-link-check-commit-…
lunny Aug 5, 2019
74ed0fe
Merge branch 'master' into enh/noid/commit-message-link-check-commit-…
lunny Aug 5, 2019
28f561c
Return error in sha1CurrentPatternProcessor
gary-kim Aug 6, 2019
0240571
Import Gitea log module
gary-kim Aug 6, 2019
d6b0392
Revert "Return error in sha1CurrentPatternProcessor"
gary-kim Aug 6, 2019
950152e
Add debug logging to sha1CurrentPatternProcessor
gary-kim Aug 7, 2019
953829d
Merge branch 'master' into enh/noid/commit-message-link-check-commit-…
zeripath Aug 7, 2019
ca446de
Merge branch 'master' into enh/noid/commit-message-link-check-commit-…
lafriks Aug 10, 2019
4e1795f
Merge branch 'master' into enh/noid/commit-message-link-check-commit-…
lunny Aug 11, 2019
74b25b4
Merge branch 'master' into enh/noid/commit-message-link-check-commit-…
lunny Aug 11, 2019
a137af0
Merge branch 'master' into enh/noid/commit-message-link-check-commit-…
gary-kim Aug 13, 2019
dccd86d
Merge branch 'master' into enh/noid/commit-message-link-check-commit-…
lunny Aug 14, 2019
65e9ee0
Merge branch 'master' into enh/noid/commit-message-link-check-commit-…
lunny Aug 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,9 @@ func (repo *Repository) mustOwnerName(e Engine) string {
func (repo *Repository) ComposeMetas() map[string]string {
if repo.ExternalMetas == nil {
repo.ExternalMetas = map[string]string{
"user": repo.MustOwner().Name,
"repo": repo.Name,
"user": repo.MustOwner().Name,
"repo": repo.Name,
"repoPath": repo.RepoPath(),
}
unit, err := repo.GetUnit(UnitTypeExternalTracker)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"

Expand Down Expand Up @@ -646,6 +647,9 @@ func fullSha1PatternProcessor(ctx *postProcessCtx, node *html.Node) {
// sha1CurrentPatternProcessor renders SHA1 strings to corresponding links that
// are assumed to be in the same repository.
func sha1CurrentPatternProcessor(ctx *postProcessCtx, node *html.Node) {
if ctx.metas == nil || ctx.metas["user"] == "" || ctx.metas["repo"] == "" || ctx.metas["repoPath"] == "" {
return
}
m := sha1CurrentPattern.FindStringSubmatchIndex(node.Data)
if m == nil {
return
Expand All @@ -657,6 +661,12 @@ func sha1CurrentPatternProcessor(ctx *postProcessCtx, node *html.Node) {
// but that is not always the case.
// Although unlikely, deadbeef and 1234567 are valid short forms of SHA1 hash
// as used by git and github for linking and thus we have to do similar.
// Because of this, we check to make sure that a matched hash is actually
// a commit in the repository before making it a link.
if _, err := git.NewCommand("rev-parse", "--verify", hash).RunInDirBytes(ctx.metas["repoPath"]); err != nil {
return
lafriks marked this conversation as resolved.
Show resolved Hide resolved
}

replaceContent(node, m[2], m[3],
createCodeLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "commit", hash), base.ShortSha(hash)))
}
Expand Down
22 changes: 12 additions & 10 deletions modules/markup/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
)

var localMetas = map[string]string{
"user": "gogits",
"repo": "gogs",
"user": "gogits",
"repo": "gogs",
"repoPath": "../../integrations/gitea-repositories-meta/user13/repo11.git/",
}

func TestRender_Commits(t *testing.T) {
Expand All @@ -30,19 +31,20 @@ func TestRender_Commits(t *testing.T) {
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}

var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
var sha = "65f1bf27bc3bf70f64657658635e66094edbcb4d"
var commit = util.URLJoin(AppSubURL, "commit", sha)
var subtree = util.URLJoin(commit, "src")
var tree = strings.Replace(subtree, "/commit/", "/tree/", -1)

test(sha, `<p><a href="`+commit+`" rel="nofollow"><code>b6dd6210ea</code></a></p>`)
test(sha[:7], `<p><a href="`+commit[:len(commit)-(40-7)]+`" rel="nofollow"><code>b6dd621</code></a></p>`)
test(sha[:39], `<p><a href="`+commit[:len(commit)-(40-39)]+`" rel="nofollow"><code>b6dd6210ea</code></a></p>`)
test(commit, `<p><a href="`+commit+`" rel="nofollow"><code>b6dd6210ea</code></a></p>`)
test(tree, `<p><a href="`+tree+`" rel="nofollow"><code>b6dd6210ea/src</code></a></p>`)
test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow"><code>b6dd6210ea</code></a></p>`)
test(sha, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
test(sha[:7], `<p><a href="`+commit[:len(commit)-(40-7)]+`" rel="nofollow"><code>65f1bf2</code></a></p>`)
test(sha[:39], `<p><a href="`+commit[:len(commit)-(40-39)]+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
test(commit, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
test(tree, `<p><a href="`+tree+`" rel="nofollow"><code>65f1bf27bc/src</code></a></p>`)
test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
test("/home/gitea/"+sha, "<p>/home/gitea/"+sha+"</p>")

test("deadbeef", `<p>deadbeef</p>`)
test("d27ace93", `<p>d27ace93</p>`)
}

func TestRender_CrossReferences(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions modules/markup/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ const AppSubURL = AppURL + Repo + "/"

// these values should match the Repo const above
var localMetas = map[string]string{
"user": "gogits",
"repo": "gogs",
"user": "gogits",
"repo": "gogs",
"repoPath": "../../../integrations/gitea-repositories-meta/user13/repo11.git/",
}

func TestRender_StandardLinks(t *testing.T) {
Expand Down Expand Up @@ -103,7 +104,7 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
<li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li>
</ul>

<p>See commit <a href="http://localhost:3000/gogits/gogs/commit/fc7f44dadf" rel="nofollow"><code>fc7f44dadf</code></a></p>
<p>See commit <a href="http://localhost:3000/gogits/gogs/commit/65f1bf27bc" rel="nofollow"><code>65f1bf27bc</code></a></p>

<p>Ideas and codes</p>

Expand Down Expand Up @@ -194,7 +195,7 @@ var sameCases = []string{
- [[Links, Language bindings, Engine bindings|Links]]
- [[Tips]]

See commit fc7f44dadf
See commit 65f1bf27bc

Ideas and codes

Expand Down