-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(safari): multi-profile support #581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| package safari | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| _ "modernc.org/sqlite" | ||
|
|
||
| "github.com/moond4rk/hackbrowserdata/log" | ||
| ) | ||
|
|
||
| // profileContext tracks the uppercase (Safari/Profiles/<UUID>) and lowercase | ||
| // (WebKit/WebsiteDataStore/<uuid>) UUID forms a named profile needs. Both empty ⇒ default profile. | ||
| type profileContext struct { | ||
| name string | ||
| uuidUpper string | ||
| uuidLower string | ||
| legacyHome string // ~/Library/Safari | ||
| container string // ~/Library/Containers/com.apple.Safari/Data/Library | ||
| } | ||
|
|
||
| func (p profileContext) isDefault() bool { return p.uuidUpper == "" } | ||
|
|
||
| // downloadOwnerUUID is the value Safari writes into DownloadEntryProfileUUIDStringKey | ||
| // for downloads that belong to this profile. The default profile uses the sentinel | ||
| // "DefaultProfile"; named profiles use their uppercase UUID. | ||
| func (p profileContext) downloadOwnerUUID() string { | ||
| if p.isDefault() { | ||
| return defaultProfileSentinel | ||
| } | ||
| return p.uuidUpper | ||
| } | ||
|
|
||
| // SafariTabs.db lists profiles in bookmarks rows with subtype=2. external_uuid "DefaultProfile" | ||
| // is the sentinel for the implicit default, which has no per-UUID directory. | ||
| const ( | ||
| safariTabsDBRelPath = "Safari/SafariTabs.db" | ||
| safariProfileSubtype = 2 | ||
| defaultProfileSentinel = "DefaultProfile" | ||
| ) | ||
|
|
||
| // Path-unsafe bytes for filenames/CSV values; Unicode letters (CJK etc.) survive. | ||
| var unsafeNameChars = regexp.MustCompile(`[/\\:*?"<>|\x00-\x1f]+`) | ||
|
|
||
| // Canonical 8-4-4-4-12 hex UUID — format check only, no semantic parse. | ||
| var uuidPattern = regexp.MustCompile(`^[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}$`) | ||
|
|
||
| // discoverSafariProfiles always lists the default first, then named profiles from SafariTabs.db | ||
| // (authoritative) with a ReadDir fallback only if the DB itself is unreadable. | ||
| func discoverSafariProfiles(legacyHome string) []profileContext { | ||
| container := deriveContainerRoot(legacyHome) | ||
|
|
||
| profiles := []profileContext{{ | ||
| name: "default", | ||
| legacyHome: legacyHome, | ||
| container: container, | ||
| }} | ||
|
|
||
| named, err := readNamedProfilesFromDB(container) | ||
| if err != nil { | ||
| // Empty DB (nil, nil) is authoritative; fall back only when DB itself is unreadable. | ||
| named = readNamedProfilesFromDir(container) | ||
| } | ||
| for _, p := range named { | ||
| p.legacyHome = legacyHome | ||
| p.container = container | ||
| profiles = append(profiles, p) | ||
| } | ||
|
|
||
| disambiguateNames(profiles) | ||
| return profiles | ||
| } | ||
|
|
||
| func deriveContainerRoot(legacyHome string) string { | ||
| return filepath.Join(filepath.Dir(legacyHome), "Containers", "com.apple.Safari", "Data", "Library") | ||
| } | ||
|
|
||
| // readNamedProfilesFromDB returns (nil, err) when the DB is missing/unreadable so the caller can | ||
| // try the ReadDir fallback; (slice, nil) — possibly empty — is authoritative. | ||
| func readNamedProfilesFromDB(container string) ([]profileContext, error) { | ||
| // Read-only + immutable so we don't disturb Safari's live WAL. | ||
| dsn := "file:" + filepath.Join(container, safariTabsDBRelPath) + "?mode=ro&immutable=1" | ||
| db, err := sql.Open("sqlite", dsn) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("open SafariTabs.db: %w", err) | ||
| } | ||
| defer db.Close() | ||
|
|
||
| // Ping forces connection; sql.Open is lazy and won't detect a missing file. | ||
| if err := db.Ping(); err != nil { | ||
| return nil, fmt.Errorf("ping SafariTabs.db: %w", err) | ||
| } | ||
|
|
||
| rows, err := db.Query( | ||
| `SELECT external_uuid, title FROM bookmarks WHERE subtype = ? AND external_uuid != ?`, | ||
| safariProfileSubtype, defaultProfileSentinel, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("query SafariTabs.db: %w", err) | ||
| } | ||
| defer rows.Close() | ||
|
|
||
| var out []profileContext | ||
| for rows.Next() { | ||
| var externalUUID, title sql.NullString | ||
| if err := rows.Scan(&externalUUID, &title); err != nil { | ||
| log.Debugf("safari profiles: scan row: %v", err) | ||
| continue | ||
| } | ||
| if !isCanonicalUUID(externalUUID.String) { | ||
| continue | ||
| } | ||
| out = append(out, newNamedProfile(externalUUID.String, title.String)) | ||
| } | ||
| return out, nil | ||
| } | ||
|
|
||
| // readNamedProfilesFromDir is the fallback for missing SafariTabs.db. Names are synthesized from UUIDs. | ||
| func readNamedProfilesFromDir(container string) []profileContext { | ||
| entries, err := os.ReadDir(filepath.Join(container, "Safari", "Profiles")) | ||
| if err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| var out []profileContext | ||
| for _, e := range entries { | ||
| if !e.IsDir() || !isCanonicalUUID(e.Name()) { | ||
| continue | ||
| } | ||
| out = append(out, newNamedProfile(e.Name(), "")) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func newNamedProfile(upperUUID, title string) profileContext { | ||
| return profileContext{ | ||
| name: resolveProfileName(title, upperUUID), | ||
| uuidUpper: upperUUID, | ||
| uuidLower: strings.ToLower(upperUUID), | ||
| } | ||
| } | ||
|
|
||
| func isCanonicalUUID(s string) bool { return uuidPattern.MatchString(s) } | ||
|
|
||
| // resolveProfileName prefers the SafariTabs.db title, falling back to "profile-<uuid[:8]>". | ||
| func resolveProfileName(title, upperUUID string) string { | ||
| if name := sanitizeProfileName(title); name != "" { | ||
| return name | ||
| } | ||
| return "profile-" + strings.ToLower(upperUUID[:8]) | ||
| } | ||
|
|
||
| func sanitizeProfileName(name string) string { | ||
| name = strings.TrimSpace(name) | ||
| if name == "" { | ||
| return "" | ||
| } | ||
| return unsafeNameChars.ReplaceAllString(name, "_") | ||
| } | ||
|
|
||
| // disambiguateNames appends "-2", "-3", … to duplicate names, in place. | ||
| func disambiguateNames(profiles []profileContext) { | ||
| occurrences := make(map[string]int, len(profiles)) | ||
| for i := range profiles { | ||
| original := profiles[i].name | ||
| if prior := occurrences[original]; prior > 0 { | ||
| profiles[i].name = fmt.Sprintf("%s-%d", original, prior+1) | ||
| } | ||
| occurrences[original]++ | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readNamedProfilesFromDB iterates rows but never checks rows.Err() before returning. That can silently drop query/IO errors mid-iteration and treat the DB as authoritative with partial results. Please add a rows.Err() check (and return an error) before returning out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — fixed in 5d5ec1c. Added
rows.Err()check after the iteration so mid-iteration IO errors no longer get swallowed as an authoritative empty result.