From 7b5f9895cef9c554a07bff4f824be03506c771ab Mon Sep 17 00:00:00 2001 From: zeripath Date: Sat, 28 Aug 2021 21:15:56 +0100 Subject: [PATCH] In Render tolerate not being passed a context (#16842) * In Render tolerate not being passed a context It is possible for RenderString to be passed to an external renderer if markdown is set to be rendered by an external renderer. No context is currently sent to these meaning that this will error out. Fix #16835 Signed-off-by: Andrew Thornton * Add Context to Repo calls for RenderString All calls from routers can easily add the context - so add it. Signed-off-by: Andrew Thornton --- modules/markup/external/external.go | 8 +++++++- routers/web/repo/issue.go | 5 +++++ routers/web/repo/milestone.go | 2 ++ routers/web/repo/projects.go | 2 ++ routers/web/repo/release.go | 2 ++ routers/web/user/home.go | 1 + routers/web/user/profile.go | 1 + 7 files changed, 20 insertions(+), 1 deletion(-) diff --git a/modules/markup/external/external.go b/modules/markup/external/external.go index 52139f5a49c3c..f7be06dbe91d3 100644 --- a/modules/markup/external/external.go +++ b/modules/markup/external/external.go @@ -14,6 +14,7 @@ import ( "runtime" "strings" + "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/process" @@ -99,7 +100,12 @@ func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io. } if ctx == nil || ctx.Ctx == nil { - return fmt.Errorf("RenderContext did not provide context") + if ctx == nil { + log.Warn("RenderContext not provided defaulting to empty ctx") + ctx = &markup.RenderContext{} + } + log.Warn("RenderContext did not provide context, defaulting to Shutdown context") + ctx.Ctx = graceful.GetManager().ShutdownContext() } processCtx, cancel := context.WithCancel(ctx.Ctx) diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 3162e0932d7c7..9d678eccdd71c 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1138,6 +1138,7 @@ func ViewIssue(ctx *context.Context) { URLPrefix: ctx.Repo.RepoLink, Metas: ctx.Repo.Repository.ComposeMetas(), GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, }, issue.Content) if err != nil { ctx.ServerError("RenderString", err) @@ -1303,6 +1304,7 @@ func ViewIssue(ctx *context.Context) { URLPrefix: ctx.Repo.RepoLink, Metas: ctx.Repo.Repository.ComposeMetas(), GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, }, comment.Content) if err != nil { ctx.ServerError("RenderString", err) @@ -1379,6 +1381,7 @@ func ViewIssue(ctx *context.Context) { URLPrefix: ctx.Repo.RepoLink, Metas: ctx.Repo.Repository.ComposeMetas(), GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, }, comment.Content) if err != nil { ctx.ServerError("RenderString", err) @@ -1740,6 +1743,7 @@ func UpdateIssueContent(ctx *context.Context) { URLPrefix: ctx.Query("context"), Metas: ctx.Repo.Repository.ComposeMetas(), GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, }, issue.Content) if err != nil { ctx.ServerError("RenderString", err) @@ -2170,6 +2174,7 @@ func UpdateCommentContent(ctx *context.Context) { URLPrefix: ctx.Query("context"), Metas: ctx.Repo.Repository.ComposeMetas(), GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, }, comment.Content) if err != nil { ctx.ServerError("RenderString", err) diff --git a/routers/web/repo/milestone.go b/routers/web/repo/milestone.go index 4cdca38dd02b6..129f1a5593545 100644 --- a/routers/web/repo/milestone.go +++ b/routers/web/repo/milestone.go @@ -89,6 +89,7 @@ func Milestones(ctx *context.Context) { URLPrefix: ctx.Repo.RepoLink, Metas: ctx.Repo.Repository.ComposeMetas(), GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, }, m.Content) if err != nil { ctx.ServerError("RenderString", err) @@ -282,6 +283,7 @@ func MilestoneIssuesAndPulls(ctx *context.Context) { URLPrefix: ctx.Repo.RepoLink, Metas: ctx.Repo.Repository.ComposeMetas(), GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, }, milestone.Content) if err != nil { ctx.ServerError("RenderString", err) diff --git a/routers/web/repo/projects.go b/routers/web/repo/projects.go index c7490893d5fe6..1d95db41672c4 100644 --- a/routers/web/repo/projects.go +++ b/routers/web/repo/projects.go @@ -82,6 +82,7 @@ func Projects(ctx *context.Context) { URLPrefix: ctx.Repo.RepoLink, Metas: ctx.Repo.Repository.ComposeMetas(), GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, }, projects[i].Description) if err != nil { ctx.ServerError("RenderString", err) @@ -324,6 +325,7 @@ func ViewProject(ctx *context.Context) { URLPrefix: ctx.Repo.RepoLink, Metas: ctx.Repo.Repository.ComposeMetas(), GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, }, project.Description) if err != nil { ctx.ServerError("RenderString", err) diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index 0665496d44c3d..a56e693ce31fd 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -146,6 +146,7 @@ func releasesOrTags(ctx *context.Context, isTagList bool) { URLPrefix: ctx.Repo.RepoLink, Metas: ctx.Repo.Repository.ComposeMetas(), GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, }, r.Note) if err != nil { ctx.ServerError("RenderString", err) @@ -215,6 +216,7 @@ func SingleRelease(ctx *context.Context) { URLPrefix: ctx.Repo.RepoLink, Metas: ctx.Repo.Repository.ComposeMetas(), GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, }, release.Note) if err != nil { ctx.ServerError("RenderString", err) diff --git a/routers/web/user/home.go b/routers/web/user/home.go index d3fc36c73012e..fbe13418a0d7f 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -272,6 +272,7 @@ func Milestones(ctx *context.Context) { milestones[i].RenderedContent, err = markdown.RenderString(&markup.RenderContext{ URLPrefix: milestones[i].Repo.Link(), Metas: milestones[i].Repo.ComposeMetas(), + Ctx: ctx, }, milestones[i].Content) if err != nil { ctx.ServerError("RenderString", err) diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 631ca2113512a..d723263d8a1a2 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -124,6 +124,7 @@ func Profile(ctx *context.Context) { URLPrefix: ctx.Repo.RepoLink, Metas: map[string]string{"mode": "document"}, GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, }, ctxUser.Description) if err != nil { ctx.ServerError("RenderString", err)