Skip to content

Commit 72256c1

Browse files
Gustedzeripathtechknowlogick
authored
Prevent NPE on partial match of compare URL and allow short SHA1 compare URLs (#18472)
* Don't panic & allow shorter sha1 - Don't panic when the full regex isn't matched and allow the usage of a shorter sha1 being used. - Resolves #18471 * Update modules/markup/html.go Co-authored-by: zeripath <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent b2250d2 commit 72256c1

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

@@ -946,6 +946,13 @@ func comparePatternProcessor(ctx *RenderContext, node *html.Node) {
946946
return
947947
}
948948

949+
// Ensure that every group (m[0]...m[7]) has a match
950+
for i := 0; i < 8; i++ {
951+
if m[i] == -1 {
952+
return
953+
}
954+
}
955+
949956
urlFull := node.Data[m[0]:m[1]]
950957
text1 := base.ShortSha(node.Data[m[2]:m[3]])
951958
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
@@ -548,3 +548,16 @@ func TestFuzz(t *testing.T) {
548548

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

0 commit comments

Comments
 (0)