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
print a warning if any required sensitive data keys are not defined
Nick Sweeting28906fd5d68c
diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yamlindex 71d369e57..cbc75c6ea 100644--- a/.github/workflows/test.yaml+++ b/.github/workflows/test.yaml@@ -40,6 +40,7 @@ jobs:- functionality/hooks- test_controller- test_tab_management+ - test_sensitive_datasteps:- uses: actions/checkout@v4- uses: astral-sh/setup-uv@v6diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.pyindex c54669e9a..5307c39d8 100644--- a/browser_use/agent/message_manager/service.py+++ b/browser_use/agent/message_manager/service.py@@ -62,7 +62,7 @@ class MessageManager:if self.settings.sensitive_data:info = f'Here are placeholders for sensitive data: {list(self.settings.sensitive_data.keys())}'- info += 'To use them, write <secret>the placeholder name</secret>'+ info += '\nTo use them, write <secret>the placeholder name</secret>'info_message = HumanMessage(content=info)self._add_message_with_tokens(info_message, message_type='init')@@ -216,10 +216,19 @@ class MessageManager:def replace_sensitive(value: str) -> str:if not self.settings.sensitive_data:return value- for key, val in self.settings.sensitive_data.items():- if not val:- continue++ # Create a dictionary with all key-value pairs from sensitive_data where value is not None or empty+ valid_sensitive_data = {k: v for k, v in self.settings.sensitive_data.items() if v}++ # If there are no valid sensitive data entries, just return the original value+ if not valid_sensitive_data:+ logger.warning('No valid entries found in sensitive_data dictionary')+ return value++ # Replace all valid sensitive data values with their placeholder tags+ for key, val in valid_sensitive_data.items():value = value.replace(val, f'<secret>{key}</secret>')+return valueif isinstance(message.content, str):diff --git a/browser_use/controller/registry/service.py b/browser_use/controller/registry/service.pyindex 4311f7004..5f86642f2 100644--- a/browser_use/controller/registry/service.py+++ b/browser_use/controller/registry/service.py@@ -154,16 +154,27 @@ class Registry(Generic[Context]):"""Replaces the sensitive data in the params"""# if there are any str with <secret>placeholder</secret> in the params, replace them with the actual value from sensitive_data+ import loggingimport re+ logger = logging.getLogger(__name__)secret_pattern = re.compile(r'<secret>(.*?)</secret>')+ # Set to track all missing placeholders across the full object+ all_missing_placeholders = set()+def replace_secrets(value):if isinstance(value, str):matches = secret_pattern.findall(value)+for placeholder in matches:- if placeholder in sensitive_data:+ if placeholder in sensitive_data and sensitive_data[placeholder]:value = value.replace(f'<secret>{placeholder}</secret>', sensitive_data[placeholder])+ else:+ # Keep track of missing placeholders+ all_missing_placeholders.add(placeholder)+ # Don't replace the tag, keep it as is+return valueelif isinstance(value, dict):return {k: replace_secrets(v) for k, v in value.items()}@@ -173,6 +184,11 @@ class Registry(Generic[Context]):params_dump = params.model_dump()processed_params = replace_secrets(params_dump)++ # Log a warning if any placeholders are missing+ if all_missing_placeholders:+ logger.warning(f'Missing or empty keys in sensitive_data dictionary: {", ".join(all_missing_placeholders)}')+return type(params).model_validate(processed_params)# @time_execution_sync('--create_action_model')diff --git a/docs/customize/sensitive-data.mdx b/docs/customize/sensitive-data.mdxindex 3bb5f6c66..92c86509e 100644--- a/docs/customize/sensitive-data.mdx+++ b/docs/customize/sensitive-data.mdx@@ -45,6 +45,14 @@ In this example:2. When the model wants to use your password it outputs x_password - and we replace it with the actual value.3. When your password is visible on the current page, we replace it in the LLM input - so that the model never has it in its state.+### Missing or Empty Values++When working with sensitive data, keep these details in mind:++- If a key referenced by the model (`<secret>key_name</secret>`) is missing from your `sensitive_data` dictionary, a warning will be logged but the substitution tag will be preserved.+- If you provide an empty value for a key in the `sensitive_data` dictionary, it will be treated the same as a missing key.+- The system will always attempt to process all valid substitutions, even if some keys are missing or empty.+Warning: Vision models still see the image of the page - where the sensitive data might be visible.This approach ensures that sensitive information remains secure while still allowing the agent to perform tasks that require authentication.