diff options
author | Jonathan Tran <jon@allspice.io> | 2023-03-05 16:59:05 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-05 22:59:05 +0100 |
commit | 4de80392bc19628091c6b77cca9a82c00b9bd834 (patch) | |
tree | ab22850fbd8cf767828f43ff6028d298139fb875 /modules | |
parent | ea7f0d6fcfe9567cac8151536b36450de8645e88 (diff) | |
download | gitea-4de80392bc19628091c6b77cca9a82c00b9bd834.tar.gz gitea-4de80392bc19628091c6b77cca9a82c00b9bd834.zip |
Add context when rendering labels or emojis (#23281)
This branch continues the work of #23092 and attempts to rid the
codebase of any `nil` contexts when using a `RenderContext`.
Anything that renders markdown or does post processing may call
`markup.sha1CurrentPatternProcessor()`, and this runs
`git.OpenRepository()`, which needs a context. It will panic if the
context is `nil`. This branch attempts to _always_ include a context
when creating a `RenderContext` to prevent future crashes.
Co-authored-by: Kyle D <kdumontnu@gmail.com>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/csv/csv_test.go | 6 | ||||
-rw-r--r-- | modules/markup/console/console_test.go | 4 | ||||
-rw-r--r-- | modules/markup/csv/csv_test.go | 4 | ||||
-rw-r--r-- | modules/markup/html.go | 3 | ||||
-rw-r--r-- | modules/markup/html_internal_test.go | 43 | ||||
-rw-r--r-- | modules/markup/html_test.go | 14 | ||||
-rw-r--r-- | modules/markup/markdown/markdown_test.go | 23 | ||||
-rw-r--r-- | modules/markup/orgmode/orgmode_test.go | 4 | ||||
-rw-r--r-- | modules/templates/helper.go | 21 |
9 files changed, 91 insertions, 31 deletions
diff --git a/modules/csv/csv_test.go b/modules/csv/csv_test.go index c627597fa3..6b91a81fc4 100644 --- a/modules/csv/csv_test.go +++ b/modules/csv/csv_test.go @@ -11,6 +11,7 @@ import ( "strings" "testing" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/markup" "github.com/stretchr/testify/assert" @@ -229,7 +230,10 @@ John Doe john@doe.com This,note,had,a,lot,of,commas,to,test,delimiters`, } for n, c := range cases { - delimiter := determineDelimiter(&markup.RenderContext{RelativePath: c.filename}, []byte(decodeSlashes(t, c.csv))) + delimiter := determineDelimiter(&markup.RenderContext{ + Ctx: git.DefaultContext, + RelativePath: c.filename, + }, []byte(decodeSlashes(t, c.csv))) assert.EqualValues(t, c.expectedDelimiter, delimiter, "case %d: delimiter should be equal, expected '%c' got '%c'", n, c.expectedDelimiter, delimiter) } } diff --git a/modules/markup/console/console_test.go b/modules/markup/console/console_test.go index 506f86194c..2337d91ac5 100644 --- a/modules/markup/console/console_test.go +++ b/modules/markup/console/console_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/markup" "github.com/stretchr/testify/assert" @@ -23,7 +24,8 @@ func TestRenderConsole(t *testing.T) { canRender := render.CanRender("test", strings.NewReader(k)) assert.True(t, canRender) - err := render.Render(&markup.RenderContext{}, strings.NewReader(k), &buf) + err := render.Render(&markup.RenderContext{Ctx: git.DefaultContext}, + strings.NewReader(k), &buf) assert.NoError(t, err) assert.EqualValues(t, v, buf.String()) } diff --git a/modules/markup/csv/csv_test.go b/modules/markup/csv/csv_test.go index b9f5769be2..8c07184b21 100644 --- a/modules/markup/csv/csv_test.go +++ b/modules/markup/csv/csv_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/markup" "github.com/stretchr/testify/assert" @@ -23,7 +24,8 @@ func TestRenderCSV(t *testing.T) { for k, v := range kases { var buf strings.Builder - err := render.Render(&markup.RenderContext{}, strings.NewReader(k), &buf) + err := render.Render(&markup.RenderContext{Ctx: git.DefaultContext}, + strings.NewReader(k), &buf) assert.NoError(t, err) assert.EqualValues(t, v, buf.String()) } diff --git a/modules/markup/html.go b/modules/markup/html.go index bcb38f99eb..76fc54cf46 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -291,9 +291,10 @@ func RenderDescriptionHTML( // RenderEmoji for when we want to just process emoji and shortcodes // in various places it isn't already run through the normal markdown processor func RenderEmoji( + ctx *RenderContext, content string, ) (string, error) { - return renderProcessString(&RenderContext{}, emojiProcessors, content) + return renderProcessString(ctx, emojiProcessors, content) } var ( diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go index 6746c40d18..a048f1f527 100644 --- a/modules/markup/html_internal_test.go +++ b/modules/markup/html_internal_test.go @@ -9,6 +9,7 @@ import ( "strings" "testing" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -70,8 +71,13 @@ var localMetas = map[string]string{ func TestRender_IssueIndexPattern(t *testing.T) { // numeric: render inputs without valid mentions test := func(s string) { - testRenderIssueIndexPattern(t, s, s, &RenderContext{}) - testRenderIssueIndexPattern(t, s, s, &RenderContext{Metas: numericMetas}) + testRenderIssueIndexPattern(t, s, s, &RenderContext{ + Ctx: git.DefaultContext, + }) + testRenderIssueIndexPattern(t, s, s, &RenderContext{ + Ctx: git.DefaultContext, + Metas: numericMetas, + }) } // should not render anything when there are no mentions @@ -119,7 +125,10 @@ func TestRender_IssueIndexPattern2(t *testing.T) { links[i] = numericIssueLink(util.URLJoin(TestRepoURL, path), "ref-issue", index, marker) } expectedNil := fmt.Sprintf(expectedFmt, links...) - testRenderIssueIndexPattern(t, s, expectedNil, &RenderContext{Metas: localMetas}) + testRenderIssueIndexPattern(t, s, expectedNil, &RenderContext{ + Ctx: git.DefaultContext, + Metas: localMetas, + }) class := "ref-issue" if isExternal { @@ -130,7 +139,10 @@ func TestRender_IssueIndexPattern2(t *testing.T) { links[i] = numericIssueLink(prefix, class, index, marker) } expectedNum := fmt.Sprintf(expectedFmt, links...) - testRenderIssueIndexPattern(t, s, expectedNum, &RenderContext{Metas: numericMetas}) + testRenderIssueIndexPattern(t, s, expectedNum, &RenderContext{ + Ctx: git.DefaultContext, + Metas: numericMetas, + }) } // should render freestanding mentions @@ -164,7 +176,10 @@ func TestRender_IssueIndexPattern3(t *testing.T) { // alphanumeric: render inputs without valid mentions test := func(s string) { - testRenderIssueIndexPattern(t, s, s, &RenderContext{Metas: alphanumericMetas}) + testRenderIssueIndexPattern(t, s, s, &RenderContext{ + Ctx: git.DefaultContext, + Metas: alphanumericMetas, + }) } test("") test("this is a test") @@ -194,7 +209,10 @@ func TestRender_IssueIndexPattern4(t *testing.T) { links[i] = externalIssueLink("https://someurl.com/someUser/someRepo/", "ref-issue ref-external-issue", name) } expected := fmt.Sprintf(expectedFmt, links...) - testRenderIssueIndexPattern(t, s, expected, &RenderContext{Metas: alphanumericMetas}) + testRenderIssueIndexPattern(t, s, expected, &RenderContext{ + Ctx: git.DefaultContext, + Metas: alphanumericMetas, + }) } test("OTT-1234 test", "%s test", "OTT-1234") test("test T-12 issue", "test %s issue", "T-12") @@ -214,7 +232,10 @@ func TestRender_IssueIndexPattern5(t *testing.T) { } expected := fmt.Sprintf(expectedFmt, links...) - testRenderIssueIndexPattern(t, s, expected, &RenderContext{Metas: metas}) + testRenderIssueIndexPattern(t, s, expected, &RenderContext{ + Ctx: git.DefaultContext, + Metas: metas, + }) } test("abc ISSUE-123 def", "abc %s def", @@ -235,7 +256,10 @@ func TestRender_IssueIndexPattern5(t *testing.T) { []string{"ISSUE-123"}, ) - testRenderIssueIndexPattern(t, "will not match", "will not match", &RenderContext{Metas: regexpMetas}) + testRenderIssueIndexPattern(t, "will not match", "will not match", &RenderContext{ + Ctx: git.DefaultContext, + Metas: regexpMetas, + }) } func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *RenderContext) { @@ -255,6 +279,7 @@ func TestRender_AutoLink(t *testing.T) { test := func(input, expected string) { var buffer strings.Builder err := PostProcess(&RenderContext{ + Ctx: git.DefaultContext, URLPrefix: TestRepoURL, Metas: localMetas, }, strings.NewReader(input), &buffer) @@ -263,6 +288,7 @@ func TestRender_AutoLink(t *testing.T) { buffer.Reset() err = PostProcess(&RenderContext{ + Ctx: git.DefaultContext, URLPrefix: TestRepoURL, Metas: localMetas, IsWiki: true, @@ -292,6 +318,7 @@ func TestRender_FullIssueURLs(t *testing.T) { test := func(input, expected string) { var result strings.Builder err := postProcess(&RenderContext{ + Ctx: git.DefaultContext, URLPrefix: TestRepoURL, Metas: localMetas, }, []processor{fullIssuePatternProcessor}, strings.NewReader(input), &result) diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index aea1d92676..f8b84485c0 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -91,6 +91,7 @@ func TestRender_CrossReferences(t *testing.T) { test := func(input, expected string) { buffer, err := RenderString(&RenderContext{ + Ctx: git.DefaultContext, RelativePath: "a.md", URLPrefix: setting.AppSubURL, Metas: localMetas, @@ -135,6 +136,7 @@ func TestRender_links(t *testing.T) { test := func(input, expected string) { buffer, err := RenderString(&RenderContext{ + Ctx: git.DefaultContext, RelativePath: "a.md", URLPrefix: TestRepoURL, }, input) @@ -234,6 +236,7 @@ func TestRender_email(t *testing.T) { test := func(input, expected string) { res, err := RenderString(&RenderContext{ + Ctx: git.DefaultContext, RelativePath: "a.md", URLPrefix: TestRepoURL, }, input) @@ -292,6 +295,7 @@ func TestRender_emoji(t *testing.T) { test := func(input, expected string) { expected = strings.ReplaceAll(expected, "&", "&") buffer, err := RenderString(&RenderContext{ + Ctx: git.DefaultContext, RelativePath: "a.md", URLPrefix: TestRepoURL, }, input) @@ -355,11 +359,13 @@ func TestRender_ShortLinks(t *testing.T) { test := func(input, expected, expectedWiki string) { buffer, err := markdown.RenderString(&RenderContext{ + Ctx: git.DefaultContext, URLPrefix: tree, }, input) assert.NoError(t, err) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) buffer, err = markdown.RenderString(&RenderContext{ + Ctx: git.DefaultContext, URLPrefix: TestRepoURL, Metas: localMetas, IsWiki: true, @@ -461,12 +467,14 @@ func TestRender_RelativeImages(t *testing.T) { test := func(input, expected, expectedWiki string) { buffer, err := markdown.RenderString(&RenderContext{ + Ctx: git.DefaultContext, URLPrefix: tree, Metas: localMetas, }, input) assert.NoError(t, err) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) buffer, err = markdown.RenderString(&RenderContext{ + Ctx: git.DefaultContext, URLPrefix: TestRepoURL, Metas: localMetas, IsWiki: true, @@ -501,6 +509,7 @@ func Test_ParseClusterFuzz(t *testing.T) { var res strings.Builder err := PostProcess(&RenderContext{ + Ctx: git.DefaultContext, URLPrefix: "https://example.com", Metas: localMetas, }, strings.NewReader(data), &res) @@ -511,6 +520,7 @@ func Test_ParseClusterFuzz(t *testing.T) { res.Reset() err = PostProcess(&RenderContext{ + Ctx: git.DefaultContext, URLPrefix: "https://example.com", Metas: localMetas, }, strings.NewReader(data), &res) @@ -531,6 +541,7 @@ func TestIssue16020(t *testing.T) { var res strings.Builder err := PostProcess(&RenderContext{ + Ctx: git.DefaultContext, URLPrefix: "https://example.com", Metas: localMetas, }, strings.NewReader(data), &res) @@ -547,6 +558,7 @@ func BenchmarkEmojiPostprocess(b *testing.B) { for i := 0; i < b.N; i++ { var res strings.Builder err := PostProcess(&RenderContext{ + Ctx: git.DefaultContext, URLPrefix: "https://example.com", Metas: localMetas, }, strings.NewReader(data), &res) @@ -557,6 +569,7 @@ func BenchmarkEmojiPostprocess(b *testing.B) { func TestFuzz(t *testing.T) { s := "t/l/issues/8#/../../a" renderContext := RenderContext{ + Ctx: git.DefaultContext, URLPrefix: "https://example.com/go-gitea/gitea", Metas: map[string]string{ "user": "go-gitea", @@ -574,6 +587,7 @@ func TestIssue18471(t *testing.T) { var res strings.Builder err := PostProcess(&RenderContext{ + Ctx: git.DefaultContext, URLPrefix: "https://example.com", Metas: localMetas, }, strings.NewReader(data), &res) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index bb458a65c1..0c7650a5ff 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -52,12 +52,14 @@ func TestRender_StandardLinks(t *testing.T) { test := func(input, expected, expectedWiki string) { buffer, err := RenderString(&markup.RenderContext{ + Ctx: git.DefaultContext, URLPrefix: setting.AppSubURL, }, input) assert.NoError(t, err) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) buffer, err = RenderString(&markup.RenderContext{ + Ctx: git.DefaultContext, URLPrefix: setting.AppSubURL, IsWiki: true, }, input) @@ -81,6 +83,7 @@ func TestRender_Images(t *testing.T) { test := func(input, expected string) { buffer, err := RenderString(&markup.RenderContext{ + Ctx: git.DefaultContext, URLPrefix: setting.AppSubURL, }, input) assert.NoError(t, err) @@ -311,6 +314,7 @@ func TestTotal_RenderWiki(t *testing.T) { for i := 0; i < len(testCases); i += 2 { line, err := RenderString(&markup.RenderContext{ + Ctx: git.DefaultContext, URLPrefix: AppSubURL, IsWiki: true, }, testCases[i]) @@ -339,6 +343,7 @@ func TestTotal_RenderString(t *testing.T) { for i := 0; i < len(testCases); i += 2 { line, err := RenderString(&markup.RenderContext{ + Ctx: git.DefaultContext, URLPrefix: AppSubURL, }, testCases[i]) assert.NoError(t, err) @@ -348,17 +353,17 @@ func TestTotal_RenderString(t *testing.T) { func TestRender_RenderParagraphs(t *testing.T) { test := func(t *testing.T, str string, cnt int) { - res, err := RenderRawString(&markup.RenderContext{}, str) + res, err := RenderRawString(&markup.RenderContext{Ctx: git.DefaultContext}, str) assert.NoError(t, err) assert.Equal(t, cnt, strings.Count(res, "<p"), "Rendered result for unix should have %d paragraph(s) but has %d:\n%s\n", cnt, strings.Count(res, "<p"), res) mac := strings.ReplaceAll(str, "\n", "\r") - res, err = RenderRawString(&markup.RenderContext{}, mac) + res, err = RenderRawString(&markup.RenderContext{Ctx: git.DefaultContext}, mac) assert.NoError(t, err) assert.Equal(t, cnt, strings.Count(res, "<p"), "Rendered result for mac should have %d paragraph(s) but has %d:\n%s\n", cnt, strings.Count(res, "<p"), res) dos := strings.ReplaceAll(str, "\n", "\r\n") - res, err = RenderRawString(&markup.RenderContext{}, dos) + res, err = RenderRawString(&markup.RenderContext{Ctx: git.DefaultContext}, dos) assert.NoError(t, err) assert.Equal(t, cnt, strings.Count(res, "<p"), "Rendered result for windows should have %d paragraph(s) but has %d:\n%s\n", cnt, strings.Count(res, "<p"), res) } @@ -386,7 +391,7 @@ func TestMarkdownRenderRaw(t *testing.T) { for _, testcase := range testcases { log.Info("Test markdown render error with fuzzy data: %x, the following errors can be recovered", testcase) - _, err := RenderRawString(&markup.RenderContext{}, string(testcase)) + _, err := RenderRawString(&markup.RenderContext{Ctx: git.DefaultContext}, string(testcase)) assert.NoError(t, err) } } @@ -398,7 +403,7 @@ func TestRenderSiblingImages_Issue12925(t *testing.T) { expected := `<p><a href="/image1" target="_blank" rel="nofollow noopener"><img src="/image1" alt="image1"></a><br> <a href="/image2" target="_blank" rel="nofollow noopener"><img src="/image2" alt="image2"></a></p> ` - res, err := RenderRawString(&markup.RenderContext{}, testcase) + res, err := RenderRawString(&markup.RenderContext{Ctx: git.DefaultContext}, testcase) assert.NoError(t, err) assert.Equal(t, expected, res) } @@ -407,7 +412,7 @@ func TestRenderEmojiInLinks_Issue12331(t *testing.T) { testcase := `[Link with emoji :moon: in text](https://gitea.io)` expected := `<p><a href="https://gitea.io" rel="nofollow">Link with emoji <span class="emoji" aria-label="waxing gibbous moon">🌔</span> in text</a></p> ` - res, err := RenderString(&markup.RenderContext{}, testcase) + res, err := RenderString(&markup.RenderContext{Ctx: git.DefaultContext}, testcase) assert.NoError(t, err) assert.Equal(t, expected, res) } @@ -441,7 +446,7 @@ func TestColorPreview(t *testing.T) { } for _, test := range positiveTests { - res, err := RenderString(&markup.RenderContext{}, test.testcase) + res, err := RenderString(&markup.RenderContext{Ctx: git.DefaultContext}, test.testcase) assert.NoError(t, err, "Unexpected error in testcase: %q", test.testcase) assert.Equal(t, test.expected, res, "Unexpected result in testcase %q", test.testcase) @@ -461,7 +466,7 @@ func TestColorPreview(t *testing.T) { } for _, test := range negativeTests { - res, err := RenderString(&markup.RenderContext{}, test) + res, err := RenderString(&markup.RenderContext{Ctx: git.DefaultContext}, test) assert.NoError(t, err, "Unexpected error in testcase: %q", test) assert.NotContains(t, res, `<span class="color-preview" style="background-color: `, "Unexpected result in testcase %q", test) } @@ -508,7 +513,7 @@ func TestMathBlock(t *testing.T) { } for _, test := range testcases { - res, err := RenderString(&markup.RenderContext{}, test.testcase) + res, err := RenderString(&markup.RenderContext{Ctx: git.DefaultContext}, test.testcase) assert.NoError(t, err, "Unexpected error in testcase: %q", test.testcase) assert.Equal(t, test.expected, res, "Unexpected result in testcase %q", test.testcase) diff --git a/modules/markup/orgmode/orgmode_test.go b/modules/markup/orgmode/orgmode_test.go index 4acb678cd7..d6467c36f7 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -26,6 +27,7 @@ func TestRender_StandardLinks(t *testing.T) { test := func(input, expected string) { buffer, err := RenderString(&markup.RenderContext{ + Ctx: git.DefaultContext, URLPrefix: setting.AppSubURL, }, input) assert.NoError(t, err) @@ -46,6 +48,7 @@ func TestRender_Images(t *testing.T) { test := func(input, expected string) { buffer, err := RenderString(&markup.RenderContext{ + Ctx: git.DefaultContext, URLPrefix: setting.AppSubURL, }, input) assert.NoError(t, err) @@ -65,6 +68,7 @@ func TestRender_Source(t *testing.T) { test := func(input, expected string) { buffer, err := RenderString(&markup.RenderContext{ + Ctx: git.DefaultContext, URLPrefix: setting.AppSubURL, }, input) assert.NoError(t, err) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 17ac68dc6b..19893c7c9d 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -385,10 +385,10 @@ func NewFuncMap() []template.FuncMap { // the table is NOT sorted with this header return "" }, - "RenderLabel": func(label *issues_model.Label) template.HTML { - return template.HTML(RenderLabel(label)) + "RenderLabel": func(ctx context.Context, label *issues_model.Label) template.HTML { + return template.HTML(RenderLabel(ctx, label)) }, - "RenderLabels": func(labels []*issues_model.Label, repoLink string) template.HTML { + "RenderLabels": func(ctx context.Context, labels []*issues_model.Label, repoLink string) template.HTML { htmlCode := `<span class="labels-list">` for _, label := range labels { // Protect against nil value in labels - shouldn't happen but would cause a panic if so @@ -396,7 +396,7 @@ func NewFuncMap() []template.FuncMap { continue } htmlCode += fmt.Sprintf("<a href='%s/issues?labels=%d'>%s</a> ", - repoLink, label.ID, RenderLabel(label)) + repoLink, label.ID, RenderLabel(ctx, label)) } htmlCode += "</span>" return template.HTML(htmlCode) @@ -808,7 +808,7 @@ func RenderIssueTitle(ctx context.Context, text, urlPrefix string, metas map[str } // RenderLabel renders a label -func RenderLabel(label *issues_model.Label) string { +func RenderLabel(ctx context.Context, label *issues_model.Label) string { labelScope := label.ExclusiveScope() textColor := "#111" @@ -821,12 +821,12 @@ func RenderLabel(label *issues_model.Label) string { if labelScope == "" { // Regular label return fmt.Sprintf("<div class='ui label' style='color: %s !important; background-color: %s !important' title='%s'>%s</div>", - textColor, label.Color, description, RenderEmoji(label.Name)) + textColor, label.Color, description, RenderEmoji(ctx, label.Name)) } // Scoped label - scopeText := RenderEmoji(labelScope) - itemText := RenderEmoji(label.Name[len(labelScope)+1:]) + scopeText := RenderEmoji(ctx, labelScope) + itemText := RenderEmoji(ctx, label.Name[len(labelScope)+1:]) itemColor := label.Color scopeColor := label.Color @@ -869,8 +869,9 @@ func RenderLabel(label *issues_model.Label) string { } // RenderEmoji renders html text with emoji post processors -func RenderEmoji(text string) template.HTML { - renderedText, err := markup.RenderEmoji(template.HTMLEscapeString(text)) +func RenderEmoji(ctx context.Context, text string) template.HTML { + renderedText, err := markup.RenderEmoji(&markup.RenderContext{Ctx: ctx}, + template.HTMLEscapeString(text)) if err != nil { log.Error("RenderEmoji: %v", err) return template.HTML("") |