From 8a21e9a20a315234da14678f2bd6a1a2d1ee331c Mon Sep 17 00:00:00 2001 From: samlm0 <9084029+samlm0@users.noreply.github.com> Date: Wed, 6 Mar 2024 12:43:41 +0800 Subject: [PATCH] fix #65 --- backend/fakeshell/commands/map.go | 7 ++++++- backend/fakeshell/menu.go | 25 ++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/backend/fakeshell/commands/map.go b/backend/fakeshell/commands/map.go index 2970777..2acf1cc 100644 --- a/backend/fakeshell/commands/map.go +++ b/backend/fakeshell/commands/map.go @@ -7,11 +7,16 @@ import ( "github.com/spf13/cobra" ) -func AddExecureableAsCommand(cmd *cobra.Command, command string) { +func AddExecureableAsCommand(cmd *cobra.Command, command string, argFilter func(args []string) ([]string, error)) { cmdDefine := &cobra.Command{ Use: command, Run: func(cmd *cobra.Command, args []string) { + args, err := argFilter(args) + if err != nil { + cmd.Println(err) + return + } c := exec.Command(command, args...) c.Env = os.Environ() c.Env = append(c.Env, "TERM=xterm-256color") diff --git a/backend/fakeshell/menu.go b/backend/fakeshell/menu.go index d381dcf..661cf6b 100644 --- a/backend/fakeshell/menu.go +++ b/backend/fakeshell/menu.go @@ -1,8 +1,10 @@ package fakeshell import ( + "errors" "fmt" "os/exec" + "regexp" "github.com/reeflective/console" "github.com/samlm0/als/v2/config" @@ -27,7 +29,24 @@ func defineMenuCommands(a *console.Console) console.Commands { "mtr": config.Config.FeatureMTR, } + argsFilter := map[string]func([]string) ([]string, error){ + "ping": func(args []string) ([]string, error) { + var re = regexp.MustCompile(`(?m)^-?f$|^-\S+f\S*$`) + for _, str := range args { + if len(re.FindAllString(str, -1)) != 0 { + return []string{}, errors.New("dangerous flag detected, stop running") + } + } + return args, nil + }, + } + hasNotFound := false + + argsPassthough := func(args []string) ([]string, error) { + return args, nil + } + for command, feature := range features { if feature { _, err := exec.LookPath(command) @@ -38,7 +57,11 @@ func defineMenuCommands(a *console.Console) console.Commands { hasNotFound = true continue } - commands.AddExecureableAsCommand(rootCmd, command) + filter, ok := argsFilter[command] + if !ok { + filter = argsPassthough + } + commands.AddExecureableAsCommand(rootCmd, command, filter) } }