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: verify runtimes and serialize installs
Barack Sokullu5a85491f5a31
diff --git a/webbrain-systray/internal/app/guards_test.go b/webbrain-systray/internal/app/guards_test.goindex 8b8ecc4c4..9ca60b52f 100644--- a/webbrain-systray/internal/app/guards_test.go+++ b/webbrain-systray/internal/app/guards_test.go@@ -121,3 +121,44 @@ func TestAccessMiddleware(t *testing.T) {})}}++func TestDecisionEndpointAcceptsProxyKeyForms(t *testing.T) {+ gin.SetMode(gin.TestMode)+ t.Setenv("WBC_HOME", t.TempDir())+ store, err := settings.Open()+ if err != nil {+ t.Fatal(err)+ }+ _, err = store.Update(func(s *settings.Settings) { s.Network = true; s.APIKey = "wb-user" })+ if err != nil {+ t.Fatal(err)+ }+ a := &App{Store: store}+ e := gin.New()+ e.POST("/companion/v1/decide", a.guardClient, func(c *gin.Context) { c.Status(http.StatusNoContent) })+ cases := []struct {+ name, url, header, value string+ want int+ }{+ {"bearer", "/companion/v1/decide", "Authorization", "Bearer wb-user", http.StatusNoContent},+ {"x-api-key", "/companion/v1/decide", "X-Api-Key", "wb-user", http.StatusNoContent},+ {"x-goog-api-key", "/companion/v1/decide", "X-Goog-Api-Key", "wb-user", http.StatusNoContent},+ {"query-key", "/companion/v1/decide?key=wb-user", "", "", http.StatusNoContent},+ {"query-auth-token", "/companion/v1/decide?auth_token=wb-user", "", "", http.StatusNoContent},+ {"wrong-key", "/companion/v1/decide?key=bad", "", "", http.StatusUnauthorized},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ r := httptest.NewRequest(http.MethodPost, "http://192.168.1.57:8327"+tc.url, nil)+ r.RemoteAddr = "192.168.1.20:50000"+ if tc.header != "" {+ r.Header.Set(tc.header, tc.value)+ }+ w := httptest.NewRecorder()+ e.ServeHTTP(w, r)+ if w.Code != tc.want {+ t.Fatalf("status %d, want %d (%s)", w.Code, tc.want, w.Body.String())+ }+ })+ }+}diff --git a/webbrain-systray/internal/app/routes.go b/webbrain-systray/internal/app/routes.goindex ffc724aa7..639aca481 100644--- a/webbrain-systray/internal/app/routes.go+++ b/webbrain-systray/internal/app/routes.go@@ -119,10 +119,7 @@ func (a *App) guardClient(c *gin.Context) {deny(c, msgNetwork)return}- key := strings.TrimSpace(strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer "))- if key == "" {- key = r.Header.Get("X-Api-Key")- }+ key := clientAPIKey(r)want := a.Store.Get().APIKeyif key == "" || subtle.ConstantTimeCompare([]byte(key), []byte(want)) != 1 {c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid API key"})@@ -131,6 +128,22 @@ func (a *App) guardClient(c *gin.Context) {c.Next()}+// Match the key forms accepted by the proxy's model endpoints.+func clientAPIKey(r *http.Request) string {+ if auth := strings.TrimSpace(r.Header.Get("Authorization")); auth != "" {+ if scheme, value, ok := strings.Cut(auth, " "); ok && strings.EqualFold(scheme, "Bearer") {+ return strings.TrimSpace(value)+ }+ return auth+ }+ for _, key := range []string{r.Header.Get("X-Api-Key"), r.Header.Get("X-Goog-Api-Key"), r.URL.Query().Get("key"), r.URL.Query().Get("auth_token")} {+ if key != "" {+ return key+ }+ }+ return ""+}+func fail(c *gin.Context, status int, err error) {c.JSON(status, gin.H{"error": err.Error()})}diff --git a/webbrain-systray/internal/d1/manager.go b/webbrain-systray/internal/d1/manager.goindex 335d45643..0f1cfcfdc 100644--- a/webbrain-systray/internal/d1/manager.go+++ b/webbrain-systray/internal/d1/manager.go@@ -276,7 +276,9 @@ func (m *Manager) Install() error {}ctx, cancel := context.WithCancel(context.Background())m.cancel = cancel+ m.phase, m.step, m.err, m.progress = PhaseDownloading, "Preparing the decision model", "", nilm.mu.Unlock()+ m.changed()go func() {defer cancel()if err := m.install(ctx, asset); err != nil {@@ -288,6 +290,10 @@ func (m *Manager) Install() error {m.set(PhaseError, "", err)return}+ if err := ctx.Err(); err != nil {+ m.set(PhaseIdle, "", nil)+ return+ }_, _ = m.store.Update(func(s *settings.Settings) { s.Decision.Enabled = true })if err := m.Load(); err != nil {logx.Printf("decision model load: %v", err)diff --git a/webbrain-systray/internal/d1/manager_files_test.go b/webbrain-systray/internal/d1/manager_files_test.goindex 94c2d6bd4..b3395ea08 100644--- a/webbrain-systray/internal/d1/manager_files_test.go+++ b/webbrain-systray/internal/d1/manager_files_test.go@@ -7,8 +7,29 @@ import ("path/filepath""testing""time"++ "github.com/esokullu/webbrain-systray/internal/settings")+func TestInstallClaimsPhaseBeforeWorkerStarts(t *testing.T) {+ if _, ok := ortForPlatform(); !ok {+ t.Skip("no runtime archive for this platform")+ }+ t.Setenv("WBC_HOME", t.TempDir())+ store, err := settings.Open()+ if err != nil {+ t.Fatal(err)+ }+ m := NewManager(store, nil)+ if err := m.Install(); err != nil {+ t.Fatal(err)+ }+ defer m.Cancel()+ if err := m.Install(); err == nil {+ t.Fatal("overlapping install was accepted")+ }+}+func TestValidFileRejectsSameSizeCorruption(t *testing.T) {path := filepath.Join(t.TempDir(), "model.data")good := []byte("pinned model contents")diff --git a/webbrain-systray/internal/localmodel/catalog.go b/webbrain-systray/internal/localmodel/catalog.goindex 458f5976f..fe0c655c9 100644--- a/webbrain-systray/internal/localmodel/catalog.go+++ b/webbrain-systray/internal/localmodel/catalog.go@@ -105,6 +105,31 @@ var llamaAssetSizes = map[string]int64{"llama-" + llamaTag + "-bin-win-cpu-arm64.zip": 12412126,}+// SHA-256 digests published with the pinned PrismML release assets.+var llamaAssetSHA = map[string]string{+ "llama-" + llamaTag + "-bin-macos-arm64.tar.gz": "59230b0d3e90715095a3f1b9a42bb6723308e7cf3d2d3ef562f49213e95fcfc1",+ "llama-" + llamaTag + "-bin-macos-x64.tar.gz": "f7ade10b7367b9bc6c22f4890cf2b26702a40a1f4cd36dcdeca50663e4f0eff4",+ "llama-" + llamaTag + "-bin-linux-cuda-12.4-x64.tar.gz": "a7003184559fc946a0109734a9de8a1d53d5a6b33e308bec381a293b912f7093",+ "llama-" + llamaTag + "-bin-linux-cuda-12.8-x64.tar.gz": "f61ac818da2c8cddc47d75c955b6f5bf821738138760842dd50413c5181cdfe1",+ "llama-" + llamaTag + "-bin-linux-cuda-13.3-x64.tar.gz": "e2d492f50d70f3e01b4b3ae68f670ae6825870d97206fb1bcb5b4376640c0e0c",+ "llama-" + llamaTag + "-bin-ubuntu-vulkan-x64.tar.gz": "4401c665506927f5c2ad44c4e2e139715de5bd5e92b693a0a1f515b9442af9bb",+ "llama-" + llamaTag + "-bin-ubuntu-vulkan-arm64.tar.gz": "e7870fa961cb4344e601ad385dfd8fb141a827536963d64e4ee01e0e80dc99bc",+ "llama-" + llamaTag + "-bin-ubuntu-x64.tar.gz": "bff69881bdb2b09dcbf5e99a1cb532b2a6821c6dc42d33ac53b2fa2cd3e6f214",+ "llama-" + llamaTag + "-bin-ubuntu-arm64.tar.gz": "50171efa3706c4765e1948ccab840f7add207d9044c0978f9248c54054a01207",+ "llama-" + llamaTag + "-bin-win-cuda-12.4-x64.zip": "64ca6810aae0e6bbb28d22f888bd009b9bdd0c24c5af8a6a7490b3eaf75ccd57",+ "llama-" + llamaTag + "-bin-win-cuda-13.3-x64.zip": "20c45aecad3f4ede81c64ababc03f1dc10103fbd4cfb09244a16d21a3dd1db65",+ "cudart-llama-bin-win-cuda-12.4-x64.zip": "8c79a9b226de4b3cacfd1f83d24f962d0773be79f1e7b75c6af4ded7e32ae1d6",+ "cudart-llama-bin-win-cuda-13.3-x64.zip": "1462a050eb4c684921ba51dcc4cc488a036674c3e73e9945ee705b854808d03e",+ "llama-" + llamaTag + "-bin-win-vulkan-x64.zip": "16d9482224e53d462eb210a766c8d94f0461cdca4d1c7263b2ba7b97092bf739",+ "llama-" + llamaTag + "-bin-win-cpu-x64.zip": "ab1ea7cfc23fdac244088fd6f1762d0efa24d3d79fe18d4f16c496cc00775728",+ "llama-" + llamaTag + "-bin-win-cpu-arm64.zip": "cc808b33d35c72a3bd5c69a0d609d422426feb9dce23868e77992b6c33b8ddb3",+}++var uvAssets = map[string]File{+ "uv-aarch64-apple-darwin.tar.gz": {Size: 14309841, SHA256: "bf1e4fb6f5f5079a2902ad3f3a9e2d337575b226a1378f2449d8404353650608"},+ "uv-x86_64-apple-darwin.tar.gz": {Size: 17182824, SHA256: "5c35634957db7ebd9c2e4cc6c66b8979fb2c26a6b0dbd1d214d6acbac4033a89"},+}+func uvAsset(arch string) string {if arch == "arm64" {return "uv-aarch64-apple-darwin.tar.gz"diff --git a/webbrain-systray/internal/localmodel/manager.go b/webbrain-systray/internal/localmodel/manager.goindex 6f94257b3..df18e3898 100644--- a/webbrain-systray/internal/localmodel/manager.go+++ b/webbrain-systray/internal/localmodel/manager.go@@ -133,10 +133,41 @@ func fileOK(path string, size int64) bool {return err == nil && !st.IsDir() && (size <= 0 || st.Size() == size)}+func runtimeManifest(p Plan) (string, bool) {+ var hashes []string+ for _, asset := range p.Assets {+ sha := llamaAssetSHA[asset]+ if sha == "" {+ return "", false+ }+ hashes = append(hashes, asset+" "+sha)+ }+ return llamaTag + "\n" + strings.Join(hashes, "\n") + "\n", len(hashes) > 0+}++func runtimeInstalled(p Plan) bool {+ want, ok := runtimeManifest(p)+ if !ok {+ return false+ }+ data, err := os.ReadFile(filepath.Join(runtimeDir(p), ".installed"))+ return err == nil && string(data) == want+}++func uvInstalled(arch string) bool {+ asset := uvAsset(arch)+ pinned, ok := uvAssets[asset]+ if !ok || !fileOK(filepath.Join(uvDir(), "uv"), 0) {+ return false+ }+ data, err := os.ReadFile(filepath.Join(uvDir(), ".installed"))+ return err == nil && string(data) == asset+" "+pinned.SHA256+"\n"+}+func (m *Manager) ggufInstalled(p Plan) bool {return m.verified.Valid(filepath.Join(ggufDir(), gguf.Model.Name), gguf.Model.Size, gguf.Model.SHA256) &&m.verified.Valid(filepath.Join(ggufDir(), gguf.Proj.Name), gguf.Proj.Size, gguf.Proj.SHA256) &&- fileOK(filepath.Join(runtimeDir(p), ".installed"), 0)+ runtimeInstalled(p)}func (m *Manager) mlxInstalled() bool {@@ -192,7 +223,7 @@ func (m *Manager) Status() Status {if format == "gguf" {st.DownloadBytes = m.remaining(filepath.Join(ggufDir(), gguf.Model.Name), gguf.Model.Size, gguf.Model.SHA256) +m.remaining(filepath.Join(ggufDir(), gguf.Proj.Name), gguf.Proj.Size, gguf.Proj.SHA256)- if !fileOK(filepath.Join(runtimeDir(plan), ".installed"), 0) {+ if !runtimeInstalled(plan) {for _, a := range plan.Assets {st.DownloadBytes += llamaAssetSizes[a]}@@ -264,7 +295,9 @@ func (m *Manager) Install(format string) error {}ctx, cancel := context.WithCancel(context.Background())m.cancel = cancel+ m.phase, m.step, m.err, m.progress = PhaseDownloading, "Preparing the local model", "", nilm.mu.Unlock()+ m.changed()_, _ = m.store.Update(func(s *settings.Settings) { s.Local.Format = format })go func() {@@ -284,6 +317,10 @@ func (m *Manager) Install(format string) error {m.setPhase(PhaseError, "", err)return}+ if err := ctx.Err(); err != nil {+ m.setPhase(PhaseIdle, "", nil)+ return+ }_, _ = m.store.Update(func(s *settings.Settings) { s.Local.AutoStart = true })if err := m.Start(); err != nil {m.setPhase(PhaseError, "", err)@@ -344,10 +381,14 @@ func (m *Manager) installGGUF(ctx context.Context, info sysinfo.Info) error {{"Bonsai 2 27B weights (PTQ1_0)", hfURL(gguf.Repo, gguf.Rev, gguf.Model.Name), filepath.Join(ggufDir(), gguf.Model.Name), gguf.Model.Size, gguf.Model.SHA256},{"Vision projector (mmproj Q8_0)", hfURL(gguf.Repo, gguf.Rev, gguf.Proj.Name), filepath.Join(ggufDir(), gguf.Proj.Name), gguf.Proj.Size, gguf.Proj.SHA256},}- runtimeReady := fileOK(filepath.Join(rdir, ".installed"), 0)+ runtimeReady := runtimeInstalled(plan)if !runtimeReady {for _, a := range plan.Assets {- items = append(items, item{"llama.cpp runtime (" + strings.Join(assetKeys([]string{a}), "") + ")", llamaURL(a), filepath.Join(dl, a), llamaAssetSizes[a], ""})+ sha := llamaAssetSHA[a]+ if sha == "" {+ return fmt.Errorf("no pinned SHA-256 for llama.cpp runtime %s", a)+ }+ items = append(items, item{"llama.cpp runtime (" + strings.Join(assetKeys([]string{a}), "") + ")", llamaURL(a), filepath.Join(dl, a), llamaAssetSizes[a], sha})}}if err := m.fetchAll(ctx, items); err != nil {@@ -355,7 +396,9 @@ func (m *Manager) installGGUF(ctx context.Context, info sysinfo.Info) error {}if !runtimeReady {m.setPhase(PhaseInstalling, "Unpacking llama.cpp runtime", nil)- _ = os.RemoveAll(rdir)+ if err := os.RemoveAll(rdir); err != nil {+ return fmt.Errorf("replace llama.cpp runtime: %w", err)+ }for _, a := range plan.Assets {src := filepath.Join(dl, a)if err := download.Extract(src, rdir, download.CommonPrefixDepth(src)); err != nil {@@ -368,7 +411,11 @@ func (m *Manager) installGGUF(ctx context.Context, info sysinfo.Info) error {if findBinary(rdir, "llama-server") == "" {return errors.New("llama-server not found in the runtime archive")}- if err := os.WriteFile(filepath.Join(rdir, ".installed"), []byte(llamaTag+"\n"), 0o644); err != nil {+ manifest, ok := runtimeManifest(plan)+ if !ok {+ return errors.New("missing pinned llama.cpp runtime checksum")+ }+ if err := os.WriteFile(filepath.Join(rdir, ".installed"), []byte(manifest), 0o644); err != nil {return err}for _, a := range plan.Assets {@@ -388,19 +435,35 @@ func (m *Manager) installMLX(ctx context.Context, info sysinfo.Info) error {items = append(items, item{label, hfURL(mlxPack.Repo, mlxPack.Rev, f.Name), filepath.Join(mlxDir(), filepath.FromSlash(f.Name)), f.Size, f.SHA256})}uvBin := filepath.Join(uvDir(), "uv")- if !fileOK(uvBin, 0) {- items = append(items, item{"uv (Python manager)", uvURL(info.Arch), filepath.Join(paths.Runtimes(), "_downloads", uvAsset(info.Arch)), 0, ""})+ needUV := !uvInstalled(info.Arch)+ if needUV {+ asset := uvAsset(info.Arch)+ pinned, ok := uvAssets[asset]+ if !ok {+ return fmt.Errorf("no pinned SHA-256 for uv runtime %s", asset)+ }+ items = append(items, item{"uv (Python manager)", uvURL(info.Arch), filepath.Join(paths.Runtimes(), "_downloads", asset), pinned.Size, pinned.SHA256})}if err := m.fetchAll(ctx, items); err != nil {return err}- if !fileOK(uvBin, 0) {+ if needUV {src := filepath.Join(paths.Runtimes(), "_downloads", uvAsset(info.Arch))+ if err := os.RemoveAll(uvDir()); err != nil {+ return fmt.Errorf("replace uv runtime: %w", err)+ }if err := download.Extract(src, uvDir(), download.CommonPrefixDepth(src)); err != nil {return fmt.Errorf("unpack uv: %w", err)}- _ = os.Remove(src)+ if !fileOK(uvBin, 0) {+ return errors.New("uv binary missing from the archive")+ }trustMacBinaries(uvDir())+ asset := uvAsset(info.Arch)+ if err := os.WriteFile(filepath.Join(uvDir(), ".installed"), []byte(asset+" "+uvAssets[asset].SHA256+"\n"), 0o600); err != nil {+ return err+ }+ _ = os.Remove(src)}if fileOK(filepath.Join(mlxVenv(), ".installed"), 0) {return nildiff --git a/webbrain-systray/internal/localmodel/manager_test.go b/webbrain-systray/internal/localmodel/manager_test.goindex 680014784..b215e0020 100644--- a/webbrain-systray/internal/localmodel/manager_test.go+++ b/webbrain-systray/internal/localmodel/manager_test.go@@ -53,6 +53,64 @@ func TestStartClaimPreventsOverlappingLaunches(t *testing.T) {}}+func TestInstallClaimsPhaseBeforeWorkerStarts(t *testing.T) {+ t.Setenv("WBC_HOME", t.TempDir())+ store, err := settings.Open()+ if err != nil {+ t.Fatal(err)+ }+ m := NewManager(store, func([]core.LocalUpstream) error { return nil }, nil)+ if err := m.Install("gguf"); err != nil {+ t.Fatal(err)+ }+ defer m.Cancel()+ if err := m.Install("gguf"); err == nil {+ t.Fatal("overlapping install was accepted")+ }+}++func TestRuntimeArchivesHavePinnedChecksums(t *testing.T) {+ for asset, size := range llamaAssetSizes {+ if size <= 0 || len(llamaAssetSHA[asset]) != 64 {+ t.Errorf("llama runtime %s has no valid size and SHA-256", asset)+ }+ }+ for _, asset := range []string{uvAsset("arm64"), uvAsset("amd64")} {+ pinned, ok := uvAssets[asset]+ if !ok || pinned.Size <= 0 || len(pinned.SHA256) != 64 {+ t.Errorf("uv runtime %s has no valid size and SHA-256", asset)+ }+ }+}++func TestRuntimeRejectsLegacyUnverifiedMarker(t *testing.T) {+ t.Setenv("WBC_HOME", t.TempDir())+ plan := Plan{Assets: []string{"llama-" + llamaTag + "-bin-win-cpu-x64.zip"}}+ if err := os.MkdirAll(runtimeDir(plan), 0o700); err != nil {+ t.Fatal(err)+ }+ marker := filepath.Join(runtimeDir(plan), ".installed")+ if err := os.WriteFile(marker, []byte(llamaTag+"\n"), 0o600); err != nil {+ t.Fatal(err)+ }+ if runtimeInstalled(plan) {+ t.Fatal("legacy runtime marker bypassed pinned archive verification")+ }+}++func TestUVRejectsUnverifiedInstall(t *testing.T) {+ t.Setenv("WBC_HOME", t.TempDir())+ if err := os.MkdirAll(uvDir(), 0o700); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(uvDir(), "uv"), []byte("old uv"), 0o700); err != nil {+ t.Fatal(err)+ }+ if uvInstalled("arm64") {+ t.Fatal("uv without a pinned archive marker was accepted")+ }+}+func TestGGUFInstalledRejectsSameSizeCorruption(t *testing.T) {t.Setenv("WBC_HOME", t.TempDir())previous := gguf@@ -75,11 +133,15 @@ func TestGGUFInstalledRejectsSameSizeCorruption(t *testing.T) {if err := os.WriteFile(projectorPath, projector, 0o600); err != nil {t.Fatal(err)}- plan := Plan{Assets: []string{"llama-test.tar.gz"}}+ plan := Plan{Assets: []string{"llama-" + llamaTag + "-bin-win-cpu-x64.zip"}}if err := os.MkdirAll(runtimeDir(plan), 0o700); err != nil {t.Fatal(err)}- if err := os.WriteFile(filepath.Join(runtimeDir(plan), ".installed"), []byte("ready"), 0o600); err != nil {+ manifest, ok := runtimeManifest(plan)+ if !ok {+ t.Fatal("test runtime has no pinned checksum")+ }+ if err := os.WriteFile(filepath.Join(runtimeDir(plan), ".installed"), []byte(manifest), 0o600); err != nil {t.Fatal(err)}m := NewManager(nil, nil, nil)