Skip to content

Commit 6211415

Browse files
committed
use SafeFilePathAbs instead of path.Join for local file accessing
1 parent 9dd4db0 commit 6211415

File tree

3 files changed

+35
-53
lines changed

3 files changed

+35
-53
lines changed

modules/options/base.go

+24-6
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,36 @@ import (
77
"fmt"
88
"io/fs"
99
"os"
10-
"path"
1110
"path/filepath"
1211

12+
"code.gitea.io/gitea/modules/log"
1313
"code.gitea.io/gitea/modules/setting"
1414
"code.gitea.io/gitea/modules/util"
1515
)
1616

1717
// Locale reads the content of a specific locale from static/bindata or custom path.
1818
func Locale(name string) ([]byte, error) {
19-
return fileFromDir(path.Join("locale", util.SafePathRel(name)))
19+
return fileFromOptionsDir("locale", name)
2020
}
2121

2222
// Readme reads the content of a specific readme from static/bindata or custom path.
2323
func Readme(name string) ([]byte, error) {
24-
return fileFromDir(path.Join("readme", util.SafePathRel(name)))
24+
return fileFromOptionsDir("readme", name)
2525
}
2626

2727
// Gitignore reads the content of a gitignore locale from static/bindata or custom path.
2828
func Gitignore(name string) ([]byte, error) {
29-
return fileFromDir(path.Join("gitignore", util.SafePathRel(name)))
29+
return fileFromOptionsDir("gitignore", name)
3030
}
3131

3232
// License reads the content of a specific license from static/bindata or custom path.
3333
func License(name string) ([]byte, error) {
34-
return fileFromDir(path.Join("license", util.SafePathRel(name)))
34+
return fileFromOptionsDir("license", name)
3535
}
3636

3737
// Labels reads the content of a specific labels from static/bindata or custom path.
3838
func Labels(name string) ([]byte, error) {
39-
return fileFromDir(path.Join("label", util.SafePathRel(name)))
39+
return fileFromOptionsDir("label", name)
4040
}
4141

4242
// WalkLocales reads the content of a specific locale
@@ -93,3 +93,21 @@ func statDirIfExist(dir string) ([]string, error) {
9393
}
9494
return files, nil
9595
}
96+
97+
func readFileFromLocal(base []string, sub string, elems ...string) ([]byte, error) {
98+
localPathElems := make([]string, len(elems)+2) // path[0] will be used for the custom path prefix
99+
localPathElems[1] = sub
100+
copy(localPathElems[2:], elems)
101+
102+
for _, dir := range base {
103+
localPathElems[0] = dir
104+
localPath := util.SafeFilePathAbs(localPathElems...)
105+
isFile, err := util.IsFile(localPath)
106+
if err != nil {
107+
log.Error("Unable to check if %s is a file. Error: %v", localPath, err)
108+
} else if isFile {
109+
return os.ReadFile(localPath)
110+
}
111+
}
112+
return nil, fmt.Errorf("asset file does not exist: %v", elems)
113+
}

modules/options/dynamic.go

+5-30
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
package options
77

88
import (
9-
"fmt"
10-
"os"
11-
"path"
12-
13-
"code.gitea.io/gitea/modules/log"
149
"code.gitea.io/gitea/modules/setting"
1510
"code.gitea.io/gitea/modules/util"
1611
)
@@ -26,8 +21,8 @@ func Dir(name string) ([]string, error) {
2621
var result []string
2722

2823
for _, dir := range []string{
29-
path.Join(setting.CustomPath, "options", name), // custom dir
30-
path.Join(setting.StaticRootPath, "options", name), // static dir
24+
util.SafeFilePathAbs(setting.CustomPath, "options", name), // custom dir
25+
util.SafeFilePathAbs(setting.StaticRootPath, "options", name), // static dir
3126
} {
3227
files, err := statDirIfExist(dir)
3328
if err != nil {
@@ -39,29 +34,9 @@ func Dir(name string) ([]string, error) {
3934
return directories.AddAndGet(name, result), nil
4035
}
4136

42-
// fileFromDir is a helper to read files from static or custom path.
43-
func fileFromDir(name string) ([]byte, error) {
44-
customPath := path.Join(setting.CustomPath, "options", name)
45-
46-
isFile, err := util.IsFile(customPath)
47-
if err != nil {
48-
log.Error("Unable to check if %s is a file. Error: %v", customPath, err)
49-
}
50-
if isFile {
51-
return os.ReadFile(customPath)
52-
}
53-
54-
staticPath := path.Join(setting.StaticRootPath, "options", name)
55-
56-
isFile, err = util.IsFile(staticPath)
57-
if err != nil {
58-
log.Error("Unable to check if %s is a file. Error: %v", staticPath, err)
59-
}
60-
if isFile {
61-
return os.ReadFile(staticPath)
62-
}
63-
64-
return []byte{}, fmt.Errorf("Asset file does not exist: %s", name)
37+
// fileFromOptionsDir is a helper to read files from static or custom path.
38+
func fileFromOptionsDir(elems ...string) ([]byte, error) {
39+
return readFileFromLocal([]string{setting.CustomPath, setting.StaticRootPath}, "options", elems...)
6540
}
6641

6742
// IsDynamic will return false when using embedded data (-tags bindata)

modules/options/static.go

+6-17
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@ package options
88
import (
99
"fmt"
1010
"io"
11-
"os"
12-
"path"
1311

14-
"code.gitea.io/gitea/modules/log"
1512
"code.gitea.io/gitea/modules/setting"
16-
"code.gitea.io/gitea/modules/util"
1713
)
1814

1915
var directories = make(directorySet)
2016

21-
// Dir returns all files from bindata or custom directory.
17+
// Dir returns all files from custom directory or bindata.
2218
func Dir(name string) ([]string, error) {
2319
if directories.Filled(name) {
2420
return directories.Get(name), nil
@@ -27,7 +23,7 @@ func Dir(name string) ([]string, error) {
2723
var result []string
2824

2925
for _, dir := range []string{
30-
path.Join(setting.CustomPath, "options", name), // custom dir
26+
filepath.Join(setting.CustomPath, "options", name), // custom dir
3127
// no static dir
3228
} {
3329
files, err := statDirIfExist(dir)
@@ -64,24 +60,17 @@ func AssetDir(dirName string) ([]string, error) {
6460
return results, nil
6561
}
6662

67-
// fileFromDir is a helper to read files from bindata or custom path.
68-
func fileFromDir(name string) ([]byte, error) {
69-
customPath := path.Join(setting.CustomPath, "options", name)
70-
71-
isFile, err := util.IsFile(customPath)
72-
if err != nil {
73-
log.Error("Unable to check if %s is a file. Error: %v", customPath, err)
74-
}
75-
if isFile {
76-
return os.ReadFile(customPath)
63+
// fileFromOptionsDir is a helper to read files from custom path or bindata.
64+
func fileFromOptionsDir(elems ...string) ([]byte, error) {
65+
if data, err := readFileFromLocal([]string{setting.CustomPath}, "options", elems...); err == nil {
66+
return data, nil
7767
}
7868

7969
f, err := Assets.Open(name)
8070
if err != nil {
8171
return nil, err
8272
}
8373
defer f.Close()
84-
8574
return io.ReadAll(f)
8675
}
8776

0 commit comments

Comments
 (0)