Skip to content

Commit d5027b6

Browse files
Gustedzeripath
Gusted
andauthored
Prevent NPE on partial match of compare URL and allow short SHA1 compare URLs (#18472) (#18473)
* Don't panic & allow shorter sha1 (#18472) - Backport of #18472 * Improve comment Co-authored-by: Andrew Thornton <[email protected]> Co-authored-by: Andrew Thornton <[email protected]>
1 parent a044ec8 commit d5027b6

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

modules/markup/html.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var (
5555
anySHA1Pattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{40})(/[-+~_%.a-zA-Z0-9/]+)?(#[-+~_%.a-zA-Z0-9]+)?`)
5656

5757
// comparePattern matches "http://domain/org/repo/compare/COMMIT1...COMMIT2#hash"
58-
comparePattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{40})(\.\.\.?)([0-9a-f]{40})?(#[-+~_%.a-zA-Z0-9]+)?`)
58+
comparePattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{7,40})(\.\.\.?)([0-9a-f]{7,40})?(#[-+~_%.a-zA-Z0-9]+)?`)
5959

6060
validLinksPattern = regexp.MustCompile(`^[a-z][\w-]+://`)
6161

@@ -944,6 +944,13 @@ func comparePatternProcessor(ctx *RenderContext, node *html.Node) {
944944
return
945945
}
946946

947+
// Ensure that every group (m[0]...m[7]) has a match
948+
for i := 0; i < 8; i++ {
949+
if m[i] == -1 {
950+
return
951+
}
952+
}
953+
947954
urlFull := node.Data[m[0]:m[1]]
948955
text1 := base.ShortSha(node.Data[m[2]:m[3]])
949956
textDots := base.ShortSha(node.Data[m[4]:m[5]])

modules/markup/html_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,3 +546,16 @@ func TestFuzz(t *testing.T) {
546546

547547
assert.NoError(t, err)
548548
}
549+
550+
func TestIssue18471(t *testing.T) {
551+
data := `http://domain/org/repo/compare/783b039...da951ce`
552+
553+
var res strings.Builder
554+
err := PostProcess(&RenderContext{
555+
URLPrefix: "https://example.com",
556+
Metas: localMetas,
557+
}, strings.NewReader(data), &res)
558+
559+
assert.NoError(t, err)
560+
assert.Equal(t, res.String(), "<a href=\"http://domain/org/repo/compare/783b039...da951ce\" class=\"compare\"><code class=\"nohighlight\">783b039...da951ce</code></a>")
561+
}

0 commit comments

Comments
 (0)