The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
package selfupdate
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/filelock"
)
// maxTarEntryBytes bounds a single file extracted from a test artifact.
const maxTarEntryBytes = 256 << 20
// acquireLock takes an exclusive advisory lock on path and returns a release
// func. It is non-blocking: if another process holds the lock it returns an
// error rather than waiting, so overlapping updater runs do not pile up.
func acquireLock(path string) (func(), error) {
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o644)
if err != nil {
return nil, err
}
held, err := filelock.TryExclusive(f)
if err != nil {
_ = f.Close()
return nil, err
}
return func() {
_ = held.Release()
_ = f.Close()
}, nil
}
// safeJoin resolves an archive entry name against dest and rejects anything that
// would escape dest (path traversal / "zip slip"). It rejects absolute paths,
// any name containing a ".." sequence (stricter than rejecting only ".."
// segments, which is fine because release archives never carry ".." in entry
// names, and a direct check on the raw name is what taint analyzers recognize
// as a sanitizer), and any entry whose cleaned, joined path is not contained
// within dest.
func safeJoin(dest, name string) (string, error) {
if filepath.IsAbs(name) || strings.HasPrefix(name, "/") || strings.HasPrefix(name, "\\") {
return "", fmt.Errorf("absolute path in archive entry: %q", name)
}
if strings.Contains(name, "..") {
return "", fmt.Errorf("path traversal in archive entry: %q", name)
}
cleanDest := filepath.Clean(dest)
target := filepath.Clean(filepath.Join(cleanDest, name))
if target != cleanDest && !strings.HasPrefix(target, cleanDest+string(os.PathSeparator)) {
return "", fmt.Errorf("archive entry escapes destination: %q", name)
}
return target, nil
}
// extractTarballInto expands a gzipped tar into dest, guarding against path
// traversal. It is used only by the insecure test seam; production installs go
// through the macOS installer.
func extractTarballInto(tarball, dest string) error {
f, err := os.Open(tarball)
if err != nil {
return err
}
defer f.Close()
gz, err := gzip.NewReader(f)
if err != nil {
return fmt.Errorf("gzip: %w", err)
}
defer gz.Close()
tr := tar.NewReader(gz)
cleanDest := filepath.Clean(dest)
for {
hdr, err := tr.Next()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
target, err := safeJoin(cleanDest, hdr.Name)
if err != nil {
return err
}
switch hdr.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(target, 0o755); err != nil {
return err
}
case tar.TypeReg:
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
return err
}
out, err := os.OpenFile(target, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(hdr.Mode)&0o777)
if err != nil {
return err
}
if _, err := io.Copy(out, io.LimitReader(tr, maxTarEntryBytes)); err != nil {
_ = out.Close()
return err
}
if err := out.Close(); err != nil {
return err
}
}
}
}
// copyTree recursively copies src to dst, preserving file modes.
func copyTree(src, dst string) error {
info, err := os.Stat(src)
if err != nil {
return err
}
if info.IsDir() {
if err := os.MkdirAll(dst, info.Mode().Perm()); err != nil {
return err
}
entries, err := os.ReadDir(src)
if err != nil {
return err
}
for _, e := range entries {
if err := copyTree(filepath.Join(src, e.Name()), filepath.Join(dst, e.Name())); err != nil {
return err
}
}
return nil
}
return copyFile(src, dst, info.Mode().Perm())
}
func copyFile(src, dst string, mode os.FileMode) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
return err
}
out, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, mode)
if err != nil {
return err
}
if _, err := io.Copy(out, in); err != nil {
_ = out.Close()
return err
}
return out.Close()
}