Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Verify extracted llama runtime files before reuse

Barack Sokullu5548a8684d94
diff --git a/webbrain-systray/internal/localmodel/manager.go b/webbrain-systray/internal/localmodel/manager.go
index 56a4303c1..716a91a7e 100644
--- a/webbrain-systray/internal/localmodel/manager.go
+++ b/webbrain-systray/internal/localmodel/manager.go
@@ -2,8 +2,12 @@ package localmodel
import (
"context"
+ "crypto/sha256"
+ "encoding/hex"
+ "encoding/json"
"errors"
"fmt"
+ "io"
"io/fs"
"net"
"net/http"
@@ -146,13 +150,100 @@ func runtimeManifest(p Plan) (string, bool) {
return llamaTag + "\n" + strings.Join(hashes, "\n") + "\n", len(hashes) > 0
}
+type runtimeFile struct {
+ Path string `json:"path"`
+ Size int64 `json:"size"`
+ SHA256 string `json:"sha256"`
+}
+
+type runtimeInstallation struct {
+ Archives string `json:"archives"`
+ Files []runtimeFile `json:"files"`
+}
+
+var runtimeVerifier download.Verifier
+
+// installedRuntimeManifest records the contents of the verified archives
+// after extraction and macOS signing. It lets later installs repair damaged
+// runtime files without downloading the unchanged model weights.
+func installedRuntimeManifest(p Plan) ([]byte, error) {
+ archives, ok := runtimeManifest(p)
+ if !ok {
+ return nil, errors.New("missing pinned llama.cpp runtime checksum")
+ }
+ dir := runtimeDir(p)
+ installed := runtimeInstallation{Archives: archives}
+ err := filepath.WalkDir(dir, func(path string, entry fs.DirEntry, err error) error {
+ if err != nil {
+ return err
+ }
+ if entry.IsDir() || path == filepath.Join(dir, ".installed") {
+ return nil
+ }
+ info, err := os.Stat(path) // follow archive symlinks to their contents
+ if err != nil {
+ return err
+ }
+ if !info.Mode().IsRegular() {
+ return fmt.Errorf("runtime entry is not a file: %s", path)
+ }
+ f, err := os.Open(path)
+ if err != nil {
+ return err
+ }
+ h := sha256.New()
+ _, copyErr := io.Copy(h, f)
+ closeErr := f.Close()
+ if err := errors.Join(copyErr, closeErr); err != nil {
+ return err
+ }
+ rel, err := filepath.Rel(dir, path)
+ if err != nil {
+ return err
+ }
+ installed.Files = append(installed.Files, runtimeFile{filepath.ToSlash(rel), info.Size(), hex.EncodeToString(h.Sum(nil))})
+ return nil
+ })
+ if err != nil {
+ return nil, err
+ }
+ if len(installed.Files) == 0 {
+ return nil, errors.New("llama.cpp runtime archive is empty")
+ }
+ return json.Marshal(installed)
+}
+
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
+ dir := runtimeDir(p)
+ data, err := os.ReadFile(filepath.Join(dir, ".installed"))
+ if err != nil {
+ return false
+ }
+ var installed runtimeInstallation
+ if json.Unmarshal(data, &installed) != nil || installed.Archives != want || len(installed.Files) == 0 {
+ return false
+ }
+ binary := "llama-server"
+ if runtime.GOOS == "windows" {
+ binary += ".exe"
+ }
+ binaryVerified := false
+ for _, f := range installed.Files {
+ 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) {
+ return false
+ }
+ if filepath.Base(f.Path) == binary {
+ binaryVerified = true
+ }
+ }
+ return binaryVerified
}
func uvInstalled(arch string) bool {
@@ -429,11 +520,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")
}
- manifest, ok := runtimeManifest(plan)
- if !ok {
- return errors.New("missing pinned llama.cpp runtime checksum")
+ manifest, err := installedRuntimeManifest(plan)
+ if err != nil {
+ return fmt.Errorf("verify unpacked llama.cpp runtime: %w", err)
}
- if err := os.WriteFile(filepath.Join(rdir, ".installed"), []byte(manifest), 0o644); err != nil {
+ if err := os.WriteFile(filepath.Join(rdir, ".installed"), manifest, 0o644); err != nil {
return err
}
for _, a := range plan.Assets {
diff --git a/webbrain-systray/internal/localmodel/manager_test.go b/webbrain-systray/internal/localmodel/manager_test.go
index fbbf37ee9..ebec9da3a 100644
--- a/webbrain-systray/internal/localmodel/manager_test.go
+++ b/webbrain-systray/internal/localmodel/manager_test.go
@@ -6,6 +6,7 @@ import (
"errors"
"os"
"path/filepath"
+ "runtime"
"strings"
"sync/atomic"
"testing"
@@ -180,6 +181,68 @@ func TestRuntimeRejectsLegacyUnverifiedMarker(t *testing.T) {
}
}
+func TestRuntimeInstalledRejectsMissingAndCorruptExtractedFiles(t *testing.T) {
+ t.Setenv("WBC_HOME", t.TempDir())
+ plan := Plan{Assets: []string{"llama-" + llamaTag + "-bin-win-cpu-x64.zip"}}
+ dir := runtimeDir(plan)
+ if err := os.MkdirAll(dir, 0o700); err != nil {
+ t.Fatal(err)
+ }
+ server := "llama-server"
+ if runtime.GOOS == "windows" {
+ server += ".exe"
+ }
+ serverPath := filepath.Join(dir, server)
+ libraryPath := filepath.Join(dir, "libggml.so")
+ if err := os.WriteFile(serverPath, []byte("server"), 0o700); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(libraryPath, []byte("library"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ manifest, err := installedRuntimeManifest(plan)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, ".installed"), manifest, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ if !runtimeInstalled(plan) {
+ t.Fatal("valid extracted runtime was rejected")
+ }
+ if err := os.Remove(libraryPath); err != nil {
+ t.Fatal(err)
+ }
+ if runtimeInstalled(plan) {
+ t.Fatal("runtime with a missing library was accepted")
+ }
+ if err := os.WriteFile(libraryPath, []byte("Library"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ modified := time.Now().Add(time.Minute)
+ if err := os.Chtimes(libraryPath, modified, modified); err != nil {
+ t.Fatal(err)
+ }
+ if runtimeInstalled(plan) {
+ t.Fatal("runtime with a same-sized corrupt library was accepted")
+ }
+ if err := os.WriteFile(libraryPath, []byte("library"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Chtimes(libraryPath, modified.Add(time.Minute), modified.Add(time.Minute)); err != nil {
+ t.Fatal(err)
+ }
+ if !runtimeInstalled(plan) {
+ t.Fatal("repaired runtime was rejected")
+ }
+ if err := os.Remove(serverPath); err != nil {
+ t.Fatal(err)
+ }
+ if runtimeInstalled(plan) {
+ t.Fatal("runtime with a missing llama-server was accepted")
+ }
+}
+
func TestUVRejectsUnverifiedInstall(t *testing.T) {
t.Setenv("WBC_HOME", t.TempDir())
if err := os.MkdirAll(uvDir(), 0o700); err != nil {
@@ -243,11 +306,18 @@ func TestGGUFInstalledRejectsSameSizeCorruption(t *testing.T) {
if err := os.MkdirAll(runtimeDir(plan), 0o700); err != nil {
t.Fatal(err)
}
- manifest, ok := runtimeManifest(plan)
- if !ok {
- t.Fatal("test runtime has no pinned checksum")
+ server := "llama-server"
+ if runtime.GOOS == "windows" {
+ server += ".exe"
+ }
+ if err := os.WriteFile(filepath.Join(runtimeDir(plan), server), []byte("server"), 0o700); err != nil {
+ t.Fatal(err)
+ }
+ manifest, err := installedRuntimeManifest(plan)
+ if err != nil {
+ t.Fatal(err)
}
- if err := os.WriteFile(filepath.Join(runtimeDir(plan), ".installed"), []byte(manifest), 0o600); err != nil {
+ if err := os.WriteFile(filepath.Join(runtimeDir(plan), ".installed"), manifest, 0o600); err != nil {
t.Fatal(err)
}
m := NewManager(nil, nil, nil)