29
"golang.org/x/exp/slices"
30
"mvdan.cc/sh/v3/interp"
33
func RestrictedReadDir(allowedPrefixes ...string) interp.ReadDirHandlerFunc {
34
return func(ctx context.Context, s string) ([]fs.FileInfo, error) {
35
path := filepath.Clean(s)
36
for _, allowedPrefix := range allowedPrefixes {
37
if strings.HasPrefix(path, allowedPrefix) {
38
return interp.DefaultReadDirHandler()(ctx, s)
42
return nil, fs.ErrNotExist
46
func RestrictedStat(allowedPrefixes ...string) interp.StatHandlerFunc {
47
return func(ctx context.Context, s string, b bool) (fs.FileInfo, error) {
48
path := filepath.Clean(s)
49
for _, allowedPrefix := range allowedPrefixes {
50
if strings.HasPrefix(path, allowedPrefix) {
51
return interp.DefaultStatHandler()(ctx, s, b)
55
return nil, fs.ErrNotExist
59
func RestrictedOpen(allowedPrefixes ...string) interp.OpenHandlerFunc {
60
return func(ctx context.Context, s string, i int, fm fs.FileMode) (io.ReadWriteCloser, error) {
61
path := filepath.Clean(s)
62
for _, allowedPrefix := range allowedPrefixes {
63
if strings.HasPrefix(path, allowedPrefix) {
64
return interp.DefaultOpenHandler()(ctx, s, i, fm)
72
func RestrictedExec(allowedCmds ...string) interp.ExecHandlerFunc {
73
return func(ctx context.Context, args []string) error {
74
if slices.Contains(allowedCmds, args[0]) {
75
return interp.DefaultExecHandler(2*time.Second)(ctx, args)