Skip to content

Commit 00ad474

Browse files
sapklunny
authored andcommitted
backport(#4091): Fix #4090 by escaping filename page/img link (without path) for short link (#4254)
1 parent c746f82 commit 00ad474

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

modules/markup/html.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ func RenderShortLinks(rawBytes []byte, urlPrefix string, noLink bool, isUncycloMark
405405
} else {
406406
link = strings.Replace(link, " ", "-", -1)
407407
}
408+
if !strings.Contains(link, "/") {
409+
link = url.PathEscape(link)
410+
}
408411
}
409412
if image {
410413
if !absoluteLink {

modules/markup/markdown/markdown_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,16 @@ func TestRender_ShortLinks(t *testing.T) {
5555
rawtree := markup.URLJoin(AppSubURL, "raw", "master")
5656
url := markup.URLJoin(tree, "Link")
5757
otherUrl := markup.URLJoin(tree, "OtherLink")
58+
encodedURL := markup.URLJoin(tree, "Link%3F")
5859
imgurl := markup.URLJoin(rawtree, "Link.jpg")
60+
encodedImgurl := markup.URLJoin(rawtree, "Link+%23.jpg")
61+
notencodedImgurl := markup.URLJoin(rawtree, "some", "path", "Link+#.jpg")
5962
urlUncyclo := markup.URLJoin(AppSubURL, "wiki", "Link")
6063
otherUrlUncyclo := markup.URLJoin(AppSubURL, "wiki", "OtherLink")
64+
encodedURLUncyclo := markup.URLJoin(AppSubURL, "wiki", "Link%3F")
6165
imgurlUncyclo := markup.URLJoin(AppSubURL, "wiki", "raw", "Link.jpg")
66+
encodedImgurlUncyclo := markup.URLJoin(AppSubURL, "wiki", "raw", "Link+%23.jpg")
67+
notencodedImgurlUncyclo := markup.URLJoin(AppSubURL, "wiki", "raw", "some", "path", "Link+#.jpg")
6268
favicon := "http://google.com/favicon.ico"
6369

6470
test(
@@ -101,6 +107,26 @@ func TestRender_ShortLinks(t *testing.T) {
101107
"[[Link]] [[OtherLink]]",
102108
`<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherUrl+`" rel="nofollow">OtherLink</a></p>`,
103109
`<p><a href="`+urlUncyclo+`" rel="nofollow">Link</a> <a href="`+otherUrlUncyclo+`" rel="nofollow">OtherLink</a></p>`)
110+
test(
111+
"[[Link?]]",
112+
`<p><a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
113+
`<p><a href="`+encodedURLUncyclo+`" rel="nofollow">Link?</a></p>`)
114+
test(
115+
"[[Link]] [[OtherLink]] [[Link?]]",
116+
`<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherUrl+`" rel="nofollow">OtherLink</a> <a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
117+
`<p><a href="`+urlUncyclo+`" rel="nofollow">Link</a> <a href="`+otherUrlUncyclo+`" rel="nofollow">OtherLink</a> <a href="`+encodedURLUncyclo+`" rel="nofollow">Link?</a></p>`)
118+
test(
119+
"[[Link #.jpg]]",
120+
`<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`"/></a></p>`,
121+
`<p><a href="`+encodedImgurlUncyclo+`" rel="nofollow"><img src="`+encodedImgurlUncyclo+`"/></a></p>`)
122+
test(
123+
"[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]",
124+
`<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" alt="AltName" title="Title"/></a></p>`,
125+
`<p><a href="`+encodedImgurlUncyclo+`" rel="nofollow"><img src="`+encodedImgurlUncyclo+`" alt="AltName" title="Title"/></a></p>`)
126+
test(
127+
"[[some/path/Link #.jpg]]",
128+
`<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`"/></a></p>`,
129+
`<p><a href="`+notencodedImgurlUncyclo+`" rel="nofollow"><img src="`+notencodedImgurlUncyclo+`"/></a></p>`)
104130
}
105131

106132
func TestMisc_IsMarkdownFile(t *testing.T) {

0 commit comments

Comments
 (0)