mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-04 00:03:59 +00:00
fix: guard cmd_run against self-kill (homed pid/service)
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
@ -133,6 +134,10 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
|||||||
return map[string]interface{}{"error": "command is required"}, nil
|
return map[string]interface{}{"error": "command is required"}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if blocked, reason := isDangerousCommand(command); blocked {
|
||||||
|
return map[string]interface{}{"error": reason, "status": "denied"}, nil
|
||||||
|
}
|
||||||
|
|
||||||
timeoutStr, _ := args["timeout"].(string)
|
timeoutStr, _ := args["timeout"].(string)
|
||||||
if timeoutStr == "" {
|
if timeoutStr == "" {
|
||||||
timeoutStr = p.defaultTimeout
|
timeoutStr = p.defaultTimeout
|
||||||
@ -239,3 +244,39 @@ func (p *Plugin) truncateOutput(s string) string {
|
|||||||
}
|
}
|
||||||
return strings.TrimRight(s, "\n")
|
return strings.TrimRight(s, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isDangerousCommand(command string) (bool, string) {
|
||||||
|
pid := os.Getpid()
|
||||||
|
pidStr := fmt.Sprintf("%d", pid)
|
||||||
|
lower := strings.ToLower(command)
|
||||||
|
|
||||||
|
dangerousPatterns := []string{
|
||||||
|
"homed",
|
||||||
|
pidStr,
|
||||||
|
"systemctl stop homeagent",
|
||||||
|
"systemctl restart homeagent",
|
||||||
|
"systemctl kill homeagent",
|
||||||
|
"service homeagent stop",
|
||||||
|
"service homeagent restart",
|
||||||
|
}
|
||||||
|
|
||||||
|
killPatterns := []string{"kill ", "killall ", "pkill ", "kill -", "kill -9"}
|
||||||
|
|
||||||
|
for _, kp := range killPatterns {
|
||||||
|
if strings.Contains(lower, kp) {
|
||||||
|
for _, target := range dangerousPatterns {
|
||||||
|
if strings.Contains(lower, strings.ToLower(target)) {
|
||||||
|
return true, fmt.Sprintf("命令被拦截:禁止向 homed 进程(pid=%s)发送信号", pidStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, dp := range dangerousPatterns[2:] {
|
||||||
|
if strings.Contains(lower, dp) {
|
||||||
|
return true, fmt.Sprintf("命令被拦截:禁止操作 homed 服务进程(pid=%s)", pidStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user