High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: constrain profile import paths
Luigi Agosti7a98692c1158
diff --git a/internal/profiles/copydir.go b/internal/profiles/copydir.goindex 89b897ea..97835773 100644--- a/internal/profiles/copydir.go+++ b/internal/profiles/copydir.go@@ -8,20 +8,20 @@ import ("path/filepath")-func copyDir(src, dst string) error {- return filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {+func copyDir(src *importSource, dst string) 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}if d.Type()&os.ModeSymlink != 0 {- return fmt.Errorf("symlinks are not allowed in imported profiles: %s", path)+ return fmt.Errorf("symlinks are not allowed in imported profiles: %s", sourcePath)}- rel, err := filepath.Rel(src, path)+ rel, err := filepath.Rel(filepath.FromSlash(src.relative), filepath.FromSlash(sourcePath))if err != nil {return err}- target := filepath.Join(dst, rel)+ target := filepath.Join(dst, filepath.FromSlash(rel))if d.IsDir() {info, err := d.Info()@@ -31,12 +31,12 @@ func copyDir(src, dst string) error {return os.MkdirAll(target, info.Mode().Perm())}- return copyFile(path, target)+ return copyFile(src.root, filepath.FromSlash(sourcePath), target)})}-func copyFile(src, dst string) error {- in, err := os.Open(src) // #nosec G304 — src is validated by caller+func copyFile(root *os.Root, src, dst string) error {+ in, err := root.Open(src)if err != nil {return fmt.Errorf("open %s: %w", src, err)}diff --git a/internal/profiles/profiles_crud.go b/internal/profiles/profiles_crud.goindex df004f0b..b667a663 100644--- a/internal/profiles/profiles_crud.go+++ b/internal/profiles/profiles_crud.go@@ -39,18 +39,13 @@ func (pm *ProfileManager) Import(name, sourcePath string) error {return err}- resolvedSourcePath, err := resolveImportSourcePath(sourcePath)+ source, err := openImportSource(sourcePath)if err != nil {return err}+ defer func() { _ = source.root.Close() }()- if _, err := os.Stat(filepath.Join(resolvedSourcePath, "Default")); err != nil {- if _, err2 := os.Stat(filepath.Join(resolvedSourcePath, "Preferences")); err2 != nil {- return fmt.Errorf("source doesn't look like a Chrome user data dir (no Default/ or Preferences found)")- }- }-- srcInfo, err := os.Lstat(resolvedSourcePath)+ srcInfo, err := source.root.Lstat(source.relative)if err != nil {return fmt.Errorf("source path invalid: %w", err)}@@ -60,18 +55,23 @@ func (pm *ProfileManager) Import(name, sourcePath string) error {if !srcInfo.IsDir() {return fmt.Errorf("source path must be a directory")}+ if _, err := source.root.Stat(source.child("Default")); err != nil {+ if _, err2 := source.root.Stat(source.child("Preferences")); err2 != nil {+ return fmt.Errorf("source doesn't look like a Chrome user data dir (no Default/ or Preferences found)")+ }+ }- slog.Info("importing profile", "name", name, "source", resolvedSourcePath)+ 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(resolvedSourcePath, dest); err != nil {+ if err := copyDir(source, dest); err != nil {_ = os.RemoveAll(dest)return fmt.Errorf("copy failed: %w", err)}- if err := os.WriteFile(filepath.Join(dest, ".pinchtab-imported"), []byte(resolvedSourcePath), 0600); err != nil {+ if err := os.WriteFile(filepath.Join(dest, ".pinchtab-imported"), []byte(source.displayPath), 0600); err != nil {slog.Warn("failed to write import marker", "err", err)}if err := writeProfileMeta(dest, ProfileMeta{diff --git a/internal/profiles/profiles_import.go b/internal/profiles/profiles_import.goindex f4810b6c..2d9e57c9 100644--- a/internal/profiles/profiles_import.go+++ b/internal/profiles/profiles_import.go@@ -7,30 +7,49 @@ import ("strings")-func resolveImportSourcePath(sourcePath string) (string, error) {+type importSource struct {+ root *os.Root+ relative string+ displayPath string+}++func openImportSource(sourcePath string) (*importSource, error) {if sourcePath == "" {- return "", fmt.Errorf("source path required")+ return nil, fmt.Errorf("source path required")}cleaned := filepath.Clean(sourcePath)if !filepath.IsAbs(cleaned) {abs, err := filepath.Abs(cleaned)if err != nil {- return "", fmt.Errorf("source path invalid: %w", err)+ return nil, fmt.Errorf("source path invalid: %w", err)}cleaned = abs}roots, err := allowedImportRoots()if err != nil {- return "", err+ return nil, err}- for _, root := range roots {- if pathWithinRoot(cleaned, root) {- return cleaned, nil+ for _, rootPath := range roots {+ relative, err := filepath.Rel(rootPath, cleaned)+ if err != nil || !pathWithinRoot(cleaned, rootPath) {+ continue+ }+ root, err := os.OpenRoot(rootPath)+ if err != nil {+ return nil, fmt.Errorf("open import root: %w", err)}+ return &importSource{root: root, relative: relative, displayPath: cleaned}, nil+ }+ return nil, fmt.Errorf("source path must be within %s", strings.Join(roots, " or "))+}++func (s *importSource) child(name string) string {+ if s.relative == "." {+ return name}- return "", fmt.Errorf("source path must be within %s", strings.Join(roots, " or "))+ return filepath.Join(s.relative, name)}func allowedImportRoots() ([]string, error) {diff --git a/internal/profiles/profiles_test.go b/internal/profiles/profiles_test.goindex dc257817..0243f644 100644--- a/internal/profiles/profiles_test.go+++ b/internal/profiles/profiles_test.go@@ -169,6 +169,27 @@ func TestProfileManagerImportRejectsSymlinkSource(t *testing.T) {}}+func TestProfileManagerImportRejectsIntermediateSymlinkOutsideRoot(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("rooted symlink test requires Unix symlink semantics")+ }++ dir := t.TempDir()+ pm := NewProfileManager(dir)+ link := filepath.Join(t.TempDir(), "escape")+ if err := os.Symlink(string(os.PathSeparator), link); err != nil {+ t.Skipf("symlink not supported: %v", err)+ }++ err := pm.Import("escaped-source", filepath.Join(link, "etc"))+ if err == nil || !strings.Contains(err.Error(), "source path invalid") {+ t.Fatalf("expected rooted source validation to reject the escaping symlink, got %v", err)+ }+ if _, statErr := os.Stat(filepath.Join(dir, profileID("escaped-source"))); !os.IsNotExist(statErr) {+ t.Fatalf("rejected import created a destination: %v", statErr)+ }+}+func TestProfileManagerImportRejectsSymlinkEntry(t *testing.T) {dir := t.TempDir()pm := NewProfileManager(dir)