@@ -6,6 +6,7 @@ package storage
6
6
7
7
import (
8
8
"context"
9
+ "errors"
9
10
"io"
10
11
"net/url"
11
12
"os"
@@ -15,6 +16,8 @@ import (
15
16
"code.gitea.io/gitea/modules/util"
16
17
)
17
18
19
+ // ErrLocalPathNotSupported represents an error that path is not supported
20
+ var ErrLocalPathNotSupported = errors .New ("local path is not supported" )
18
21
var _ ObjectStorage = & LocalStorage {}
19
22
20
23
// LocalStorageType is the type descriptor for local storage
@@ -59,11 +62,18 @@ func NewLocalStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error
59
62
60
63
// Open a file
61
64
func (l * LocalStorage ) Open (path string ) (Object , error ) {
65
+ if ! l .isValid (path ) {
66
+ return nil , ErrLocalPathNotSupported
67
+ }
62
68
return os .Open (filepath .Join (l .dir , path ))
63
69
}
64
70
65
71
// Save a file
66
72
func (l * LocalStorage ) Save (path string , r io.Reader , size int64 ) (int64 , error ) {
73
+ if ! l .isValid (path ) {
74
+ return 0 , ErrLocalPathNotSupported
75
+ }
76
+
67
77
p := filepath .Join (l .dir , path )
68
78
if err := os .MkdirAll (filepath .Dir (p ), os .ModePerm ); err != nil {
69
79
return 0 , err
@@ -107,8 +117,19 @@ func (l *LocalStorage) Stat(path string) (os.FileInfo, error) {
107
117
return os .Stat (filepath .Join (l .dir , path ))
108
118
}
109
119
120
+ func (l * LocalStorage ) isValid (path string ) bool {
121
+ a , err := filepath .Abs (path )
122
+ if err != nil {
123
+ return false
124
+ }
125
+ return a == "/" + path
126
+ }
127
+
110
128
// Delete delete a file
111
129
func (l * LocalStorage ) Delete (path string ) error {
130
+ if ! l .isValid (path ) {
131
+ return ErrLocalPathNotSupported
132
+ }
112
133
p := filepath .Join (l .dir , path )
113
134
return util .Remove (p )
114
135
}
0 commit comments