gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package markup_test
  4. import (
  5. "io"
  6. "strings"
  7. "testing"
  8. "code.gitea.io/gitea/modules/emoji"
  9. "code.gitea.io/gitea/modules/markup"
  10. "code.gitea.io/gitea/modules/markup/markdown"
  11. "code.gitea.io/gitea/modules/setting"
  12. testModule "code.gitea.io/gitea/modules/test"
  13. "code.gitea.io/gitea/modules/util"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. var (
  17. testRepoOwnerName = "user13"
  18. testRepoName = "repo11"
  19. localMetas = map[string]string{"user": testRepoOwnerName, "repo": testRepoName}
  20. )
  21. func TestRender_Commits(t *testing.T) {
  22. test := func(input, expected string) {
  23. rctx := markup.NewTestRenderContext(markup.TestAppURL, localMetas).WithRelativePath("a.md")
  24. buffer, err := markup.RenderString(rctx, input)
  25. assert.NoError(t, err)
  26. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  27. }
  28. sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
  29. repo := markup.TestAppURL + testRepoOwnerName + "/" + testRepoName + "/"
  30. commit := util.URLJoin(repo, "commit", sha)
  31. commitPath := "/user13/repo11/commit/" + sha
  32. tree := util.URLJoin(repo, "tree", sha, "src")
  33. file := util.URLJoin(repo, "commit", sha, "example.txt")
  34. fileWithExtra := file + ":"
  35. fileWithHash := file + "#L2"
  36. fileWithHasExtra := file + "#L2:"
  37. commitCompare := util.URLJoin(repo, "compare", sha+"..."+sha)
  38. commitCompareWithHash := commitCompare + "#L2"
  39. test(sha, `<p><a href="`+commitPath+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  40. test(sha[:7], `<p><a href="`+commitPath[:len(commitPath)-(40-7)]+`" rel="nofollow"><code>65f1bf2</code></a></p>`)
  41. test(sha[:39], `<p><a href="`+commitPath[:len(commitPath)-(40-39)]+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  42. test(commit, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  43. test(tree, `<p><a href="`+tree+`" rel="nofollow"><code>65f1bf27bc/src</code></a></p>`)
  44. test(file, `<p><a href="`+file+`" rel="nofollow"><code>65f1bf27bc/example.txt</code></a></p>`)
  45. test(fileWithExtra, `<p><a href="`+file+`" rel="nofollow"><code>65f1bf27bc/example.txt</code></a>:</p>`)
  46. test(fileWithHash, `<p><a href="`+fileWithHash+`" rel="nofollow"><code>65f1bf27bc/example.txt (L2)</code></a></p>`)
  47. test(fileWithHasExtra, `<p><a href="`+fileWithHash+`" rel="nofollow"><code>65f1bf27bc/example.txt (L2)</code></a>:</p>`)
  48. test(commitCompare, `<p><a href="`+commitCompare+`" rel="nofollow"><code>65f1bf27bc...65f1bf27bc</code></a></p>`)
  49. test(commitCompareWithHash, `<p><a href="`+commitCompareWithHash+`" rel="nofollow"><code>65f1bf27bc...65f1bf27bc (L2)</code></a></p>`)
  50. test("commit "+sha, `<p>commit <a href="`+commitPath+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  51. test("/home/gitea/"+sha, "<p>/home/gitea/"+sha+"</p>")
  52. test("deadbeef", `<p>deadbeef</p>`)
  53. test("d27ace93", `<p>d27ace93</p>`)
  54. test(sha[:14]+".x", `<p>`+sha[:14]+`.x</p>`)
  55. expected14 := `<a href="` + commitPath[:len(commitPath)-(40-14)] + `" rel="nofollow"><code>` + sha[:10] + `</code></a>`
  56. test(sha[:14]+".", `<p>`+expected14+`.</p>`)
  57. test(sha[:14]+",", `<p>`+expected14+`,</p>`)
  58. test("["+sha[:14]+"]", `<p>[`+expected14+`]</p>`)
  59. }
  60. func TestRender_CrossReferences(t *testing.T) {
  61. defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
  62. test := func(input, expected string) {
  63. rctx := markup.NewTestRenderContext(markup.TestAppURL, localMetas).WithRelativePath("a.md")
  64. buffer, err := markup.RenderString(rctx, input)
  65. assert.NoError(t, err)
  66. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  67. }
  68. test(
  69. "test-owner/test-repo#12345",
  70. `<p><a href="/test-owner/test-repo/issues/12345" class="ref-issue" rel="nofollow">test-owner/test-repo#12345</a></p>`)
  71. test(
  72. "go-gitea/gitea#12345",
  73. `<p><a href="/go-gitea/gitea/issues/12345" class="ref-issue" rel="nofollow">go-gitea/gitea#12345</a></p>`)
  74. test(
  75. "/home/gitea/go-gitea/gitea#12345",
  76. `<p>/home/gitea/go-gitea/gitea#12345</p>`)
  77. test(
  78. util.URLJoin(markup.TestAppURL, "gogitea", "gitea", "issues", "12345"),
  79. `<p><a href="`+util.URLJoin(markup.TestAppURL, "gogitea", "gitea", "issues", "12345")+`" class="ref-issue" rel="nofollow">gogitea/gitea#12345</a></p>`)
  80. test(
  81. util.URLJoin(markup.TestAppURL, "go-gitea", "gitea", "issues", "12345"),
  82. `<p><a href="`+util.URLJoin(markup.TestAppURL, "go-gitea", "gitea", "issues", "12345")+`" class="ref-issue" rel="nofollow">go-gitea/gitea#12345</a></p>`)
  83. test(
  84. util.URLJoin(markup.TestAppURL, "gogitea", "some-repo-name", "issues", "12345"),
  85. `<p><a href="`+util.URLJoin(markup.TestAppURL, "gogitea", "some-repo-name", "issues", "12345")+`" class="ref-issue" rel="nofollow">gogitea/some-repo-name#12345</a></p>`)
  86. inputURL := "https://host/a/b/commit/0123456789012345678901234567890123456789/foo.txt?a=b#L2-L3"
  87. test(
  88. inputURL,
  89. `<p><a href="`+inputURL+`" rel="nofollow"><code>0123456789/foo.txt (L2-L3)</code></a></p>`)
  90. }
  91. func TestRender_links(t *testing.T) {
  92. setting.AppURL = markup.TestAppURL
  93. defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
  94. test := func(input, expected string) {
  95. buffer, err := markup.RenderString(markup.NewTestRenderContext().WithRelativePath("a.md"), input)
  96. assert.NoError(t, err)
  97. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  98. }
  99. oldCustomURLSchemes := setting.Markdown.CustomURLSchemes
  100. markup.ResetDefaultSanitizerForTesting()
  101. defer func() {
  102. setting.Markdown.CustomURLSchemes = oldCustomURLSchemes
  103. markup.ResetDefaultSanitizerForTesting()
  104. markup.CustomLinkURLSchemes(oldCustomURLSchemes)
  105. }()
  106. setting.Markdown.CustomURLSchemes = []string{"ftp", "magnet"}
  107. markup.CustomLinkURLSchemes(setting.Markdown.CustomURLSchemes)
  108. // Text that should be turned into URL
  109. test(
  110. "https://www.example.com",
  111. `<p><a href="https://www.example.com" rel="nofollow">https://www.example.com</a></p>`)
  112. test(
  113. "http://www.example.com",
  114. `<p><a href="http://www.example.com" rel="nofollow">http://www.example.com</a></p>`)
  115. test(
  116. "https://example.com",
  117. `<p><a href="https://example.com" rel="nofollow">https://example.com</a></p>`)
  118. test(
  119. "http://example.com",
  120. `<p><a href="http://example.com" rel="nofollow">http://example.com</a></p>`)
  121. test(
  122. "http://foo.com/blah_blah",
  123. `<p><a href="http://foo.com/blah_blah" rel="nofollow">http://foo.com/blah_blah</a></p>`)
  124. test(
  125. "http://foo.com/blah_blah/",
  126. `<p><a href="http://foo.com/blah_blah/" rel="nofollow">http://foo.com/blah_blah/</a></p>`)
  127. test(
  128. "http://www.example.com/wpstyle/?p=364",
  129. `<p><a href="http://www.example.com/wpstyle/?p=364" rel="nofollow">http://www.example.com/wpstyle/?p=364</a></p>`)
  130. test(
  131. "https://www.example.com/foo/?bar=baz&inga=42&quux",
  132. `<p><a href="https://www.example.com/foo/?bar=baz&amp;inga=42&amp;quux" rel="nofollow">https://www.example.com/foo/?bar=baz&amp;inga=42&amp;quux</a></p>`)
  133. test(
  134. "http://142.42.1.1/",
  135. `<p><a href="http://142.42.1.1/" rel="nofollow">http://142.42.1.1/</a></p>`)
  136. test(
  137. "https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd",
  138. `<p><a href="https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd" rel="nofollow">https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd</a></p>`)
  139. test(
  140. "https://en.wikipedia.org/wiki/URL_(disambiguation)",
  141. `<p><a href="https://en.wikipedia.org/wiki/URL_(disambiguation)" rel="nofollow">https://en.wikipedia.org/wiki/URL_(disambiguation)</a></p>`)
  142. test(
  143. "https://foo_bar.example.com/",
  144. `<p><a href="https://foo_bar.example.com/" rel="nofollow">https://foo_bar.example.com/</a></p>`)
  145. test(
  146. "https://stackoverflow.com/questions/2896191/what-is-go-used-fore",
  147. `<p><a href="https://stackoverflow.com/questions/2896191/what-is-go-used-fore" rel="nofollow">https://stackoverflow.com/questions/2896191/what-is-go-used-fore</a></p>`)
  148. test(
  149. "https://username:password@gitea.com",
  150. `<p><a href="https://username:password@gitea.com" rel="nofollow">https://username:password@gitea.com</a></p>`)
  151. test(
  152. "ftp://gitea.com/file.txt",
  153. `<p><a href="ftp://gitea.com/file.txt" rel="nofollow">ftp://gitea.com/file.txt</a></p>`)
  154. test(
  155. "magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&dn=download",
  156. `<p><a href="magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&amp;dn=download" rel="nofollow">magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&amp;dn=download</a></p>`)
  157. test(
  158. `[link](https://example.com)`,
  159. `<p><a href="https://example.com" rel="nofollow">link</a></p>`)
  160. test(
  161. `[link](mailto:test@example.com)`,
  162. `<p><a href="mailto:test@example.com" rel="nofollow">link</a></p>`)
  163. test(
  164. `[link](javascript:xss)`,
  165. `<p>link</p>`)
  166. // Test that should *not* be turned into URL
  167. test(
  168. "www.example.com",
  169. `<p>www.example.com</p>`)
  170. test(
  171. "example.com",
  172. `<p>example.com</p>`)
  173. test(
  174. "test.example.com",
  175. `<p>test.example.com</p>`)
  176. test(
  177. "http://",
  178. `<p>http://</p>`)
  179. test(
  180. "https://",
  181. `<p>https://</p>`)
  182. test(
  183. "://",
  184. `<p>://</p>`)
  185. test(
  186. "www",
  187. `<p>www</p>`)
  188. test(
  189. "ftps://gitea.com",
  190. `<p>ftps://gitea.com</p>`)
  191. t.Run("LinkEllipsis", func(t *testing.T) {
  192. input := util.EllipsisDisplayString("http://10.1.2.3", 12)
  193. assert.Equal(t, "http://10…", input)
  194. test(input, "<p>http://10…</p>")
  195. input = util.EllipsisDisplayString("http://10.1.2.3", 13)
  196. assert.Equal(t, "http://10.…", input)
  197. test(input, "<p>http://10.…</p>")
  198. })
  199. }
  200. func TestRender_email(t *testing.T) {
  201. setting.AppURL = markup.TestAppURL
  202. defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
  203. test := func(input, expected string) {
  204. res, err := markup.RenderString(markup.NewTestRenderContext().WithRelativePath("a.md"), input)
  205. assert.NoError(t, err)
  206. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res), "input: %s", input)
  207. }
  208. // Text that should be turned into email link
  209. test(
  210. "info@gitea.com",
  211. `<p><a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a></p>`)
  212. test(
  213. "(info@gitea.com)",
  214. `<p>(<a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>)</p>`)
  215. test(
  216. "[info@gitea.com]",
  217. `<p>[<a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>]</p>`)
  218. test(
  219. "info@gitea.com.",
  220. `<p><a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>.</p>`)
  221. test(
  222. "firstname+lastname@gitea.com",
  223. `<p><a href="mailto:firstname+lastname@gitea.com" rel="nofollow">firstname+lastname@gitea.com</a></p>`)
  224. test(
  225. "send email to info@gitea.co.uk.",
  226. `<p>send email to <a href="mailto:info@gitea.co.uk" rel="nofollow">info@gitea.co.uk</a>.</p>`)
  227. test(
  228. `j.doe@example.com,
  229. j.doe@example.com.
  230. j.doe@example.com;
  231. j.doe@example.com?
  232. j.doe@example.com!`,
  233. `<p><a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>,
  234. <a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>.
  235. <a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>;
  236. <a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>?
  237. <a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>!</p>`)
  238. // match GitHub behavior
  239. test("email@domain@domain.com", `<p>email@<a href="mailto:domain@domain.com" rel="nofollow">domain@domain.com</a></p>`)
  240. // match GitHub behavior
  241. test(`"info@gitea.com"`, `<p>&#34;<a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>&#34;</p>`)
  242. // Test that should *not* be turned into email links
  243. test(
  244. "/home/gitea/mailstore/info@gitea/com",
  245. `<p>/home/gitea/mailstore/info@gitea/com</p>`)
  246. test(
  247. "git@try.gitea.io:go-gitea/gitea.git",
  248. `<p>git@try.gitea.io:go-gitea/gitea.git</p>`)
  249. test(
  250. "https://foo:bar@gitea.io",
  251. `<p><a href="https://foo:bar@gitea.io" rel="nofollow">https://foo:bar@gitea.io</a></p>`)
  252. test(
  253. "gitea@3",
  254. `<p>gitea@3</p>`)
  255. test(
  256. "gitea@gmail.c",
  257. `<p>gitea@gmail.c</p>`)
  258. test(
  259. "email@domain..com",
  260. `<p>email@domain..com</p>`)
  261. cases := []struct {
  262. input, expected string
  263. }{
  264. // match GitHub behavior
  265. {"?a@d.zz", `<p>?<a href="mailto:a@d.zz" rel="nofollow">a@d.zz</a></p>`},
  266. {"*a@d.zz", `<p>*<a href="mailto:a@d.zz" rel="nofollow">a@d.zz</a></p>`},
  267. {"~a@d.zz", `<p>~<a href="mailto:a@d.zz" rel="nofollow">a@d.zz</a></p>`},
  268. // the following cases don't match GitHub behavior, but they are valid email addresses ...
  269. // maybe we should reduce the candidate characters for the "name" part in the future
  270. {"a*a@d.zz", `<p><a href="mailto:a*a@d.zz" rel="nofollow">a*a@d.zz</a></p>`},
  271. {"a~a@d.zz", `<p><a href="mailto:a~a@d.zz" rel="nofollow">a~a@d.zz</a></p>`},
  272. }
  273. for _, c := range cases {
  274. test(c.input, c.expected)
  275. }
  276. }
  277. func TestRender_emoji(t *testing.T) {
  278. setting.AppURL = markup.TestAppURL
  279. setting.StaticURLPrefix = markup.TestAppURL
  280. test := func(input, expected string) {
  281. expected = strings.ReplaceAll(expected, "&", "&amp;")
  282. buffer, err := markup.RenderString(markup.NewTestRenderContext().WithRelativePath("a.md"), input)
  283. assert.NoError(t, err)
  284. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  285. }
  286. // Make sure we can successfully match every emoji in our dataset with regex
  287. for i := range emoji.GemojiData {
  288. test(
  289. emoji.GemojiData[i].Emoji,
  290. `<p><span class="emoji" aria-label="`+emoji.GemojiData[i].Description+`">`+emoji.GemojiData[i].Emoji+`</span></p>`)
  291. }
  292. for i := range emoji.GemojiData {
  293. test(
  294. ":"+emoji.GemojiData[i].Aliases[0]+":",
  295. `<p><span class="emoji" aria-label="`+emoji.GemojiData[i].Description+`">`+emoji.GemojiData[i].Emoji+`</span></p>`)
  296. }
  297. // Text that should be turned into or recognized as emoji
  298. test(
  299. ":gitea:",
  300. `<p><span class="emoji" aria-label="gitea"><img alt=":gitea:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/gitea.png"/></span></p>`)
  301. test(
  302. ":custom-emoji:",
  303. `<p>:custom-emoji:</p>`)
  304. setting.UI.CustomEmojisMap["custom-emoji"] = ":custom-emoji:"
  305. test(
  306. ":custom-emoji:",
  307. `<p><span class="emoji" aria-label="custom-emoji"><img alt=":custom-emoji:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/custom-emoji.png"/></span></p>`)
  308. test(
  309. "这是字符:1::+1: some🐊 \U0001f44d:custom-emoji: :gitea:",
  310. `<p>这是字符:1:<span class="emoji" aria-label="thumbs up">👍</span> some<span class="emoji" aria-label="crocodile">🐊</span> `+
  311. `<span class="emoji" aria-label="thumbs up">👍</span><span class="emoji" aria-label="custom-emoji"><img alt=":custom-emoji:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/custom-emoji.png"/></span> `+
  312. `<span class="emoji" aria-label="gitea"><img alt=":gitea:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/gitea.png"/></span></p>`)
  313. test(
  314. "Some text with 😄 in the middle",
  315. `<p>Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span> in the middle</p>`)
  316. test(
  317. "Some text with :smile: in the middle",
  318. `<p>Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span> in the middle</p>`)
  319. test(
  320. "Some text with 😄😄 2 emoji next to each other",
  321. `<p>Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span><span class="emoji" aria-label="grinning face with smiling eyes">😄</span> 2 emoji next to each other</p>`)
  322. test(
  323. "😎🤪🔐🤑❓",
  324. `<p><span class="emoji" aria-label="smiling face with sunglasses">😎</span><span class="emoji" aria-label="zany face">🤪</span><span class="emoji" aria-label="locked with key">🔐</span><span class="emoji" aria-label="money-mouth face">🤑</span><span class="emoji" aria-label="red question mark">❓</span></p>`)
  325. // should match nothing
  326. test(
  327. "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
  328. `<p>2001:0db8:85a3:0000:0000:8a2e:0370:7334</p>`)
  329. test(
  330. ":not exist:",
  331. `<p>:not exist:</p>`)
  332. }
  333. func TestRender_ShortLinks(t *testing.T) {
  334. setting.AppURL = markup.TestAppURL
  335. tree := util.URLJoin(markup.TestRepoURL, "src", "master")
  336. test := func(input, expected string) {
  337. buffer, err := markdown.RenderString(markup.NewTestRenderContext(tree), input)
  338. assert.NoError(t, err)
  339. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  340. }
  341. url := util.URLJoin(tree, "Link")
  342. otherURL := util.URLJoin(tree, "Other-Link")
  343. encodedURL := util.URLJoin(tree, "Link%3F")
  344. imgurl := util.URLJoin(tree, "Link.jpg")
  345. otherImgurl := util.URLJoin(tree, "Link+Other.jpg")
  346. encodedImgurl := util.URLJoin(tree, "Link+%23.jpg")
  347. notencodedImgurl := util.URLJoin(tree, "some", "path", "Link+#.jpg")
  348. renderableFileURL := util.URLJoin(tree, "markdown_file.md")
  349. unrenderableFileURL := util.URLJoin(tree, "file.zip")
  350. favicon := "http://google.com/favicon.ico"
  351. test(
  352. "[[Link]]",
  353. `<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
  354. )
  355. test(
  356. "[[Link.-]]",
  357. `<p><a href="http://localhost:3000/test-owner/test-repo/src/master/Link.-" rel="nofollow">Link.-</a></p>`,
  358. )
  359. test(
  360. "[[Link.jpg]]",
  361. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Link.jpg" alt="Link.jpg"/></a></p>`,
  362. )
  363. test(
  364. "[["+favicon+"]]",
  365. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico" alt="`+favicon+`"/></a></p>`,
  366. )
  367. test(
  368. "[[Name|Link]]",
  369. `<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
  370. )
  371. test(
  372. "[[Name|Link.jpg]]",
  373. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Name" alt="Name"/></a></p>`,
  374. )
  375. test(
  376. "[[Name|Link.jpg|alt=AltName]]",
  377. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="AltName" alt="AltName"/></a></p>`,
  378. )
  379. test(
  380. "[[Name|Link.jpg|title=Title]]",
  381. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="Title"/></a></p>`,
  382. )
  383. test(
  384. "[[Name|Link.jpg|alt=AltName|title=Title]]",
  385. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  386. )
  387. test(
  388. "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
  389. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  390. )
  391. test(
  392. "[[Name|Link Other.jpg|alt=\"AltName\"|title='Title']]",
  393. `<p><a href="`+otherImgurl+`" rel="nofollow"><img src="`+otherImgurl+`" title="Title" alt="AltName"/></a></p>`,
  394. )
  395. test(
  396. "[[Link]] [[Other Link]]",
  397. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a></p>`,
  398. )
  399. test(
  400. "[[Link?]]",
  401. `<p><a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  402. )
  403. test(
  404. "[[Link]] [[Other Link]] [[Link?]]",
  405. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a> <a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  406. )
  407. test(
  408. "[[markdown_file.md]]",
  409. `<p><a href="`+renderableFileURL+`" rel="nofollow">markdown_file.md</a></p>`,
  410. )
  411. test(
  412. "[[file.zip]]",
  413. `<p><a href="`+unrenderableFileURL+`" rel="nofollow">file.zip</a></p>`,
  414. )
  415. test(
  416. "[[Link #.jpg]]",
  417. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Link #.jpg" alt="Link #.jpg"/></a></p>`,
  418. )
  419. test(
  420. "[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]",
  421. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Title" alt="AltName"/></a></p>`,
  422. )
  423. test(
  424. "[[some/path/Link #.jpg]]",
  425. `<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`" title="Link #.jpg" alt="some/path/Link #.jpg"/></a></p>`,
  426. )
  427. test(
  428. "<p><a href=\"https://example.org\">[[foobar]]</a></p>",
  429. `<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`,
  430. )
  431. }
  432. func Test_ParseClusterFuzz(t *testing.T) {
  433. setting.AppURL = markup.TestAppURL
  434. localMetas := map[string]string{"user": "go-gitea", "repo": "gitea"}
  435. data := "<A><maTH><tr><MN><bodY ÿ><temPlate></template><tH><tr></A><tH><d<bodY "
  436. var res strings.Builder
  437. err := markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
  438. assert.NoError(t, err)
  439. assert.NotContains(t, res.String(), "<html")
  440. data = "<!DOCTYPE html>\n<A><maTH><tr><MN><bodY ÿ><temPlate></template><tH><tr></A><tH><d<bodY "
  441. res.Reset()
  442. err = markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
  443. assert.NoError(t, err)
  444. assert.NotContains(t, res.String(), "<html")
  445. }
  446. func TestPostProcess(t *testing.T) {
  447. setting.StaticURLPrefix = markup.TestAppURL // can't run standalone
  448. defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
  449. test := func(input, expected string) {
  450. var res strings.Builder
  451. err := markup.PostProcessDefault(markup.NewTestRenderContext(markup.TestAppURL, map[string]string{"user": "go-gitea", "repo": "gitea"}), strings.NewReader(input), &res)
  452. assert.NoError(t, err)
  453. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res.String()))
  454. }
  455. // Issue index shouldn't be post-processing in a document.
  456. test(
  457. "#1",
  458. "#1")
  459. // But cross-referenced issue index should work.
  460. test(
  461. "go-gitea/gitea#12345",
  462. `<a href="/go-gitea/gitea/issues/12345" class="ref-issue">go-gitea/gitea#12345</a>`)
  463. // Test that other post-processing still works.
  464. test(
  465. ":gitea:",
  466. `<span class="emoji" aria-label="gitea"><img alt=":gitea:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/gitea.png"/></span>`)
  467. test(
  468. "Some text with 😄 in the middle",
  469. `Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span> in the middle`)
  470. test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
  471. `<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="ref-issue">person/repo#4 (comment)</a>`)
  472. // special tags, GitHub's behavior, and for unclosed tags, output as text content as much as possible
  473. test("<script>a", `&lt;script&gt;a`)
  474. test("<script>a</script>", `&lt;script&gt;a&lt;/script&gt;`)
  475. test("<STYLE>a", `&lt;STYLE&gt;a`)
  476. test("<style>a</STYLE>", `&lt;style&gt;a&lt;/STYLE&gt;`)
  477. // other special tags, our special behavior
  478. test("<?php\nfoo", "&lt;?php\nfoo")
  479. test("<%asp\nfoo", "&lt;%asp\nfoo")
  480. }
  481. func TestIssue16020(t *testing.T) {
  482. setting.AppURL = markup.TestAppURL
  483. localMetas := map[string]string{
  484. "user": "go-gitea",
  485. "repo": "gitea",
  486. }
  487. data := `<img src="data:image/png;base64,i//V"/>`
  488. var res strings.Builder
  489. err := markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
  490. assert.NoError(t, err)
  491. assert.Equal(t, data, res.String())
  492. }
  493. func BenchmarkEmojiPostprocess(b *testing.B) {
  494. data := "🥰 "
  495. for len(data) < 1<<16 {
  496. data += data
  497. }
  498. b.ResetTimer()
  499. for b.Loop() {
  500. var res strings.Builder
  501. err := markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
  502. assert.NoError(b, err)
  503. }
  504. }
  505. func TestFuzz(t *testing.T) {
  506. s := "t/l/issues/8#/../../a"
  507. renderContext := markup.NewTestRenderContext()
  508. err := markup.PostProcessDefault(renderContext, strings.NewReader(s), io.Discard)
  509. assert.NoError(t, err)
  510. }
  511. func TestIssue18471(t *testing.T) {
  512. data := `http://domain/org/repo/compare/783b039...da951ce`
  513. var res strings.Builder
  514. err := markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
  515. assert.NoError(t, err)
  516. assert.Equal(t, `<a href="http://domain/org/repo/compare/783b039...da951ce" class="compare"><code>783b039...da951ce</code></a>`, res.String())
  517. }
  518. func TestIsFullURL(t *testing.T) {
  519. assert.True(t, markup.IsFullURLString("https://example.com"))
  520. assert.True(t, markup.IsFullURLString("mailto:test@example.com"))
  521. assert.True(t, markup.IsFullURLString("data:image/11111"))
  522. assert.False(t, markup.IsFullURLString("/foo:bar"))
  523. }