diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-03-08 20:17:39 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-08 20:17:39 +0800 |
commit | b116418f05b822481bba3613873eef876da73814 (patch) | |
tree | 65d1d88d940c26029e7ef920c4dd30761763ce46 /modules/util | |
parent | 090e75392385041b3abb30d02564962a3ff687f6 (diff) | |
download | gitea-b116418f05b822481bba3613873eef876da73814.tar.gz gitea-b116418f05b822481bba3613873eef876da73814.zip |
Use CleanPath instead of path.Clean (#23371)
As title.
Diffstat (limited to 'modules/util')
-rw-r--r-- | modules/util/path.go | 8 | ||||
-rw-r--r-- | modules/util/path_test.go | 12 |
2 files changed, 20 insertions, 0 deletions
diff --git a/modules/util/path.go b/modules/util/path.go index 74acb7a85f..5aa9e15f5c 100644 --- a/modules/util/path.go +++ b/modules/util/path.go @@ -14,6 +14,14 @@ import ( "strings" ) +// CleanPath ensure to clean the path +func CleanPath(p string) string { + if strings.HasPrefix(p, "/") { + return path.Clean(p) + } + return path.Clean("/" + p)[1:] +} + // EnsureAbsolutePath ensure that a path is absolute, making it // relative to absoluteBase if necessary func EnsureAbsolutePath(path, absoluteBase string) string { diff --git a/modules/util/path_test.go b/modules/util/path_test.go index 93f4f67cf6..2f020f924d 100644 --- a/modules/util/path_test.go +++ b/modules/util/path_test.go @@ -136,3 +136,15 @@ func TestMisc_IsReadmeFileName(t *testing.T) { assert.Equal(t, testCase.idx, idx) } } + +func TestCleanPath(t *testing.T) { + cases := map[string]string{ + "../../test": "test", + "/test": "/test", + "/../test": "/test", + } + + for k, v := range cases { + assert.Equal(t, v, CleanPath(k)) + } +} |