fix: harden command execution helpers (#12834)

This commit is contained in:
王贺
2026-05-25 11:38:31 +08:00
committed by GitHub
parent 0fe9033d23
commit 4fa14f055d
5 changed files with 28 additions and 15 deletions
+11 -7
View File
@@ -753,13 +753,17 @@ func loadOpenclawSkillSearchOutput(containerName, source, keyword string) (strin
case "skillhub":
return cmd.RunDockerExecWithStdout(2*time.Minute, containerName, "skillhub", "search", keyword, "--json")
default:
return cmd.RunDockerExecWithStdout(
2*time.Minute,
containerName,
"sh",
"-c",
fmt.Sprintf("CLAWHUB_REGISTRY=%q clawhub search %q", resolveClawhubRegistry(source), keyword),
)
return cmd.NewCommandMgr(cmd.WithTimeout(2*time.Minute)).
RunWithStdout(
"docker",
"exec",
"-e",
"CLAWHUB_REGISTRY="+resolveClawhubRegistry(source),
containerName,
"clawhub",
"search",
keyword,
)
}
}
+3 -3
View File
@@ -198,10 +198,10 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error {
}
func (u *ImageRepoService) CheckConn(host, user, password string) error {
cmdMgr := cmd.NewCommandMgr()
stdout, err := cmdMgr.RunWithStdout("docker", "login", "-u", user, "-p", password, host)
cmdMgr := cmd.NewCommandMgr(cmd.WithStdin(strings.NewReader(password)))
stdout, err := cmdMgr.RunWithStdout("docker", "login", "-u", user, "--password-stdin", host)
if err != nil {
return fmt.Errorf("stdout: %s, stderr: %v", stdout, err)
return fmt.Errorf("docker login failed: %v", err)
}
if strings.Contains(string(stdout), "Login Succeeded") {
return nil
+9
View File
@@ -26,6 +26,7 @@ type CommandHelper struct {
workDir string
outputFile string
scriptPath string
stdin io.Reader
env []string
timeout time.Duration
taskItem *task.Task
@@ -331,6 +332,9 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
if len(c.workDir) != 0 {
cmd.Dir = c.workDir
}
if c.stdin != nil {
cmd.Stdin = c.stdin
}
defer func() {
for _, closer := range loggerClosers {
_ = closer.Close()
@@ -435,6 +439,11 @@ func WithScriptPath(scriptPath string) Option {
s.scriptPath = scriptPath
}
}
func WithStdin(stdin io.Reader) Option {
return func(s *CommandHelper) {
s.stdin = stdin
}
}
func WithEnv(env ...string) Option {
return func(s *CommandHelper) {
s.env = append(s.env, env...)
+3 -3
View File
@@ -24,11 +24,11 @@ func (t TarGzArchiver) Extract(ctx context.Context, filePath, dstDir string, sec
if err := os.MkdirAll(dstDir, 0755); err != nil {
return fmt.Errorf("failed to create destination dir: %w", err)
}
cmdMgr := cmd.NewCommandMgr(cmd.WithContext(ctx), cmd.WithIgnoreExist1())
if len(secret) != 0 {
return runTarGzDecryptToDir(cmd.NewCommandMgr(cmd.WithIgnoreExist1()), filePath, dstDir, secret, false)
} else {
return runTarGzExtractToDir(cmd.NewCommandMgr(cmd.WithIgnoreExist1()), filePath, dstDir)
return runTarGzDecryptToDir(cmdMgr, filePath, dstDir, secret, false)
}
return runTarGzExtractToDir(cmdMgr, filePath, dstDir)
}
func (t TarGzArchiver) Compress(ctx context.Context, sourcePaths []string, dstFile string, secret string) error {
+2 -2
View File
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"path"
"strings"
"time"
"github.com/1Panel-dev/1Panel/agent/constant"
@@ -43,7 +42,8 @@ func (z ZipArchiver) Compress(ctx context.Context, sourcePaths []string, dstFile
relativePaths[i] = path.Base(sp)
}
cmdMgr := cmd.NewCommandMgr(cmd.WithWorkDir(baseDir), cmd.WithContext(ctx))
if err = cmdMgr.Run("zip", "-qr", tmpFile, strings.Join(relativePaths, " ")); err != nil {
args := append([]string{"-qr", tmpFile}, relativePaths...)
if err = cmdMgr.Run("zip", args...); err != nil {
return err
}
if err = op.Mv(tmpFile, dstFile); err != nil {