Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: confine profile import destinations

Luigi Agosti6b86f2d95ea1
diff --git a/internal/profiles/copydir.go b/internal/profiles/copydir.go
index 97835773..82d3e2dc 100644
--- a/internal/profiles/copydir.go
+++ b/internal/profiles/copydir.go
@@ -8,7 +8,7 @@ import (
"path/filepath"
)
-func copyDir(src *importSource, dst string) error {
+func copyDir(src *importSource, dst *os.Root) error {
return fs.WalkDir(src.root.FS(), filepath.ToSlash(src.relative), func(sourcePath string, d fs.DirEntry, err error) error {
if err != nil {
return err
@@ -21,22 +21,25 @@ func copyDir(src *importSource, dst string) error {
if err != nil {
return err
}
- target := filepath.Join(dst, filepath.FromSlash(rel))
+ target := filepath.FromSlash(rel)
+ if !filepath.IsLocal(target) {
+ return fmt.Errorf("import path escapes destination: %s", sourcePath)
+ }
if d.IsDir() {
info, err := d.Info()
if err != nil {
return err
}
- return os.MkdirAll(target, info.Mode().Perm())
+ return dst.MkdirAll(target, info.Mode().Perm())
}
- return copyFile(src.root, filepath.FromSlash(sourcePath), target)
+ return copyFile(src.root, filepath.FromSlash(sourcePath), dst, target)
})
}
-func copyFile(root *os.Root, src, dst string) error {
- in, err := root.Open(src)
+func copyFile(srcRoot *os.Root, src string, dstRoot *os.Root, dst string) error {
+ in, err := srcRoot.Open(src)
if err != nil {
return fmt.Errorf("open %s: %w", src, err)
}
@@ -47,7 +50,7 @@ func copyFile(root *os.Root, src, dst string) error {
return err
}
- out, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode().Perm())
+ out, err := dstRoot.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode().Perm())
if err != nil {
return fmt.Errorf("create %s: %w", dst, err)
}
diff --git a/internal/profiles/copydir_test.go b/internal/profiles/copydir_test.go
new file mode 100644
index 00000000..6cd96534
--- /dev/null
+++ b/internal/profiles/copydir_test.go
@@ -0,0 +1,40 @@
+package profiles
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func TestCopyDirRejectsDestinationSymlinkEscape(t *testing.T) {
+ srcPath := filepath.Join(t.TempDir(), "source")
+ if err := os.MkdirAll(filepath.Join(srcPath, "Default"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(srcPath, "Default", "Preferences"), []byte("source"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ src, err := openImportSource(srcPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer func() { _ = src.root.Close() }()
+
+ dst := t.TempDir()
+ outside := t.TempDir()
+ if err := os.Symlink(outside, filepath.Join(dst, "Default")); err != nil {
+ t.Skipf("symlinks unavailable: %v", err)
+ }
+ dstRoot, err := os.OpenRoot(dst)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer func() { _ = dstRoot.Close() }()
+
+ if err := copyDir(src, dstRoot); err == nil {
+ t.Fatal("copyDir followed a destination symlink outside its root")
+ }
+ if _, err := os.Stat(filepath.Join(outside, "Preferences")); !os.IsNotExist(err) {
+ t.Fatalf("copyDir wrote outside its destination root: %v", err)
+ }
+}
diff --git a/internal/profiles/identity.go b/internal/profiles/identity.go
index 1f7dbd58..fa2a9a0f 100644
--- a/internal/profiles/identity.go
+++ b/internal/profiles/identity.go
@@ -101,9 +101,25 @@ func readProfileMeta(profileDir string) ProfileMeta {
}
func writeProfileMeta(profileDir string, meta ProfileMeta) error {
- data, err := json.MarshalIndent(meta, "", " ")
+ data, err := profileMetaJSON(meta)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(profileDir, "profile.json"), data, 0644)
}
+
+func writeProfileMetaRoot(root *os.Root, meta ProfileMeta) error {
+ data, err := profileMetaJSON(meta)
+ if err != nil {
+ return err
+ }
+ return root.WriteFile("profile.json", data, 0644)
+}
+
+func profileMetaJSON(meta ProfileMeta) ([]byte, error) {
+ data, err := json.MarshalIndent(meta, "", " ")
+ if err != nil {
+ return nil, err
+ }
+ return data, nil
+}
diff --git a/internal/profiles/profiles_crud.go b/internal/profiles/profiles_crud.go
index b667a663..c8612d1e 100644
--- a/internal/profiles/profiles_crud.go
+++ b/internal/profiles/profiles_crud.go
@@ -34,7 +34,7 @@ func (pm *ProfileManager) Import(name, sourcePath string) error {
pm.mu.Lock()
defer pm.mu.Unlock()
- _, dest, err := pm.preflightProfileDestination(name, nil)
+ id, _, err := pm.preflightProfileDestination(name, nil)
if err != nil {
return err
}
@@ -62,25 +62,44 @@ func (pm *ProfileManager) Import(name, sourcePath string) error {
}
slog.Info("importing profile", "name", name, "source", source.displayPath)
- // Import is all-or-nothing. preflight already established that dest did not
- // exist, so anything under it now is ours to remove — and leaving a partial
- // copy would make every retry fail with "already exists" instead of the real
- // cause (a live Chrome user data dir has Singleton* symlinks copyDir rejects).
- if err := copyDir(source, dest); err != nil {
- _ = os.RemoveAll(dest)
+ // Import is all-or-nothing. The rooted Mkdir claims the preflighted id
+ // atomically, so anything under it is ours to remove on failure.
+ baseRoot, err := os.OpenRoot(pm.baseDir)
+ if err != nil {
+ return fmt.Errorf("open profile root: %w", err)
+ }
+ defer func() { _ = baseRoot.Close() }()
+ if err := baseRoot.Mkdir(id, srcInfo.Mode().Perm()); err != nil {
+ return fmt.Errorf("create profile destination: %w", err)
+ }
+ destRoot, err := baseRoot.OpenRoot(id)
+ if err != nil {
+ _ = baseRoot.RemoveAll(id)
+ return fmt.Errorf("open profile destination: %w", err)
+ }
+ cleanup := func() {
+ _ = destRoot.Close()
+ _ = baseRoot.RemoveAll(id)
+ }
+ if err := copyDir(source, destRoot); err != nil {
+ cleanup()
return fmt.Errorf("copy failed: %w", err)
}
- if err := os.WriteFile(filepath.Join(dest, ".pinchtab-imported"), []byte(source.displayPath), 0600); err != nil {
+ if err := destRoot.WriteFile(".pinchtab-imported", []byte(source.displayPath), 0600); err != nil {
slog.Warn("failed to write import marker", "err", err)
}
- if err := writeProfileMeta(dest, ProfileMeta{
- ID: profileID(name),
+ if err := writeProfileMetaRoot(destRoot, ProfileMeta{
+ ID: id,
Name: name,
}); err != nil {
- _ = os.RemoveAll(dest)
+ cleanup()
return err
}
+ if err := destRoot.Close(); err != nil {
+ _ = baseRoot.RemoveAll(id)
+ return fmt.Errorf("close profile destination: %w", err)
+ }
return nil
}