@@ -7,10 +7,12 @@ package storage
7
7
import (
8
8
"context"
9
9
"errors"
10
+ "fmt"
10
11
"io"
11
12
"net/url"
12
13
"os"
13
14
"path/filepath"
15
+ "strings"
14
16
15
17
"code.gitea.io/gitea/modules/log"
16
18
"code.gitea.io/gitea/modules/util"
@@ -62,15 +64,15 @@ func NewLocalStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error
62
64
63
65
// Open a file
64
66
func (l * LocalStorage ) Open (path string ) (Object , error ) {
65
- if ! l . isValid (path ) {
67
+ if ! isLocalPathValid (path ) {
66
68
return nil , ErrLocalPathNotSupported
67
69
}
68
70
return os .Open (filepath .Join (l .dir , path ))
69
71
}
70
72
71
73
// Save a file
72
74
func (l * LocalStorage ) Save (path string , r io.Reader , size int64 ) (int64 , error ) {
73
- if ! l . isValid (path ) {
75
+ if ! isLocalPathValid (path ) {
74
76
return 0 , ErrLocalPathNotSupported
75
77
}
76
78
@@ -117,17 +119,17 @@ func (l *LocalStorage) Stat(path string) (os.FileInfo, error) {
117
119
return os .Stat (filepath .Join (l .dir , path ))
118
120
}
119
121
120
- func ( l * LocalStorage ) isValid (path string ) bool {
121
- a , err := filepath .Abs (path )
122
- if err != nil {
122
+ func isLocalPathValid (path string ) bool {
123
+ a := filepath .Clean (path )
124
+ if strings . HasPrefix ( a , fmt . Sprintf ( "..%c" , filepath . Separator )) {
123
125
return false
124
126
}
125
- return a == "/" + path
127
+ return a == path
126
128
}
127
129
128
130
// Delete delete a file
129
131
func (l * LocalStorage ) Delete (path string ) error {
130
- if ! l . isValid (path ) {
132
+ if ! isLocalPathValid (path ) {
131
133
return ErrLocalPathNotSupported
132
134
}
133
135
p := filepath .Join (l .dir , path )
0 commit comments