mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-10 00:26:14 +02:00
Restrict abbreviated hash matching to commit URLs
For short hashes (< 40 chars), only match URLs with /commit/ path segment to avoid false positives like src/main/20260304.txt being rendered as commit links. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
e73752bee6
commit
5c3929a442
2 files changed
+8
No files matched your search
@@ -75,6 +75,11 @@ func anyHashPatternExtract(s string) (ret anyHashPatternResult, ok bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ret.CommitID = s[m[pos]:m[pos+1]]
|
ret.CommitID = s[m[pos]:m[pos+1]]
|
||||||
|
// For abbreviated hashes (< 40 chars), only match commit URLs to avoid
|
||||||
|
// false positives like "http://host/org/repo/src/main/20260304.txt"
|
||||||
|
if len(ret.CommitID) < 40 && !strings.HasSuffix(s[ret.PosStart:m[pos]], "/commit/") {
|
||||||
|
return ret, false
|
||||||
|
}
|
||||||
pos += 2
|
pos += 2
|
||||||
|
|
||||||
ret.CommitExt = s[m[pos]:m[pos+1]]
|
ret.CommitExt = s[m[pos]:m[pos+1]]
|
||||||
|
|||||||
@@ -382,6 +382,9 @@ func TestRegExp_anySHA1Pattern(t *testing.T) {
|
|||||||
"https://github.com/jquery/jquery/commit/0705be4": {
|
"https://github.com/jquery/jquery/commit/0705be4": {
|
||||||
CommitID: "0705be4",
|
CommitID: "0705be4",
|
||||||
},
|
},
|
||||||
|
// short hex strings in non-commit URLs should not match
|
||||||
|
"http://localhost:3000/foo/bar/src/main/20260304.txt": {},
|
||||||
|
"http://localhost:3000/foo/bar/blob/main/abcdef1/file": {},
|
||||||
"non-url": {},
|
"non-url": {},
|
||||||
"http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678?a=b#L1-L2": {
|
"http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678?a=b#L1-L2": {
|
||||||
CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
|
CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
|
||||||
|
|||||||
Reference in new issue
Block a user