Skip to content

Commit 43d1183

Browse files
yardenshohamsillyguodongzeripath
authored
escape filename when assemble URL (#22850) (#22871)
Backport #22850 Fixes: #22843 ### Cause: https://github.com/go-gitea/gitea/blob/affdd40296960a08a4223330ccbd1fb88c96ea1a/services/repository/files/content.go#L161 Previously, we did not escape the **"%"** that might be in "treePath" when call "url.parse()". ![image](https://user-images.githubusercontent.com/33891828/218066318-5a909e50-2a17-46e6-b32f-684b2aa4b91f.png) This function will check whether "%" is the beginning of an escape character. Obviously, the "%" in the example (hello%mother.txt) is not that. So, the function will return a error. ### Solution: We can escape "treePath" by call "url.PathEscape()" function firstly. ### Screenshot: ![image](https://user-images.githubusercontent.com/33891828/218069781-1a030f8b-18d0-4804-b0f8-73997849ef43.png) Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: sillyguodong <33891828+sillyguodong@users.noreply.github.com> Co-authored-by: Andrew Thornton <art27@cantab.net>
1 parent 8fa419c commit 43d1183

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

modules/lfs/endpoint.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
package lfs
66

77
import (
8-
"fmt"
98
"net/url"
109
"os"
1110
"path"
1211
"path/filepath"
1312
"strings"
1413

1514
"code.gitea.io/gitea/modules/log"
15+
"code.gitea.io/gitea/modules/util"
1616
)
1717

1818
// DetermineEndpoint determines an endpoint from the clone url or uses the specified LFS url.
@@ -96,7 +96,7 @@ func endpointFromLocalPath(path string) *url.URL {
9696
return nil
9797
}
9898

99-
path = fmt.Sprintf("file://%s%s", slash, filepath.ToSlash(path))
99+
path = "file://" + slash + util.PathEscapeSegments(filepath.ToSlash(path))
100100

101101
u, _ := url.Parse(path)
102102

services/repository/files/content.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/git"
1717
"code.gitea.io/gitea/modules/setting"
1818
api "code.gitea.io/gitea/modules/structs"
19+
"code.gitea.io/gitea/modules/util"
1920
)
2021

2122
// ContentType repo content type
@@ -159,7 +160,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
159160
return nil, fmt.Errorf("no commit found for the ref [ref: %s]", ref)
160161
}
161162

162-
selfURL, err := url.Parse(fmt.Sprintf("%s/contents/%s?ref=%s", repo.APIURL(), treePath, origRef))
163+
selfURL, err := url.Parse(repo.APIURL() + "/contents/" + util.PathEscapeSegments(treePath) + "?ref=" + url.QueryEscape(origRef))
163164
if err != nil {
164165
return nil, err
165166
}
@@ -218,23 +219,23 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
218219
}
219220
// Handle links
220221
if entry.IsRegular() || entry.IsLink() {
221-
downloadURL, err := url.Parse(fmt.Sprintf("%s/raw/%s/%s/%s", repo.HTMLURL(), refType, ref, treePath))
222+
downloadURL, err := url.Parse(repo.HTMLURL() + "/raw/" + url.PathEscape(string(refType)) + "/" + util.PathEscapeSegments(ref) + "/" + util.PathEscapeSegments(treePath))
222223
if err != nil {
223224
return nil, err
224225
}
225226
downloadURLString := downloadURL.String()
226227
contentsResponse.DownloadURL = &downloadURLString
227228
}
228229
if !entry.IsSubModule() {
229-
htmlURL, err := url.Parse(fmt.Sprintf("%s/src/%s/%s/%s", repo.HTMLURL(), refType, ref, treePath))
230+
htmlURL, err := url.Parse(repo.HTMLURL() + "/src/" + url.PathEscape(string(refType)) + "/" + util.PathEscapeSegments(ref) + "/" + util.PathEscapeSegments(treePath))
230231
if err != nil {
231232
return nil, err
232233
}
233234
htmlURLString := htmlURL.String()
234235
contentsResponse.HTMLURL = &htmlURLString
235236
contentsResponse.Links.HTMLURL = &htmlURLString
236237

237-
gitURL, err := url.Parse(fmt.Sprintf("%s/git/blobs/%s", repo.APIURL(), entry.ID.String()))
238+
gitURL, err := url.Parse(repo.APIURL() + "/git/blobs/" + url.PathEscape(entry.ID.String()))
238239
if err != nil {
239240
return nil, err
240241
}

0 commit comments

Comments
 (0)