Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
fix(tools): only rewrite upload path to FileSystem on local sessions
Saurav Panda65a377c20fa5
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.pyindex 7d67672de..cf999b849 100644--- a/browser_use/tools/service.py+++ b/browser_use/tools/service.py@@ -845,8 +845,13 @@ class Tools(Generic[Context]):# Also check if it's a recently downloaded file that might not be in available_file_paths yetdownloaded_files = browser_session.downloaded_filesif params.path not in downloaded_files:- # Finally, check if it's a file in the FileSystem service- if file_system and file_system.get_dir():+ # Finally, check if it's a file in the FileSystem service.+ # Only rewrite to the local FileSystem path on local sessions —+ # on remote sessions, params.path is meant to address a file on+ # the remote machine, and a coincidental basename collision with+ # a local managed file (e.g. `/tmp/note.md` colliding with a+ # local `note.md`) must not silently upload the local file.+ if browser_session.is_local and file_system and file_system.get_dir():# Check if the file is actually managed by the FileSystem service# The path should be just the filename for FileSystem filesfile_obj = file_system.get_file(params.path)@@ -868,13 +873,9 @@ class Tools(Generic[Context]):return ActionResult(error=msg)params = UploadFileAction(index=params.index, path=file_system_path)else:- # If browser is remote, allow passing a remote-accessible absolute path- if not browser_session.is_local:- pass- else:- msg = f'File path {params.path} is not available. To fix: The user must add this file path to the available_file_paths parameter when creating the Agent. Example: Agent(task="...", llm=llm, browser=browser, available_file_paths=["{params.path}"])'- logger.error(f'❌ {msg}')- return ActionResult(error=msg)+ msg = f'File path {params.path} is not available. To fix: The user must add this file path to the available_file_paths parameter when creating the Agent. Example: Agent(task="...", llm=llm, browser=browser, available_file_paths=["{params.path}"])'+ logger.error(f'❌ {msg}')+ return ActionResult(error=msg)else:# If browser is remote, allow passing a remote-accessible absolute pathif not browser_session.is_local:diff --git a/tests/ci/security/test_upload_file_containment.py b/tests/ci/security/test_upload_file_containment.pyindex 4d39d8541..c1646ce14 100644--- a/tests/ci/security/test_upload_file_containment.py+++ b/tests/ci/security/test_upload_file_containment.py@@ -133,3 +133,64 @@ async def test_traversal_with_no_basename_match_still_fails_safely(assert result.error is not None# Should be the "not available" rejection, not a path-resolution success.assert 'not available' in result.error.lower() or 'does not exist' in result.error.lower()+++class _StubRemoteBrowserSession(_StubBrowserSession):+ """Stub for a remote (non-local) browser session — the upload action's+ rules differ here. Remote paths are passed through to the browser process."""++ is_local = False+++async def test_remote_session_does_not_rewrite_to_local_filesystem_on_basename_collision(+ tmp_path,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """For remote sessions, an absolute remote path that happens to share a+ basename with a local FileSystem-managed file MUST be passed through+ unchanged. Rewriting to the local FileSystem path would silently upload+ the wrong file (the local managed one) instead of the remote-machine file+ the agent referenced.++ Pre-existing issue surfaced by the GHSA-j9hj-92j8-jv9h review (codex bot).+ """+ fs = FileSystem(base_dir=tmp_path)+ # Create a local managed file with a basename that collides with the+ # remote path the agent will reference.+ await fs.write_file('note.md', 'LOCAL content the agent did NOT ask for')++ # What the remote-session agent would pass — a path valid on the remote+ # machine, NOT in the local FileSystem.+ remote_path = '/tmp/note.md'++ # Capture every path that the action checks for existence. For remote+ # sessions the local-existence check is skipped, so we instead capture+ # the resolved path via the same os.path.exists hook used by the local+ # test — if the rewrite-to-local-filesystem branch fires, the next call+ # will be against the data_dir path; if not, no call should reference it.+ exists_calls: list[str] = []+ real_exists = os.path.exists++ def capturing_exists(path: str) -> bool:+ exists_calls.append(str(path))+ return real_exists(path)++ monkeypatch.setattr('browser_use.tools.service.os.path.exists', capturing_exists)++ tools = Tools()+ await tools.registry.execute_action(+ 'upload_file',+ {'index': 0, 'path': remote_path},+ browser_session=_StubRemoteBrowserSession(), # type: ignore[arg-type]+ file_system=fs,+ available_file_paths=[],+ )++ # The action should NOT have rewritten to a local FileSystem path. If it+ # had, we'd see the data_dir/note.md path appear in the captured calls.+ data_dir_str = str(fs.get_dir())+ rewrites = [p for p in exists_calls if p.startswith(data_dir_str)]+ assert not rewrites, (+ f'Remote session upload silently rewrote to local FileSystem path: {rewrites}. '+ f'The agent intended to upload {remote_path!r}; the rewrite would have uploaded the local managed file instead.'+ )