Repository navigation
fix(browser): keep browser-use-managed profiles off the OS keychain - #6059
FanouZeng-TT wants to merge 1 commit into
Conversation
Chromium synchronously reaches the OS credential store (macOS Keychain, Linux Secret Service) while initialising a profile. On a browser-use-managed throwaway profile there is nothing to decrypt and, under headless/CI/ containers, no interactive session to answer the prompt -- Chromium hangs at startup and never opens its CDP port, and the caller sees an opaque TimeoutError. Reportedly also a surprise Keychain prompt on a local machine. browser-use#3225 removed --use-mock-keychain/--password-store=basic globally because a real user profile passed via user_data_dir needs the real keychain to decrypt saved cookies and passwords. That fix is preserved here: the flags are applied only when the profile directory is one browser-use owns (the temp dir made for user_data_dir=None, any browser-use temp prefix, or the launch watchdog's temp-dir fallback), and never to a copy of a caller-supplied Chrome profile. Managed-ness is evaluated on each get_args() call rather than captured once at construction, so a profile whose user_data_dir changes later -- the watchdog retrying in a browseruse-tmp-* directory, or a caller reassigning the field -- is judged by the directory it actually launches with. Fixes browser-use#5415.
There was a problem hiding this comment.
3 issues found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="browser_use/browser/profile.py">
<violation number="1" location="browser_use/browser/profile.py:877">
P2: The copied-profile marker overrides the live directory check, so a copied profile's `browseruse-tmp-*` watchdog fallback still uses the OS keychain. Check the fallback directory before honoring the copied-profile marker.</violation>
<violation number="2" location="browser_use/browser/profile.py:882">
P2: This substring check matches any path component, so a caller-owned Chrome profile beneath a directory named `browser-use-user-data-dir-...` gets mock-keychain flags and may no longer decrypt saved credentials. Match the generated prefix on the user-data directory's basename instead.</violation>
<violation number="3" location="browser_use/browser/profile.py:945">
P1: Passing a copied Chrome profile through `BrowserSession` loses this private provenance marker during `model_dump()`/reconstruction, so `get_args()` applies mock-keychain flags to the copied credentials. Preserve the copied-profile provenance when merging the session profile, or avoid rebuilding it.</violation>
</file>
Click Fix with cubic on a comment, or reply @cubic-dev-ai fix this, and cubic writes the fix.
View guided diff | Re-trigger cubic
| logger.info(f'Created new profile ({self.profile_directory}) in temp directory: {temp_dir}') | ||
|
|
||
| self.user_data_dir = temp_dir | ||
| self._copied_from_caller_profile = True |
There was a problem hiding this comment.
P1: Passing a copied Chrome profile through BrowserSession loses this private provenance marker during model_dump()/reconstruction, so get_args() applies mock-keychain flags to the copied credentials. Preserve the copied-profile provenance when merging the session profile, or avoid rebuilding it.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At browser_use/browser/profile.py, line 945:
<comment>Passing a copied Chrome profile through `BrowserSession` loses this private provenance marker during `model_dump()`/reconstruction, so `get_args()` applies mock-keychain flags to the copied credentials. Preserve the copied-profile provenance when merging the session profile, or avoid rebuilding it.</comment>
<file context>
@@ -902,6 +942,7 @@ def _copy_profile(self) -> None:
logger.info(f'Created new profile ({self.profile_directory}) in temp directory: {temp_dir}')
self.user_data_dir = temp_dir
+ self._copied_from_caller_profile = True
def get_args(self) -> list[str]:
</file context>
| if self.user_data_dir is None: | ||
| return True | ||
| user_data_str = str(self.user_data_dir).lower() | ||
| return 'browser-use-user-data-dir-' in user_data_str or 'browseruse-tmp-' in user_data_str |
There was a problem hiding this comment.
P2: This substring check matches any path component, so a caller-owned Chrome profile beneath a directory named browser-use-user-data-dir-... gets mock-keychain flags and may no longer decrypt saved credentials. Match the generated prefix on the user-data directory's basename instead.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At browser_use/browser/profile.py, line 882:
<comment>This substring check matches any path component, so a caller-owned Chrome profile beneath a directory named `browser-use-user-data-dir-...` gets mock-keychain flags and may no longer decrypt saved credentials. Match the generated prefix on the user-data directory's basename instead.</comment>
<file context>
@@ -841,6 +860,27 @@ def validate_highlight_elements_conflict(self) -> Self:
+ if self.user_data_dir is None:
+ return True
+ user_data_str = str(self.user_data_dir).lower()
+ return 'browser-use-user-data-dir-' in user_data_str or 'browseruse-tmp-' in user_data_str
+
def model_post_init(self, __context: Any) -> None:
</file context>
| return 'browser-use-user-data-dir-' in user_data_str or 'browseruse-tmp-' in user_data_str | |
| user_data_name = Path(user_data_str).name | |
| return user_data_name.startswith('browser-use-user-data-dir-') or user_data_name.startswith('browseruse-tmp-') |
| caller reassigning the field -- is judged by the directory it actually launches | ||
| with. | ||
| """ | ||
| if self._copied_from_caller_profile: |
There was a problem hiding this comment.
P2: The copied-profile marker overrides the live directory check, so a copied profile's browseruse-tmp-* watchdog fallback still uses the OS keychain. Check the fallback directory before honoring the copied-profile marker.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At browser_use/browser/profile.py, line 877:
<comment>The copied-profile marker overrides the live directory check, so a copied profile's `browseruse-tmp-*` watchdog fallback still uses the OS keychain. Check the fallback directory before honoring the copied-profile marker.</comment>
<file context>
@@ -841,6 +860,27 @@ def validate_highlight_elements_conflict(self) -> Self:
+ caller reassigning the field -- is judged by the directory it actually launches
+ with.
+ """
+ if self._copied_from_caller_profile:
+ return False
+ if self.user_data_dir is None:
</file context>
| if self._copied_from_caller_profile: | |
| if self._copied_from_caller_profile and 'browseruse-tmp-' not in str(self.user_data_dir).lower(): |
Summary
Fixes #5415.
Chromium synchronously reaches the OS credential store (macOS Keychain, Linux Secret Service) while it initialises a profile. For a browser-use-managed throwaway profile there is nothing to decrypt and, under headless/CI/containers, no interactive session to answer the prompt — so Chromium hangs at startup and never opens its CDP port.
LocalBrowserWatchdog.on_BrowserLaunchEventthen never returns and the caller sees an opaqueTimeoutError. On an interactive machine this instead surfaces as a Keychain prompt on every fresh profile launch, including the firstuv run pytest tests/ciafter a fresh clone.#3225removed--use-mock-keychainand--password-store=basicfrom the global defaults because a real, pre-existing Chrome profile passed viauser_data_dirgenuinely needs the real keychain to decrypt saved cookies and passwords. That fix is still correct for that scenario and is preserved here.Fix
Apply the two flags only when the profile directory is one browser-use owns:
user_data_dir=None;browseruse-tmp-*fallback directory.They are never applied to a copy of a caller-supplied Chrome profile:
_copy_profile()copies such a profile into a temp dir before launch, but that copy still holds the caller's saved credentials, so it must keep real keychain access (#3225).Managed-ness is evaluated on every
get_args()call rather than captured once at construction. This matters becauseuser_data_dircan change after init — most notablyLocalBrowserWatchdog._launch_browser, which retries a failed launch in a freshbrowseruse-tmp-*directory — and the flags must track the directory the browser actually launches with. A profile whoseuser_data_diris reassigned to a real Chrome profile after construction likewise drops the flags.Bug verification
Before the fix, a managed profile's
get_args()contains neither flag. After the fix the same profile includes them, and each caller-supplied / copied / reassigned-real-profile case still excludes them. The new test file exercises both directions:test_default_profile_gets_mock_keychain— fails before, passes after;test_explicit_user_profile_keeps_real_keychain,test_copied_user_profile_is_not_treated_as_managed,test_default_config_profile_dir_is_not_managed— guard the#3225behavior;test_managed_profile_keeps_mock_keychain_after_temp_fallback,test_reassigned_real_profile_dir_drops_mock_keychain— coveruser_data_dirchanging after construction.Independently, launching the exact Chromium binary browser-use uses with otherwise identical argument lists showed the flip: without the two flags the CDP port never opens (no
curl http://127.0.0.1:<port>/json/versionreply even after 8s); with them it responds within ~1s.Validation
uv run pytest -q tests/ci/test_managed_profile_keychain_args.py— 6 passeduv run pytest -q tests/ci --ignore=tests/ci/browser --ignore=tests/ci/interactions --ignore=tests/ci/integrations --ignore=tests/ci/infrastructure— 342 passed, 12 skipped, 1 pre-existing failure unrelated to this change (test_beta_agent_constructor_type_hints_match_browser_use_core_params; the same assertion fails on unmodifiedmain, a module-import-ordering issue)uv run ruff check,uv run ruff format --check— cleanuv run pyright browser_use/browser/profile.py tests/ci/test_managed_profile_keychain_args.py— 0 errorsuv run pre-commit run --files …— all hooks passedRelation to earlier attempts
Supersedes the earlier attempts at this same fix (
#5446,#5430,#5417), which were closed by the stale bot. The difference here is the per-launch evaluation of managed-ness described above, so the watchdog's temp-dir fallback and any post-constructionuser_data_dirreassignment are handled rather than evaluated once at init.Summary by cubic
Fixes #5415: Chromium hangs at startup under headless/CI/containers when launching a browser-use-managed throwaway profile, because Chromium blocks on the OS credential store (macOS Keychain, Linux Secret Service) before opening its CDP port. On interactive machines this surfaced as a Keychain prompt on every fresh profile launch.
--use-mock-keychainand--password-store=basic, removed globally by #3225, are now applied only when browser-use owns the profile directory — the temp dir created foruser_data_dir=None, any dir still carrying a browser-use temp prefix, and the launch watchdog's fallback dir. Caller-supplied profiles, including copies made by_copy_profile(), keep real keychain access so saved cookies and passwords can still be decrypted. Managed-ness is evaluated on everyget_args()call rather than captured at construction, so the flags trackuser_data_dirreassignments after init, such as the watchdog retrying in a fresh temp dir.Written for commit dfb104c. Summary will update on new commits.