@@ -123,7 +123,7 @@ func (m *FS) CopyFilesUnder(dir string) error {
123
123
124
124
// Stat returns a FileInfo describing the file.
125
125
func (m * FS ) Stat (name string ) (fs.FileInfo , error ) {
126
- if strings . HasPrefix (name , "../" ) && m . underlyingRoot != "" {
126
+ if m . isPathAboveRoot (name ) {
127
127
return os .Stat (filepath .Join (m .underlyingRoot , name ))
128
128
}
129
129
@@ -145,15 +145,15 @@ func (m *FS) Stat(name string) (fs.FileInfo, error) {
145
145
// ReadDir reads the named directory
146
146
// and returns a list of directory entries sorted by filename.
147
147
func (m * FS ) ReadDir (name string ) ([]fs.DirEntry , error ) {
148
- if strings . HasPrefix (name , "../" ) && m . underlyingRoot != "" {
148
+ if m . isPathAboveRoot (name ) {
149
149
return os .ReadDir (filepath .Join (m .underlyingRoot , name ))
150
150
}
151
151
return m .root .ReadDir (cleanPath (name ))
152
152
}
153
153
154
154
// Open opens the named file for reading.
155
155
func (m * FS ) Open (name string ) (fs.File , error ) {
156
- if strings . HasPrefix (name , "../" ) && m . underlyingRoot != "" {
156
+ if m . isPathAboveRoot (name ) {
157
157
return os .Open (filepath .Join (m .underlyingRoot , name ))
158
158
}
159
159
return m .root .Open (cleanPath (name ))
@@ -188,7 +188,7 @@ func (m *FS) MkdirAll(path string, perm fs.FileMode) error {
188
188
// The caller is permitted to modify the returned byte slice.
189
189
// This method should return a copy of the underlying data.
190
190
func (m * FS ) ReadFile (name string ) ([]byte , error ) {
191
- if strings . HasPrefix (name , "../" ) && m . underlyingRoot != "" {
191
+ if m . isPathAboveRoot (name ) {
192
192
return os .ReadFile (filepath .Join (m .underlyingRoot , name ))
193
193
}
194
194
@@ -245,3 +245,7 @@ func cleanPath(path string) string {
245
245
path = strings .TrimLeft (path , "/" ) // Remove the leading slash
246
246
return path
247
247
}
248
+
249
+ func (m * FS ) isPathAboveRoot (name string ) bool {
250
+ return (name == ".." || strings .HasPrefix (name , "../" )) && m .underlyingRoot != ""
251
+ }
0 commit comments