Skip to content

Commit 51ab495

Browse files
escape filename when assemble URL (#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 <[email protected]> Co-authored-by: Andrew Thornton <[email protected]>
1 parent 8fa54d0 commit 51ab495

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

Diff for: modules/lfs/endpoint.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
package lfs
55

66
import (
7-
"fmt"
87
"net/url"
98
"os"
109
"path"
1110
"path/filepath"
1211
"strings"
1312

1413
"code.gitea.io/gitea/modules/log"
14+
"code.gitea.io/gitea/modules/util"
1515
)
1616

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

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

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

Diff for: services/repository/files/content.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/modules/git"
1616
"code.gitea.io/gitea/modules/setting"
1717
api "code.gitea.io/gitea/modules/structs"
18+
"code.gitea.io/gitea/modules/util"
1819
)
1920

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

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

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

0 commit comments

Comments
 (0)