From c1f76aea45f11e1d5ae22c047cf3bda9c681de8d Mon Sep 17 00:00:00 2001 From: Rafael Date: Wed, 10 Apr 2024 18:49:57 +0100 Subject: [PATCH] Use raw Wiki links for non-renderable Wiki files (#30273) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In Wiki pages, short-links created to local Wiki files were always expanded as regular Wiki Links. In particular, if a link wanted to point to a file that Gitea doesn't know how to render (e.g, a .zip file), a user following the link would be silently redirected to the Wiki's home page. This change makes short-links* in Wiki pages be expanded to raw wiki links, so these local wiki files may be accessed without manually accessing their URL. * only short-links ending in a file extension that isn't renderable are affected. Closes #27121. Signed-off-by: Rafael Girão Co-authored-by: silverwind --- modules/markup/html.go | 22 +++++++++++++--- modules/markup/html_test.go | 12 +++++++++ modules/markup/markdown/markdown_test.go | 24 +++++++++--------- modules/markup/markdown/transform_link.go | 13 +++++++++- routers/web/repo/wiki_test.go | 13 +++++----- .../81/a1c039774e337621609336c0e44ed9f92278f7 | Bin 0 -> 68 bytes .../91/dc55f9de16a558e859123f2b99668469b1a1dc | Bin 0 -> 234 bytes .../a5/bbc0fd39a696feabed2d4cccaf05abbcaf3b02 | 1 + .../cf/19952a40b92eb2f86689146a65ac2d87c0818a | Bin 0 -> 255 bytes .../e1/6da91326b845f1ba86a7df0a67db352f96dcb0 | Bin 0 -> 149 bytes .../user2/repo1.wiki.git/refs/heads/master | 2 +- tests/integration/git_clone_wiki_test.go | 1 + 12 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 tests/gitea-repositories-meta/user2/repo1.wiki.git/objects/81/a1c039774e337621609336c0e44ed9f92278f7 create mode 100644 tests/gitea-repositories-meta/user2/repo1.wiki.git/objects/91/dc55f9de16a558e859123f2b99668469b1a1dc create mode 100644 tests/gitea-repositories-meta/user2/repo1.wiki.git/objects/a5/bbc0fd39a696feabed2d4cccaf05abbcaf3b02 create mode 100644 tests/gitea-repositories-meta/user2/repo1.wiki.git/objects/cf/19952a40b92eb2f86689146a65ac2d87c0818a create mode 100644 tests/gitea-repositories-meta/user2/repo1.wiki.git/objects/e1/6da91326b845f1ba86a7df0a67db352f96dcb0 diff --git a/modules/markup/html.go b/modules/markup/html.go index 56aa1cb49c..cef643bf18 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -709,7 +709,8 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) { name += tail image := false - switch ext := filepath.Ext(link); ext { + ext := filepath.Ext(link) + switch ext { // fast path: empty string, ignore case "": // leave image as false @@ -767,11 +768,26 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) { } } else { if !absoluteLink { + var base string if ctx.IsWiki { - link = util.URLJoin(ctx.Links.WikiLink(), link) + switch ext { + case "": + // no file extension, create a regular wiki link + base = ctx.Links.WikiLink() + default: + // we have a file extension: + // return a regular wiki link if it's a renderable file (extension), + // raw link otherwise + if Type(link) != "" { + base = ctx.Links.WikiLink() + } else { + base = ctx.Links.WikiRawLink() + } + } } else { - link = util.URLJoin(ctx.Links.SrcLink(), link) + base = ctx.Links.SrcLink() } + link = util.URLJoin(base, link) } childNode.Type = html.TextNode childNode.Data = name diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 55de65d196..916e74fb62 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -427,6 +427,10 @@ func TestRender_ShortLinks(t *testing.T) { otherImgurlWiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw", "Link+Other.jpg") encodedImgurlWiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw", "Link+%23.jpg") notencodedImgurlWiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw", "some", "path", "Link+#.jpg") + renderableFileURL := util.URLJoin(tree, "markdown_file.md") + renderableFileURLWiki := util.URLJoin(markup.TestRepoURL, "wiki", "markdown_file.md") + unrenderableFileURL := util.URLJoin(tree, "file.zip") + unrenderableFileURLWiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw", "file.zip") favicon := "http://google.com/favicon.ico" test( @@ -481,6 +485,14 @@ func TestRender_ShortLinks(t *testing.T) { "[[Link]] [[Other Link]] [[Link?]]", `

Link Other Link Link?

`, `

Link Other Link Link?

`) + test( + "[[markdown_file.md]]", + `

markdown_file.md

`, + `

markdown_file.md

`) + test( + "[[file.zip]]", + `

file.zip

`, + `

file.zip

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

Link #.jpg

`, diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index a9c9024982..d9b67e43af 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -653,9 +653,9 @@ space

Expected: `

space @mention-user
/just/a/path.bin
https://example.com/file.bin
-local link
+local link
remote link
-local link
+local link
remote link
local image
local image
@@ -711,9 +711,9 @@ space

Expected: `

space @mention-user
/just/a/path.bin
https://example.com/file.bin
-local link
+local link
remote link
-local link
+local link
remote link
local image
local image
@@ -769,9 +769,9 @@ space

Expected: `

space @mention-user
/just/a/path.bin
https://example.com/file.bin
-local link
+local link
remote link
-local link
+local link
remote link
local image
local image
@@ -829,9 +829,9 @@ space

Expected: `

space @mention-user
/just/a/path.bin
https://example.com/file.bin
-local link
+local link
remote link
-local link
+local link
remote link
local image
local image
@@ -889,9 +889,9 @@ space

Expected: `

space @mention-user
/just/a/path.bin
https://example.com/file.bin
-local link
+local link
remote link
-local link
+local link
remote link
local image
local image
@@ -951,9 +951,9 @@ space

Expected: `

space @mention-user
/just/a/path.bin
https://example.com/file.bin
-local link
+local link
remote link
-local link
+local link
remote link
local image
local image
diff --git a/modules/markup/markdown/transform_link.go b/modules/markup/markdown/transform_link.go index 8bf19ea4ce..7e305b74bc 100644 --- a/modules/markup/markdown/transform_link.go +++ b/modules/markup/markdown/transform_link.go @@ -4,6 +4,8 @@ package markdown import ( + "path/filepath" + "code.gitea.io/gitea/modules/markup" giteautil "code.gitea.io/gitea/modules/util" @@ -18,7 +20,16 @@ func (g *ASTTransformer) transformLink(ctx *markup.RenderContext, v *ast.Link, r if !isAnchorFragment && !markup.IsFullURLBytes(link) { base := ctx.Links.Base if ctx.IsWiki { - base = ctx.Links.WikiLink() + if filepath.Ext(string(link)) == "" { + // This link doesn't have a file extension - assume a regular wiki link + base = ctx.Links.WikiLink() + } else if markup.Type(string(link)) != "" { + // If it's a file type we can render, use a regular wiki link + base = ctx.Links.WikiLink() + } else { + // Otherwise, use a raw link instead + base = ctx.Links.WikiRawLink() + } } else if ctx.Links.HasBranchInfo() { base = ctx.Links.SrcLink() } diff --git a/routers/web/repo/wiki_test.go b/routers/web/repo/wiki_test.go index 2894c06fbd..4602dcfeb4 100644 --- a/routers/web/repo/wiki_test.go +++ b/routers/web/repo/wiki_test.go @@ -200,12 +200,13 @@ func TestDeleteWikiPagePost(t *testing.T) { func TestWikiRaw(t *testing.T) { for filepath, filetype := range map[string]string{ - "jpeg.jpg": "image/jpeg", - "images/jpeg.jpg": "image/jpeg", - "Page With Spaced Name": "text/plain; charset=utf-8", - "Page-With-Spaced-Name": "text/plain; charset=utf-8", - "Page With Spaced Name.md": "", // there is no "Page With Spaced Name.md" in repo - "Page-With-Spaced-Name.md": "text/plain; charset=utf-8", + "jpeg.jpg": "image/jpeg", + "images/jpeg.jpg": "image/jpeg", + "files/Non-Renderable-File.zip": "application/octet-stream", + "Page With Spaced Name": "text/plain; charset=utf-8", + "Page-With-Spaced-Name": "text/plain; charset=utf-8", + "Page With Spaced Name.md": "", // there is no "Page With Spaced Name.md" in repo + "Page-With-Spaced-Name.md": "text/plain; charset=utf-8", } { unittest.PrepareTestEnv(t) diff --git a/tests/gitea-repositories-meta/user2/repo1.wiki.git/objects/81/a1c039774e337621609336c0e44ed9f92278f7 b/tests/gitea-repositories-meta/user2/repo1.wiki.git/objects/81/a1c039774e337621609336c0e44ed9f92278f7 new file mode 100644 index 0000000000000000000000000000000000000000..17a5547da8286e051f08cb79be1169efacb59654 GIT binary patch literal 68 zcmV-K0K5Nq0V^p=O;s>8WH2-^Ff%bx@XOEB4NA>RNi9lD%1PCA%gjmDtI8~3c$m9V aSZ#;v$6am9?{lT!Hr1bYX9EC2Q5Lqqq8?@d literal 0 HcmV?d00001 diff --git a/tests/gitea-repositories-meta/user2/repo1.wiki.git/objects/91/dc55f9de16a558e859123f2b99668469b1a1dc b/tests/gitea-repositories-meta/user2/repo1.wiki.git/objects/91/dc55f9de16a558e859123f2b99668469b1a1dc new file mode 100644 index 0000000000000000000000000000000000000000..8390a40c0805716854172c1d6c26de3e3845cbf6 GIT binary patch literal 234 zcmV5w`4FhFfcPQQSivmP1VayVR+T_r@efUH~XP%EAKClN_3cu z?N&pT1SF=X>V{{QWaxV40+}GyMzKeJyfpDk%lIT}wrp;^tlw3e#TcrC3lfu4Q*`|j zAvP5KNeVoZ(l^mZfMKqov;3mR-Ln*+dP4J3i<1)zQd1P%GIMY`$HV{#6w-hyiWwRg z9H z%ir?<3b7zI{+7Xv1#6*+OydQjTw3c!jr8XSv4cjr%R-khhg>l*l zr^xF;Dc(^hq!&uiByGp1(cmN)9%YFMuIQ0g_z3CiGs0_n$R;;)NEk1L>=tZnjx}Tx zvDwQTaK*VC)hIN)bhVg$AQ$=>Ccm FYv@Vae2V}8 literal 0 HcmV?d00001 diff --git a/tests/gitea-repositories-meta/user2/repo1.wiki.git/objects/e1/6da91326b845f1ba86a7df0a67db352f96dcb0 b/tests/gitea-repositories-meta/user2/repo1.wiki.git/objects/e1/6da91326b845f1ba86a7df0a67db352f96dcb0 new file mode 100644 index 0000000000000000000000000000000000000000..da281ff7916dcb6dac5154f4d87bab9af4428b29 GIT binary patch literal 149 zcmV;G0BZku0ZYosPf{?nG+_wvW@Zs#U|`^2_|;Mq!FtVSryP){0>tbLG7KL1xv6@& zDWM^p49pR8S<^tcw1S&~k>v$50|QG6P+4%;hZE<0Lp3~n{GUGJI(b4TjA7BFo{(0a z85@+ufyOg3$uZ-yKmw?rfq@Z-mo$P{aO+qh)}dJy;LXYgQpN~`zCb!2#9;scqk${9 Dp4URN literal 0 HcmV?d00001 diff --git a/tests/gitea-repositories-meta/user2/repo1.wiki.git/refs/heads/master b/tests/gitea-repositories-meta/user2/repo1.wiki.git/refs/heads/master index 38984b12b7..b352f15003 100644 --- a/tests/gitea-repositories-meta/user2/repo1.wiki.git/refs/heads/master +++ b/tests/gitea-repositories-meta/user2/repo1.wiki.git/refs/heads/master @@ -1 +1 @@ -0dca5bd9b5d7ef937710e056f575e86c0184ba85 +a5bbc0fd39a696feabed2d4cccaf05abbcaf3b02 diff --git a/tests/integration/git_clone_wiki_test.go b/tests/integration/git_clone_wiki_test.go index d7949dfe25..ef662300f3 100644 --- a/tests/integration/git_clone_wiki_test.go +++ b/tests/integration/git_clone_wiki_test.go @@ -45,6 +45,7 @@ func TestRepoCloneWiki(t *testing.T) { assertFileExist(t, filepath.Join(dstPath, "Page-With-Image.md")) assertFileExist(t, filepath.Join(dstPath, "Page-With-Spaced-Name.md")) assertFileExist(t, filepath.Join(dstPath, "images")) + assertFileExist(t, filepath.Join(dstPath, "files/Non-Renderable-File.zip")) assertFileExist(t, filepath.Join(dstPath, "jpeg.jpg")) }) })