High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: quarantined profiles keep the original name in profile.json, so four directories share one profile ID PIN-027
Luigi Agosti079bed5b04c5
diff --git a/internal/bridge/profile_lock.go b/internal/bridge/profile_lock.goindex 8ec5d305..a3cb8c1c 100644--- a/internal/bridge/profile_lock.go+++ b/internal/bridge/profile_lock.go@@ -151,6 +151,11 @@ func quarantineCorruptedProfile(profileDir string) (string, error) {if err := os.Rename(profileDir, quarantinePath); err != nil {return "", fmt.Errorf("rename profile dir: %w", err)}+ // The rename carries profile.json along, where it now names a profile this+ // directory no longer is. Drop it so nothing on disk makes that claim.+ if err := os.Remove(filepath.Join(quarantinePath, "profile.json")); err != nil && !os.IsNotExist(err) {+ slog.Warn("stale profile metadata left in quarantined profile", "profile", quarantinePath, "err", err)+ }if err := os.MkdirAll(profileDir, 0700); err != nil {return quarantinePath, fmt.Errorf("recreate profile dir: %w", err)}diff --git a/internal/bridge/profile_lock_test.go b/internal/bridge/profile_lock_test.goindex e4a470f5..ae900c15 100644--- a/internal/bridge/profile_lock_test.go+++ b/internal/bridge/profile_lock_test.go@@ -390,3 +390,54 @@ func TestQuarantineCorruptedProfile_ProceedsAfterWaitTimeout(t *testing.T) {t.Fatal("expected a quarantine path despite the timed-out wait")}}++// A quarantined directory that keeps profile.json still claims to be the+// profile it was moved away from; the listing then shows two directories under+// one name and one ID.+func TestQuarantineDropsTheStaleProfileMetadata(t *testing.T) {+ t.Parallel()++ tmp := t.TempDir()+ profileDir := filepath.Join(tmp, "default")+ if err := os.MkdirAll(filepath.Join(profileDir, "Default"), 0755); err != nil {+ t.Fatalf("setup: %v", err)+ }+ meta := []byte(`{"id":"prof_37a8eec1","name":"default"}`)+ if err := os.WriteFile(filepath.Join(profileDir, "profile.json"), meta, 0644); err != nil {+ t.Fatalf("setup metadata: %v", err)+ }++ quarantinePath, err := quarantineCorruptedProfile(profileDir)+ if err != nil {+ t.Fatalf("quarantineCorruptedProfile: %v", err)+ }++ if _, err := os.Stat(filepath.Join(quarantinePath, "profile.json")); !os.IsNotExist(err) {+ data, _ := os.ReadFile(filepath.Join(quarantinePath, "profile.json"))+ t.Fatalf("quarantined directory still claims a profile identity: %s", data)+ }+ if _, err := os.Stat(filepath.Join(quarantinePath, "Default")); err != nil {+ t.Fatalf("quarantine must preserve the rest of the profile: %v", err)+ }+ if _, err := os.Stat(profileDir); err != nil {+ t.Fatalf("live profile directory not recreated: %v", err)+ }+}++func TestQuarantineWithoutMetadataStillSucceeds(t *testing.T) {+ t.Parallel()++ tmp := t.TempDir()+ profileDir := filepath.Join(tmp, "default")+ if err := os.MkdirAll(filepath.Join(profileDir, "Default"), 0755); err != nil {+ t.Fatalf("setup: %v", err)+ }++ quarantinePath, err := quarantineCorruptedProfile(profileDir)+ if err != nil {+ t.Fatalf("quarantineCorruptedProfile: %v", err)+ }+ if _, err := os.Stat(filepath.Join(quarantinePath, "Default")); err != nil {+ t.Fatalf("quarantined directory incomplete: %v", err)+ }+}diff --git a/internal/profiles/profiles.go b/internal/profiles/profiles.goindex d1f335f1..72f8caba 100644--- a/internal/profiles/profiles.go+++ b/internal/profiles/profiles.go@@ -73,8 +73,7 @@ func (pm *ProfileManager) findProfileDirByName(name string) (string, error) {if entry.Name() == profileID(name) {return dir, nil}- meta := readProfileMeta(dir)- if meta.Name == name {+ if trustedProfileMeta(pm.baseDir, entry.Name()).Name == name {return dir, nil}}@@ -151,6 +150,21 @@ func (pm *ProfileManager) List() ([]bridge.ProfileInfo, error) {return profiles, nil}+// trustedProfileMeta reads dirName's profile.json and keeps it only for a+// directory the metadata actually owns: the profile's own name, or the ID+// derived from it, which is what create and import use. Quarantine renames a+// directory and carries profile.json along, so the file then names the profile+// the directory used to be; trusting it would give both directories one name,+// and ProfileID hashes the name, so one ID. Falling back to the directory name+// keeps them distinct, since directory names are unique within baseDir.+func trustedProfileMeta(baseDir, dirName string) ProfileMeta {+ meta := readProfileMeta(filepath.Join(baseDir, dirName))+ if meta.Name == "" || dirName == meta.Name || dirName == profileID(meta.Name) {+ return meta+ }+ return ProfileMeta{}+}+func (pm *ProfileManager) profileInfo(dirName string) (ProfileDetailedInfo, error) {if err := ValidateProfileName(dirName); err != nil {return ProfileDetailedInfo{}, err@@ -168,7 +182,7 @@ func (pm *ProfileManager) profileInfo(dirName string) (ProfileDetailedInfo, erro}chromeProfileName, accountEmail, accountName, hasAccount := readChromeProfileIdentity(dir)- meta := readProfileMeta(dir)+ meta := trustedProfileMeta(pm.baseDir, dirName)profileName := meta.Nameif profileName == "" {profileName = dirName@@ -212,8 +226,7 @@ func (pm *ProfileManager) FindByID(id string) (string, error) {if !entry.IsDir() {continue}- dir := filepath.Join(pm.baseDir, entry.Name())- meta := readProfileMeta(dir)+ meta := trustedProfileMeta(pm.baseDir, entry.Name())if meta.ID == id {if meta.Name != "" {return meta.Name, nildiff --git a/internal/profiles/profiles_test.go b/internal/profiles/profiles_test.goindex 64a6b8e6..4b21ad0c 100644--- a/internal/profiles/profiles_test.go+++ b/internal/profiles/profiles_test.go@@ -1060,3 +1060,193 @@ func TestProfileRenameRejectsDuplicate(t *testing.T) {t.Errorf("expected 'already exists' error, got: %v", err)}}++// seedProfileDir writes a profile directory with metadata naming metaName,+// which is how a directory copied or renamed out from under its profile.json+// looks on disk.+func seedProfileDir(t *testing.T, baseDir, dirName, metaName string) {+ t.Helper()+ dir := filepath.Join(baseDir, dirName)+ if err := os.MkdirAll(filepath.Join(dir, "Default"), 0755); err != nil {+ t.Fatalf("MkdirAll: %v", err)+ }+ if metaName == "" {+ return+ }+ if err := writeProfileMeta(dir, ProfileMeta{ID: profileID(metaName), Name: metaName}); err != nil {+ t.Fatalf("writeProfileMeta: %v", err)+ }+}++func listedByPath(t *testing.T, pm *ProfileManager, dirName string) bridge.ProfileInfo {+ t.Helper()+ profiles, err := pm.List()+ if err != nil {+ t.Fatalf("List: %v", err)+ }+ for _, p := range profiles {+ if filepath.Base(p.Path) == dirName {+ return p+ }+ }+ t.Fatalf("directory %q missing from the listing: %+v", dirName, profiles)+ return bridge.ProfileInfo{}+}++func TestListMetadataNamingAnotherProfileFallsBackToTheDirectoryName(t *testing.T) {+ base := t.TempDir()+ pm := NewProfileManager(base)+ if err := pm.Create("live"); err != nil {+ t.Fatal(err)+ }+ seedProfileDir(t, base, "live.quarantine-1785345247", "live")++ live := listedByPath(t, pm, profileID("live"))+ stale := listedByPath(t, pm, "live.quarantine-1785345247")++ if stale.Name != "live.quarantine-1785345247" {+ t.Fatalf("quarantined entry name = %q, want its directory name", stale.Name)+ }+ if stale.ID == live.ID {+ t.Fatalf("quarantined entry shares the live profile's ID %q", stale.ID)+ }+ if stale.ID != profileID("live.quarantine-1785345247") {+ t.Fatalf("quarantined ID = %q, want the ID derived from its directory name", stale.ID)+ }+}++func TestListNeverReturnsTwoProfilesWithOneID(t *testing.T) {+ base := t.TempDir()+ pm := NewProfileManager(base)+ if err := pm.Create("live"); err != nil {+ t.Fatal(err)+ }+ for _, dirName := range []string{"live.quarantine-1", "live.quarantine-2", "copy-of-live"} {+ seedProfileDir(t, base, dirName, "live")+ }++ profiles, err := pm.List()+ if err != nil {+ t.Fatal(err)+ }+ if len(profiles) != 4 {+ t.Fatalf("expected all 4 directories listed, got %d", len(profiles))+ }+ seen := map[string]string{}+ for _, p := range profiles {+ if prev, dup := seen[p.ID]; dup {+ t.Fatalf("duplicate id %q shared by %q and %q", p.ID, prev, p.Path)+ }+ seen[p.ID] = p.Path+ }+}++func TestListKeepsMetadataWhenItNamesItsOwnDirectory(t *testing.T) {+ base := t.TempDir()+ pm := NewProfileManager(base)+ if err := pm.CreateWithMeta("owned", ProfileMeta{UseWhen: "reading the metadata still works"}); err != nil {+ t.Fatal(err)+ }+ // A name-named directory is the other legitimate layout; both must keep+ // reading their metadata. UseWhen only survives when the metadata is kept,+ // so it distinguishes "trusted" from "fell back to the directory name".+ seedProfileDir(t, base, "by-name", "by-name")+ if err := writeProfileMeta(filepath.Join(base, "by-name"), ProfileMeta{+ ID: profileID("by-name"),+ Name: "by-name",+ UseWhen: "a name-named directory keeps its metadata too",+ }); err != nil {+ t.Fatalf("writeProfileMeta: %v", err)+ }++ idNamed := listedByPath(t, pm, profileID("owned"))+ if idNamed.Name != "owned" || idNamed.UseWhen != "reading the metadata still works" {+ t.Fatalf("id-named directory lost its metadata: name=%q useWhen=%q", idNamed.Name, idNamed.UseWhen)+ }+ nameNamed := listedByPath(t, pm, "by-name")+ if nameNamed.Name != "by-name" || nameNamed.ID != profileID("by-name") {+ t.Fatalf("name-named directory: name=%q id=%q", nameNamed.Name, nameNamed.ID)+ }+ if nameNamed.UseWhen != "a name-named directory keeps its metadata too" {+ t.Fatalf("name-named directory lost its metadata: useWhen=%q", nameNamed.UseWhen)+ }+}++func TestListWithoutMetadataStillDerivesTheNameFromTheDirectory(t *testing.T) {+ base := t.TempDir()+ pm := NewProfileManager(base)+ seedProfileDir(t, base, "bare-dir", "")++ got := listedByPath(t, pm, "bare-dir")+ if got.Name != "bare-dir" || got.ID != profileID("bare-dir") {+ t.Fatalf("no-metadata fallback changed: name=%q id=%q", got.Name, got.ID)+ }+}++func TestResolutionByNameAndIDIgnoresADirectoryClaimingAnotherProfile(t *testing.T) {+ base := t.TempDir()+ pm := NewProfileManager(base)+ if err := pm.Create("live"); err != nil {+ t.Fatal(err)+ }+ seedProfileDir(t, base, "live.quarantine-1785345247", "live")+ liveDir := filepath.Join(base, profileID("live"))++ path, err := pm.ProfilePath("live")+ if err != nil {+ t.Fatalf("resolve by name: %v", err)+ }+ if path != liveDir {+ t.Fatalf("--profile live resolved to %q, want the live directory %q", path, liveDir)+ }++ name, err := pm.FindByID(profileID("live"))+ if err != nil {+ t.Fatalf("resolve by id: %v", err)+ }+ if name != "live" {+ t.Fatalf("FindByID returned %q, want live", name)+ }+ quarantined, err := pm.FindByID(profileID("live.quarantine-1785345247"))+ if err != nil {+ t.Fatalf("resolve quarantined id: %v", err)+ }+ if quarantined != "live.quarantine-1785345247" {+ t.Fatalf("quarantined ID resolved to %q, want its own directory name", quarantined)+ }+}++// Rename writes the new name into metadata before renaming the directory. If+// the rename fails it must put the old name back, or the directory is left+// claiming a profile it is not — which is the state this fix stops trusting.+func TestProfileRenameRollsBackMetadataWhenTheDirectoryRenameFails(t *testing.T) {+ base := t.TempDir()+ pm := NewProfileManager(base)+ if err := pm.Create("old-name"); err != nil {+ t.Fatal(err)+ }+ oldDir := filepath.Join(base, profileID("old-name"))++ // The destination must look free to the preflight and still be unwritable+ // at rename time, or Rename returns before it ever touches the metadata.+ if err := os.Chmod(base, 0500); err != nil {+ t.Fatalf("chmod base: %v", err)+ }+ t.Cleanup(func() { _ = os.Chmod(base, 0755) })++ if err := pm.Rename("old-name", "new-name"); err == nil {+ t.Fatal("expected the rename to fail while the destination is occupied")+ }++ meta := readProfileMeta(oldDir)+ if meta.Name != "old-name" || meta.ID != profileID("old-name") {+ t.Fatalf("metadata not rolled back: name=%q id=%q", meta.Name, meta.ID)+ }+ if !pm.Exists("old-name") {+ t.Fatal("the profile must still resolve under its old name")+ }+ got := listedByPath(t, pm, profileID("old-name"))+ if got.Name != "old-name" || got.ID != profileID("old-name") {+ t.Fatalf("listing after a failed rename: name=%q id=%q", got.Name, got.ID)+ }+}