Skip to content

Commit 08ff490

Browse files
committed
fix
1 parent c28aab6 commit 08ff490

File tree

6 files changed

+59
-21
lines changed

6 files changed

+59
-21
lines changed

custom/conf/app.example.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,6 +2541,11 @@ LEVEL = Info
25412541
;; * no-sanitizer: Disable the sanitizer and render the content inside current page. It's **insecure** and may lead to XSS attack if the content contains malicious code.
25422542
;; * iframe: Render the content in a separate standalone page and embed it into current page by iframe. The iframe is in sandbox mode with same-origin disabled, and the JS code are safely isolated from parent page.
25432543
;RENDER_CONTENT_MODE=sanitized
2544+
;;
2545+
;; Whether post-process the rendered HTML content, including:
2546+
;; resolve relative links and image sources, recognizing issue/commit references, escaping invisible characters,
2547+
;; mentioning users, rendering permlink code blocks, replacing emoji shorthands, etc.
2548+
;NEED_POST_PROCESS=false
25442549

25452550
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25462551
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

modules/markup/external/external.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"code.gitea.io/gitea/modules/markup"
1616
"code.gitea.io/gitea/modules/process"
1717
"code.gitea.io/gitea/modules/setting"
18+
19+
"github.com/kballard/go-shellquote"
1820
)
1921

2022
// RegisterRenderers registers all supported third part renderers according settings
@@ -81,7 +83,10 @@ func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.
8183
envMark("GITEA_PREFIX_SRC"), baseLinkSrc,
8284
envMark("GITEA_PREFIX_RAW"), baseLinkRaw,
8385
).Replace(p.Command)
84-
commands := strings.Fields(command)
86+
commands, err := shellquote.Split(command)
87+
if err != nil || len(commands) == 0 {
88+
return fmt.Errorf("%s invalid command %q: %w", p.Name(), p.Command, err)
89+
}
8590
args := commands[1:]
8691

