forked from projectdiscovery/simplehttpserver
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
begin of work on pseudo-sandbox mode
- Loading branch information
Showing
6 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package httpserver | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"path/filepath" | ||
) | ||
|
||
type SandboxFileSystem struct { | ||
fs http.FileSystem | ||
RootFolder string | ||
} | ||
|
||
func (sbfs SandboxFileSystem) Open(path string) (http.File, error) { | ||
abspath, err := filepath.Abs(filepath.Join(sbfs.RootFolder, path)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
filename := filepath.Base(abspath) | ||
// rejects names starting with a dot like .file | ||
dotmatch, err := filepath.Match(".*", filename) | ||
if err != nil { | ||
return nil, err | ||
} else if dotmatch { | ||
return nil, errors.New("invalid file") | ||
} | ||
|
||
// reject symlinks | ||
symlinkCheck, err := filepath.EvalSymlinks(abspath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if symlinkCheck != abspath { | ||
return nil, errors.New("symlinks not allowed") | ||
} | ||
|
||
// check if the path is within the configured folder | ||
if sbfs.RootFolder != abspath { | ||
pattern := sbfs.RootFolder + string(filepath.Separator) + "*" | ||
matched, err := filepath.Match(pattern, abspath) | ||
if err != nil { | ||
return nil, err | ||
} else if !matched { | ||
return nil, errors.New("invalid file") | ||
} | ||
} | ||
|
||
f, err := sbfs.fs.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return f, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package httpserver | ||
|
||
func toMb(n int) int64 { | ||
return int64(n) * 1024 * 1024 | ||
} |