From 00ad4745ba196c52441e28b04ba133038d203e15 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sun, 17 Jun 2018 09:33:44 +0200 Subject: [PATCH] backport(#4091): Fix #4090 by escaping filename page/img link (without path) for short link (#4254) --- modules/markup/html.go | 3 +++ modules/markup/markdown/markdown_test.go | 26 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/modules/markup/html.go b/modules/markup/html.go index 383525477..1dc6627f2 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -405,6 +405,9 @@ func RenderShortLinks(rawBytes []byte, urlPrefix string, noLink bool, isWikiMark } else { link = strings.Replace(link, " ", "-", -1) } + if !strings.Contains(link, "/") { + link = url.PathEscape(link) + } } if image { if !absoluteLink { diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 9ca3de01c..c7a97813b 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -55,10 +55,16 @@ func TestRender_ShortLinks(t *testing.T) { rawtree := markup.URLJoin(AppSubURL, "raw", "master") url := markup.URLJoin(tree, "Link") otherUrl := markup.URLJoin(tree, "OtherLink") + encodedURL := markup.URLJoin(tree, "Link%3F") imgurl := markup.URLJoin(rawtree, "Link.jpg") + encodedImgurl := markup.URLJoin(rawtree, "Link+%23.jpg") + notencodedImgurl := markup.URLJoin(rawtree, "some", "path", "Link+#.jpg") urlWiki := markup.URLJoin(AppSubURL, "wiki", "Link") otherUrlWiki := markup.URLJoin(AppSubURL, "wiki", "OtherLink") + encodedURLWiki := markup.URLJoin(AppSubURL, "wiki", "Link%3F") imgurlWiki := markup.URLJoin(AppSubURL, "wiki", "raw", "Link.jpg") + encodedImgurlWiki := markup.URLJoin(AppSubURL, "wiki", "raw", "Link+%23.jpg") + notencodedImgurlWiki := markup.URLJoin(AppSubURL, "wiki", "raw", "some", "path", "Link+#.jpg") favicon := "http://google.com/favicon.ico" test( @@ -101,6 +107,26 @@ func TestRender_ShortLinks(t *testing.T) { "[[Link]] [[OtherLink]]", `

Link OtherLink

`, `

Link OtherLink

`) + test( + "[[Link?]]", + `

Link?

`, + `

Link?

`) + test( + "[[Link]] [[OtherLink]] [[Link?]]", + `

Link OtherLink Link?

`, + `

Link OtherLink Link?

`) + test( + "[[Link #.jpg]]", + `

`, + `

`) + test( + "[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]", + `

AltName

`, + `

AltName

`) + test( + "[[some/path/Link #.jpg]]", + `

`, + `

`) } func TestMisc_IsMarkdownFile(t *testing.T) {