aboutsummaryrefslogtreecommitdiffstats
path: root/modules/markup
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2025-03-14 03:14:35 +0800
committerGitHub <noreply@github.com>2025-03-13 12:14:35 -0700
commit8a0f7f0975d7d14160022ef94984b5c031220e6d (patch)
tree3a6265475c52efadee0876478b4d79e7dba9d747 /modules/markup
parent403775e74ed11097b30a9a683b2253c0967c0dcd (diff)
downloadgitea-8a0f7f0975d7d14160022ef94984b5c031220e6d.tar.gz
gitea-8a0f7f0975d7d14160022ef94984b5c031220e6d.zip
Fix markdown render (#33870)
Fix #33869, some code block is moved to make it easier to test. Added a new test
Diffstat (limited to 'modules/markup')
-rw-r--r--modules/markup/markdown/markdown.go22
-rw-r--r--modules/markup/markdown/markdown_attention_test.go14
-rw-r--r--modules/markup/markdown/transform_blockquote.go3
-rw-r--r--modules/markup/renderer_test.go4
4 files changed, 22 insertions, 21 deletions
diff --git a/modules/markup/markdown/markdown.go b/modules/markup/markdown/markdown.go
index b5fffccdb9..ace31eb540 100644
--- a/modules/markup/markdown/markdown.go
+++ b/modules/markup/markdown/markdown.go
@@ -159,6 +159,14 @@ func render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
limit: setting.UI.MaxDisplayFileSize * 3,
}
+ // FIXME: Don't read all to memory, but goldmark doesn't support
+ buf, err := io.ReadAll(input)
+ if err != nil {
+ log.Error("Unable to ReadAll: %v", err)
+ return err
+ }
+ buf = giteautil.NormalizeEOL(buf)
+
// FIXME: should we include a timeout to abort the renderer if it takes too long?
defer func() {
err := recover()
@@ -166,20 +174,12 @@ func render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
return
}
- log.Warn("Unable to render markdown due to panic in goldmark: %v", err)
- if (!setting.IsProd && !setting.IsInTesting) || log.IsDebug() {
- log.Error("Panic in markdown: %v\n%s", err, log.Stack(2))
- }
+ log.Error("Panic in markdown: %v\n%s", err, log.Stack(2))
+ escapedHTML := template.HTMLEscapeString(giteautil.UnsafeBytesToString(buf))
+ _, _ = output.Write(giteautil.UnsafeStringToBytes(escapedHTML))
}()
- // FIXME: Don't read all to memory, but goldmark doesn't support
pc := newParserContext(ctx)
- buf, err := io.ReadAll(input)
- if err != nil {
- log.Error("Unable to ReadAll: %v", err)
- return err
- }
- buf = giteautil.NormalizeEOL(buf)
// Preserve original length.
bufWithMetadataLength := len(buf)
diff --git a/modules/markup/markdown/markdown_attention_test.go b/modules/markup/markdown/markdown_attention_test.go
index f6ec775b2c..7b54653ec0 100644
--- a/modules/markup/markdown/markdown_attention_test.go
+++ b/modules/markup/markdown/markdown_attention_test.go
@@ -23,6 +23,11 @@ func TestAttention(t *testing.T) {
defer svg.MockIcon("octicon-alert")()
defer svg.MockIcon("octicon-stop")()
+ test := func(input, expected string) {
+ result, err := markdown.RenderString(markup.NewTestRenderContext(), input)
+ assert.NoError(t, err)
+ assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(result)))
+ }
renderAttention := func(attention, icon string) string {
tmpl := `<blockquote class="attention-header attention-{attention}"><p><svg class="attention-icon attention-{attention} svg {icon}" width="16" height="16"></svg><strong class="attention-{attention}">{Attention}</strong></p>`
tmpl = strings.ReplaceAll(tmpl, "{attention}", attention)
@@ -31,12 +36,6 @@ func TestAttention(t *testing.T) {
return tmpl
}
- test := func(input, expected string) {
- result, err := markdown.RenderString(markup.NewTestRenderContext(), input)
- assert.NoError(t, err)
- assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(result)))
- }
-
test(`
> [!NOTE]
> text
@@ -53,4 +52,7 @@ func TestAttention(t *testing.T) {
// legacy GitHub style
test(`> **warning**`, renderAttention("warning", "octicon-alert")+"\n</blockquote>")
+
+ // edge case (it used to cause panic)
+ test(">\ntext", "<blockquote>\n</blockquote>\n<p>text</p>")
}
diff --git a/modules/markup/markdown/transform_blockquote.go b/modules/markup/markdown/transform_blockquote.go
index 2651d44a69..3a8c6fa018 100644
--- a/modules/markup/markdown/transform_blockquote.go
+++ b/modules/markup/markdown/transform_blockquote.go
@@ -115,6 +115,9 @@ func (g *ASTTransformer) transformBlockquote(v *ast.Blockquote, reader text.Read
// grab these nodes and make sure we adhere to the attention blockquote structure
firstParagraph := v.FirstChild()
+ if firstParagraph == nil {
+ return ast.WalkContinue, nil
+ }
g.applyElementDir(firstParagraph)
attentionType, processedNodes := g.extractBlockquoteAttentionEmphasis(firstParagraph, reader)
diff --git a/modules/markup/renderer_test.go b/modules/markup/renderer_test.go
deleted file mode 100644
index 0791081f94..0000000000
--- a/modules/markup/renderer_test.go
+++ /dev/null
@@ -1,4 +0,0 @@
-// Copyright 2017 The Gitea Authors. All rights reserved.
-// SPDX-License-Identifier: MIT
-
-package markup_test