Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
companion: validate runtime executable and cancel installer trees
Barack Sokullu997c61d7393e
diff --git a/webbrain-systray/internal/localmodel/manager.go b/webbrain-systray/internal/localmodel/manager.goindex 4bf7d860d..90bc7cdb8 100644--- a/webbrain-systray/internal/localmodel/manager.go+++ b/webbrain-systray/internal/localmodel/manager.go@@ -216,6 +216,9 @@ func installedRuntimeManifest(p Plan) ([]byte, error) {if !info.Mode().IsRegular() {return fmt.Errorf("runtime entry is not a file: %s", path)}+ if runtime.GOOS != "windows" && filepath.Base(path) == "llama-server" && info.Mode().Perm()&0o111 == 0 {+ return fmt.Errorf("llama-server is not executable: %s", path)+ }sha, err := fileSHA256(path)if err != nil {return err@@ -259,10 +262,17 @@ func runtimeInstalled(p Plan) bool {if !filepath.IsLocal(filepath.FromSlash(f.Path)) || f.Size < 0 || len(f.SHA256) != 64 {return false}- if !runtimeVerifier.Valid(filepath.Join(dir, filepath.FromSlash(f.Path)), f.Size, f.SHA256) {+ path := filepath.Join(dir, filepath.FromSlash(f.Path))+ if !runtimeVerifier.Valid(path, f.Size, f.SHA256) {return false}if filepath.Base(f.Path) == binary {+ if runtime.GOOS != "windows" {+ info, err := os.Stat(path)+ if err != nil || info.Mode().Perm()&0o111 == 0 {+ return false+ }+ }binaryVerified = true}}diff --git a/webbrain-systray/internal/localmodel/manager_test.go b/webbrain-systray/internal/localmodel/manager_test.goindex 64bb5b152..33a34c824 100644--- a/webbrain-systray/internal/localmodel/manager_test.go+++ b/webbrain-systray/internal/localmodel/manager_test.go@@ -374,6 +374,23 @@ func TestRuntimeInstalledRejectsMissingAndCorruptExtractedFiles(t *testing.T) {if !runtimeInstalled(plan) {t.Fatal("valid extracted runtime was rejected")}+ if runtime.GOOS != "windows" {+ if err := os.Chmod(serverPath, 0o600); err != nil {+ t.Fatal(err)+ }+ if runtimeInstalled(plan) {+ t.Fatal("non-executable llama-server was accepted")+ }+ if _, err := installedRuntimeManifest(plan); err == nil {+ t.Fatal("non-executable llama-server was marked as installed")+ }+ if err := os.Chmod(serverPath, 0o700); err != nil {+ t.Fatal(err)+ }+ if !runtimeInstalled(plan) {+ t.Fatal("repaired llama-server was rejected")+ }+ }if err := os.Remove(libraryPath); err != nil {t.Fatal(err)}diff --git a/webbrain-systray/internal/osutil/exec.go b/webbrain-systray/internal/osutil/exec.goindex c95597d16..4bc141306 100644--- a/webbrain-systray/internal/osutil/exec.go+++ b/webbrain-systray/internal/osutil/exec.go@@ -3,6 +3,7 @@ package osutilimport ("context""os/exec"+ "time")// Command is exec.Command with the platform tweaks every child process of a@@ -17,5 +18,7 @@ func Command(name string, args ...string) *exec.Cmd {func CommandContext(ctx context.Context, name string, args ...string) *exec.Cmd {cmd := exec.CommandContext(ctx, name, args...)hideWindow(cmd)+ cmd.Cancel = func() error { return cancelProcessTree(cmd) }+ cmd.WaitDelay = 5 * time.Secondreturn cmd}diff --git a/webbrain-systray/internal/osutil/exec_other.go b/webbrain-systray/internal/osutil/exec_other.goindex 0e0ecf18f..f4026919a 100644--- a/webbrain-systray/internal/osutil/exec_other.go+++ b/webbrain-systray/internal/osutil/exec_other.go@@ -3,6 +3,8 @@package osutilimport (+ "errors"+ "os""os/exec""syscall")@@ -15,3 +17,14 @@ func hideWindow(cmd *exec.Cmd) {}cmd.SysProcAttr.Setpgid = true}++func cancelProcessTree(cmd *exec.Cmd) error {+ if cmd.Process == nil {+ return os.ErrProcessDone+ }+ err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)+ if errors.Is(err, syscall.ESRCH) {+ return os.ErrProcessDone+ }+ return err+}diff --git a/webbrain-systray/internal/osutil/exec_test.go b/webbrain-systray/internal/osutil/exec_test.gonew file mode 100644index 000000000..bd3fd3388--- /dev/null+++ b/webbrain-systray/internal/osutil/exec_test.go@@ -0,0 +1,90 @@+package osutil++import (+ "context"+ "os"+ "os/exec"+ "path/filepath"+ "strconv"+ "strings"+ "testing"+ "time"+)++func TestProcessTreeHelper(t *testing.T) {+ if os.Getenv("WBC_TREE_CHILD") == "1" {+ for i := 0; i < 600; i++ {+ f, err := os.OpenFile(os.Getenv("WBC_TREE_HEARTBEAT"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)+ if err != nil {+ os.Exit(2)+ }+ _, _ = f.WriteString("x")+ _ = f.Close()+ time.Sleep(50 * time.Millisecond)+ }+ return+ }+ if os.Getenv("WBC_TREE_PARENT") == "1" {+ child := exec.Command(os.Args[0], "-test.run=^TestProcessTreeHelper$")+ child.Env = append(os.Environ(), "WBC_TREE_CHILD=1")+ if err := child.Start(); err != nil {+ os.Exit(2)+ }+ _ = os.WriteFile(os.Getenv("WBC_TREE_PID"), []byte(strconv.Itoa(child.Process.Pid)), 0o600)+ _ = child.Wait()+ }+}++func TestCommandContextKillsDescendants(t *testing.T) {+ dir := t.TempDir()+ heartbeat, pidFile := filepath.Join(dir, "heartbeat"), filepath.Join(dir, "child.pid")+ ctx, cancel := context.WithCancel(context.Background())+ defer cancel()+ cmd := CommandContext(ctx, os.Args[0], "-test.run=^TestProcessTreeHelper$")+ cmd.Env = append(os.Environ(), "WBC_TREE_PARENT=1", "WBC_TREE_HEARTBEAT="+heartbeat, "WBC_TREE_PID="+pidFile)+ if err := cmd.Start(); err != nil {+ t.Fatal(err)+ }+ cleanupChild := true+ t.Cleanup(func() {+ _ = cmd.Process.Kill()+ if data, err := os.ReadFile(pidFile); cleanupChild && err == nil {+ if pid, err := strconv.Atoi(strings.TrimSpace(string(data))); err == nil {+ if child, err := os.FindProcess(pid); err == nil {+ _ = child.Kill()+ }+ }+ }+ })+ deadline := time.Now().Add(10 * time.Second)+ for {+ if data, err := os.ReadFile(heartbeat); err == nil && len(data) > 0 {+ break+ }+ if time.Now().After(deadline) {+ t.Fatal("child process never began writing")+ }+ time.Sleep(20 * time.Millisecond)+ }+ cancel()+ finished := make(chan error, 1)+ go func() { finished <- cmd.Wait() }()+ select {+ case <-finished:+ case <-time.After(10 * time.Second):+ t.Fatal("parent process did not stop after cancellation")+ }+ before, err := os.ReadFile(heartbeat)+ if err != nil {+ t.Fatal(err)+ }+ time.Sleep(250 * time.Millisecond)+ after, err := os.ReadFile(heartbeat)+ if err != nil {+ t.Fatal(err)+ }+ if len(after) != len(before) {+ t.Fatalf("child process kept running after cancellation: %d -> %d bytes", len(before), len(after))+ }+ cleanupChild = false+}diff --git a/webbrain-systray/internal/osutil/exec_windows.go b/webbrain-systray/internal/osutil/exec_windows.goindex 4dd4bc067..90b01059d 100644--- a/webbrain-systray/internal/osutil/exec_windows.go+++ b/webbrain-systray/internal/osutil/exec_windows.go@@ -3,7 +3,11 @@package osutilimport (+ "fmt"+ "os""os/exec"+ "strconv"+ "strings""syscall")@@ -16,3 +20,16 @@ func hideWindow(cmd *exec.Cmd) {cmd.SysProcAttr.HideWindow = truecmd.SysProcAttr.CreationFlags |= createNoWindow}++func cancelProcessTree(cmd *exec.Cmd) error {+ if cmd.Process == nil {+ return os.ErrProcessDone+ }+ killer := exec.Command("taskkill", "/PID", strconv.Itoa(cmd.Process.Pid), "/T", "/F")+ hideWindow(killer)+ if output, err := killer.CombinedOutput(); err != nil {+ _ = cmd.Process.Kill()+ return fmt.Errorf("terminate process tree: %w: %s", err, strings.TrimSpace(string(output)))+ }+ return nil+}