Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix: add file validation for uploads and dynamic text extension list

Magnus Müllere072135590c0
diff --git a/browser_use/browser/watchdogs/default_action_watchdog.py b/browser_use/browser/watchdogs/default_action_watchdog.py
index 10c676f6c..b3812a3a4 100644
--- a/browser_use/browser/watchdogs/default_action_watchdog.py
+++ b/browser_use/browser/watchdogs/default_action_watchdog.py
@@ -2,6 +2,7 @@
import asyncio
import json
+import os
from cdp_use.cdp.input.commands import DispatchKeyEventParameters
@@ -2269,6 +2270,14 @@ class DefaultActionWatchdog(BaseWatchdog):
cdp_client = self.browser_session.cdp_client
session_id = await self._get_session_id_for_element(element_node)
+ # Validate file before upload
+ if os.path.exists(event.file_path):
+ file_size = os.path.getsize(event.file_path)
+ if file_size == 0:
+ msg = f'Upload failed - file {event.file_path} is empty (0 bytes).'
+ raise BrowserError(message=msg, long_term_memory=msg)
+ self.logger.debug(f'📎 File {event.file_path} validated ({file_size} bytes)')
+
# Set file(s) to upload
backend_node_id = element_node.backend_node_id
await cdp_client.send.DOM.setFileInputFiles(
diff --git a/browser_use/filesystem/file_system.py b/browser_use/filesystem/file_system.py
index da1cb9478..a2da20afa 100644
--- a/browser_use/filesystem/file_system.py
+++ b/browser_use/filesystem/file_system.py
@@ -328,7 +328,11 @@ class FileSystem:
)
return result
- if extension in ['md', 'txt', 'json', 'jsonl', 'csv']:
+ # Text-based extensions: derive from _file_types, excluding those with special readers
+ _special_extensions = {'docx', 'pdf', 'jpg', 'jpeg', 'png'}
+ text_extensions = [ext for ext in self._file_types if ext not in _special_extensions]
+
+ if extension in text_extensions:
import anyio
async with await anyio.open_file(full_filename, 'r') as f:
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index bcd2d9fbc..7abfae684 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -469,11 +469,15 @@ class Tools(Generic[Context]):
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}"])'
raise BrowserError(message=msg, long_term_memory=msg)
- # For local browsers, ensure the file exists on the local filesystem
+ # For local browsers, ensure the file exists and has content
if browser_session.is_local:
if not os.path.exists(params.path):
msg = f'File {params.path} does not exist'
return ActionResult(error=msg)
+ file_size = os.path.getsize(params.path)
+ if file_size == 0:
+ msg = f'File {params.path} is empty (0 bytes). The file may not have been saved correctly.'
+ return ActionResult(error=msg)
# Get the selector map to find the node
selector_map = await browser_session.get_selector_map()