8792
if p.IsInputFile {

modules/markup/render.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,31 +120,36 @@ func (ctx *RenderContext) WithHelper(helper RenderHelper) *RenderContext {
120120
return ctx
121121
}
122122

123-
// Render renders markup file to HTML with all specific handling stuff.
124-
func Render(ctx *RenderContext, input io.Reader, output io.Writer) error {
123+
func FindRenderer(ctx *RenderContext) (Renderer, error) {
125124
if ctx.RenderOptions.MarkupType == "" && ctx.RenderOptions.RelativePath != "" {
126125
ctx.RenderOptions.MarkupType = DetectMarkupTypeByFileName(ctx.RenderOptions.RelativePath)
127126
if ctx.RenderOptions.MarkupType == "" {
128-
return util.NewInvalidArgumentErrorf("unsupported file to render: %q", ctx.RenderOptions.RelativePath)
127+
return nil, util.NewInvalidArgumentErrorf("unsupported file to render: %q", ctx.RenderOptions.RelativePath)
129128
}
130129
}
131130

132131
renderer := renderers[ctx.RenderOptions.MarkupType]
133132
if renderer == nil {
134-
return util.NewInvalidArgumentErrorf("unsupported markup type: %q", ctx.RenderOptions.MarkupType)
133+
return nil, util.NewNotExistErrorf("unsupported markup type: %q", ctx.RenderOptions.MarkupType)
135134
}
136135

137-
if ctx.RenderOptions.RelativePath != "" {
138-
if externalRender, ok := renderer.(ExternalRenderer); ok && externalRender.DisplayInIFrame() {
139-
if !ctx.RenderOptions.InStandalonePage {
140-
// for an external "DisplayInIFrame" render, it could only output its content in a standalone page
141-
// otherwise, a <iframe> should be outputted to embed the external rendered page
142-
return renderIFrame(ctx, output)
143-
}
144-
}
136+
return renderer, nil
137+
}
138+
139+
func RendererNeedPostProcess(renderer Renderer) bool {
140+
if r, ok := renderer.(PostProcessRenderer); ok && r.NeedPostProcess() {
141+
return true
145142
}
143+
return false
144+
}
146145

147-
return render(ctx, renderer, input, output)
146+
// Render renders markup file to HTML with all specific handling stuff.
147+
func Render(ctx *RenderContext, input io.Reader, output io.Writer) error {
148+
renderer, err := FindRenderer(ctx)
149+
if err != nil {
150+
return err
151+
}
152+
return RenderWithRenderer(ctx, renderer, input, output)
148153
}
149154

150155
// RenderString renders Markup string to HTML with all specific handling stuff and return string
@@ -185,7 +190,17 @@ func pipes() (io.ReadCloser, io.WriteCloser, func()) {
185190
}
186191
}
187192

188-
func render(ctx *RenderContext, renderer Renderer, input io.Reader, output io.Writer) error {
193+
func RenderWithRenderer(ctx *RenderContext, renderer Renderer, input io.Reader, output io.Writer) error {
194+
if ctx.RenderOptions.RelativePath != "" {
195+
if externalRender, ok := renderer.(ExternalRenderer); ok && externalRender.DisplayInIFrame() {
196+
if !ctx.RenderOptions.InStandalonePage {
197+
// for an external "DisplayInIFrame" render, it could only output its content in a standalone page
198+
// otherwise, a <iframe> should be outputted to embed the external rendered page
199+
return renderIFrame(ctx, output)
200+
}
201+
}
202+
}
203+
189204
ctx.usedByRender = true
190205
if ctx.RenderHelper != nil {
191206
defer ctx.RenderHelper.CleanUp()
@@ -214,7 +229,7 @@ func render(ctx *RenderContext, renderer Renderer, input io.Reader, output io.Wr
214229
}
215230

216231
eg.Go(func() (err error) {
217-
if r, ok := renderer.(PostProcessRenderer); ok && r.NeedPostProcess() {
232+
if RendererNeedPostProcess(renderer) {
218233
err = PostProcessDefault(ctx, pr1, pw2)
219234
} else {
220235
_, err = io.Copy(pw2, pr1)

modules/setting/markup.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ func newMarkupRenderer(name string, sec ConfigSection) {
259259
FileExtensions: exts,
260260
Command: command,
261261
IsInputFile: sec.Key("IS_INPUT_FILE").MustBool(false),
262-
NeedPostProcess: sec.Key("NEED_POSTPROCESS").MustBool(true),
263262
RenderContentMode: renderContentMode,
263+
264+
// if no sanitizer is needed, no post process is needed
265+
NeedPostProcess: sec.Key("NEED_POST_PROCESS").MustBool(renderContentMode == RenderContentModeSanitized),
264266
})
265267
}

routers/web/repo/view.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,28 @@ func loadLatestCommitData(ctx *context.Context, latestCommit *git.Commit) bool {
151151
}
152152

153153
func markupRender(ctx *context.Context, renderCtx *markup.RenderContext, input io.Reader) (escaped *charset.EscapeStatus, output template.HTML, err error) {
154+
renderer, err := markup.FindRenderer(renderCtx)
155+
if err != nil {
156+
return nil, "", err
157+
}
158+
154159
markupRd, markupWr := io.Pipe()
155160
defer markupWr.Close()
161+
156162
done := make(chan struct{})
157163
go func() {
158164
sb := &strings.Builder{}
159-
// We allow NBSP here this is rendered
160-
escaped, _ = charset.EscapeControlReader(markupRd, sb, ctx.Locale, charset.RuneNBSP)
165+
if markup.RendererNeedPostProcess(renderer) {
166+
escaped, _ = charset.EscapeControlReader(markupRd, sb, ctx.Locale, charset.RuneNBSP) // We allow NBSP here this is rendered
167+
} else {
168+
escaped = &charset.EscapeStatus{}
169+
_, _ = io.Copy(sb, markupRd)
170+
}
161171
output = template.HTML(sb.String())
162172
close(done)
163173
}()
164-
err = markup.Render(renderCtx, input, markupWr)
174+
175+
err = markup.RenderWithRenderer(renderCtx, renderer, input, markupWr)
165176
_ = markupWr.CloseWithError(err)
166177
<-done
167178
return escaped, output, err

templates/repo/view_file.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
{{if not .IsMarkup}}
9494
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus}}
9595
{{end}}
96-
<div class="file-view {{if .IsMarkup}}markup {{.MarkupType}}{{else if .IsPlainText}}plain-text{{else if .IsDisplayingSource}}code-view{{end}}">
96+
<div class="file-view {{if .IsMarkup}}markup markup-type-{{.MarkupType}}{{else if .IsPlainText}}plain-text{{else if .IsDisplayingSource}}code-view{{end}}">
9797
{{if .IsFileTooLarge}}
9898
{{template "shared/filetoolarge" dict "RawFileLink" .RawFileLink}}
9999
{{else if not .FileSize}}

0 commit comments

Comments
 (0)