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
added normalize the filename
Saurav Pandaa7c2144cd1ca
diff --git a/browser_use/filesystem/file_system.py b/browser_use/filesystem/file_system.pyindex 361ac466e..23a3552db 100644--- a/browser_use/filesystem/file_system.py+++ b/browser_use/filesystem/file_system.py@@ -393,18 +393,23 @@ class FileSystem:def _resolve_filename(self, file_name: str) -> tuple[str, bool]:"""Resolve a filename, attempting sanitization if the original is invalid.+ Normalizes to basename first to prevent directory traversal (e.g. ../secret.md).+Returns:- (resolved_name, was_sanitized): The resolved filename and whether sanitization was applied.- If resolution fails, returns (original_name, False).+ (resolved_name, was_changed): The resolved filename and whether it differs from the input.+ If resolution fails, returns (basename, was_changed)."""- if self._is_valid_filename(file_name):- return file_name, False+ base_name = os.path.basename(file_name)+ was_changed = base_name != file_name++ if self._is_valid_filename(base_name):+ return base_name, was_changed- sanitized = self.sanitize_filename(file_name)- if sanitized != os.path.basename(file_name) and self._is_valid_filename(sanitized):+ sanitized = self.sanitize_filename(base_name)+ if sanitized != base_name and self._is_valid_filename(sanitized):return sanitized, True- return file_name, False+ return base_name, was_changeddef _parse_filename(self, filename: str) -> tuple[str, str]:"""Parse filename into name and extension. Always check _is_valid_filename first."""@@ -532,10 +537,7 @@ class FileSystem:file_obj = self.files.get(resolved)if not file_obj:if was_sanitized:- result['message'] = (- f"File '{resolved}' not found. "- f"(Filename was auto-corrected from '{full_filename}')"- )+ result['message'] = f"File '{resolved}' not found. (Filename was auto-corrected from '{full_filename}')"else:result['message'] = f"File '{full_filename}' not found."return resultdiff --git a/tests/ci/infrastructure/test_filesystem.py b/tests/ci/infrastructure/test_filesystem.pyindex 5b4e2d418..55ea7e685 100644--- a/tests/ci/infrastructure/test_filesystem.py+++ b/tests/ci/infrastructure/test_filesystem.py@@ -1163,6 +1163,44 @@ class TestFilenameSanitization:fs.nuke()+ async def test_path_traversal_prevented(self):+ """Test that directory traversal in filenames is stripped to basename."""+ with tempfile.TemporaryDirectory() as tmp_dir:+ fs = FileSystem(base_dir=tmp_dir, create_default_files=False)++ # Write with path traversal - should strip to basename 'secret.md'+ result = await fs.write_file('../secret.md', 'traversal attempt')+ assert 'successfully' in result+ assert 'secret.md' in result++ # File should be stored under basename only, inside data_dir+ assert 'secret.md' in fs.files+ file_on_disk = fs.data_dir / 'secret.md'+ assert file_on_disk.exists()++ # Parent directory should NOT have the file+ escaped_path = fs.data_dir.parent / 'secret.md'+ assert not escaped_path.exists()++ # Nested traversal also stripped+ result = await fs.write_file('../../etc/passwd.txt', 'nope')+ assert 'successfully' in result+ assert 'passwd.txt' in result+ assert (fs.data_dir / 'passwd.txt').exists()++ # Absolute paths stripped to basename+ result = await fs.write_file('/tmp/evil.md', 'nope')+ assert 'successfully' in result+ assert 'evil.md' in result+ assert (fs.data_dir / 'evil.md').exists()++ # resolve_filename returns basename, not the traversal path+ resolved, was_changed = fs._resolve_filename('../secret.md')+ assert resolved == 'secret.md'+ assert was_changed is True++ fs.nuke()+class TestFileSystemIntegration:"""Integration tests for FileSystem with real file operations."""