generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logic): add filtered virtual FS
- Loading branch information
Showing
2 changed files
with
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package fs | ||
|
||
import ( | ||
"io/fs" | ||
"net/url" | ||
|
||
"github.com/okp4/okp4d/x/logic/util" | ||
) | ||
|
||
// FilteredFS is a wrapper around a fs.FS that filters out files that are not allowed to be read. | ||
// This is used to prevent the interpreter from reading files, using protocols that are not allowed to be used | ||
// by the interpreter on the blockchain. | ||
// The whitelist and blacklist are mutually exclusive. If both are set, the blacklist will be ignored. | ||
type FilteredFS struct { | ||
decorated fs.FS | ||
whitelist []*url.URL | ||
blacklist []*url.URL | ||
} | ||
|
||
var _ fs.FS = (*FilteredFS)(nil) | ||
|
||
// NewFilteredFS returns a new FilteredFS object that will filter out files that are not allowed to be read | ||
// according to the whitelist and blacklist parameters. | ||
func NewFilteredFS(whitelist, blacklist []*url.URL, decorated fs.FS) *FilteredFS { | ||
return &FilteredFS{ | ||
decorated: decorated, | ||
whitelist: whitelist, | ||
blacklist: blacklist, | ||
} | ||
} | ||
|
||
// Open opens the named file. | ||
// The name parameter is a URL that will be parsed and checked against the whitelist and blacklist configured. | ||
func (f *FilteredFS) Open(name string) (fs.File, error) { | ||
urlFile, err := url.Parse(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !util.WhitelistBlacklistMatches(f.whitelist, f.blacklist, util.UrlMatches)(urlFile) { | ||
return nil, &fs.PathError{ | ||
Op: "open", | ||
Path: name, | ||
Err: fs.ErrPermission, | ||
} | ||
} | ||
|
||
return f.decorated.Open(name) | ||
} |
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