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(agent): redact sensitive data nested below list boundaries in saved history
Mohil-Ahuja63d13371fb46
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.pyindex 4dde414b3..8c035b2e3 100644--- a/browser_use/agent/views.py+++ b/browser_use/agent/views.py@@ -521,6 +521,18 @@ class AgentHistory(BaseModel):return redact_sensitive_string(value, sensitive_values)+ def _filter_sensitive_data_from_value(self, value: Any, sensitive_data: dict[str, str | dict[str, str]] | None) -> Any:+ """Recursively filter sensitive data from any supported container or string value"""+ if isinstance(value, str):+ return self._filter_sensitive_data_from_string(value, sensitive_data)+ if isinstance(value, dict):+ return {key: self._filter_sensitive_data_from_value(item, sensitive_data) for key, item in value.items()}+ if isinstance(value, list):+ return [self._filter_sensitive_data_from_value(item, sensitive_data) for item in value]+ if isinstance(value, tuple):+ return tuple(self._filter_sensitive_data_from_value(item, sensitive_data) for item in value)+ return value+def _filter_sensitive_data_from_dict(self, data: dict[str, Any], sensitive_data: dict[str, str | dict[str, str]] | None) -> dict[str, Any]:@@ -528,24 +540,7 @@ class AgentHistory(BaseModel):if not sensitive_data:return data- filtered_data = {}- for key, value in data.items():- if isinstance(value, str):- filtered_data[key] = self._filter_sensitive_data_from_string(value, sensitive_data)- elif isinstance(value, dict):- filtered_data[key] = self._filter_sensitive_data_from_dict(value, sensitive_data)- elif isinstance(value, list):- filtered_data[key] = [- self._filter_sensitive_data_from_string(item, sensitive_data)- if isinstance(item, str)- else self._filter_sensitive_data_from_dict(item, sensitive_data)- if isinstance(item, dict)- else item- for item in value- ]- else:- filtered_data[key] = value- return filtered_data+ return {key: self._filter_sensitive_data_from_value(value, sensitive_data) for key, value in data.items()}def model_dump(self, sensitive_data: dict[str, str | dict[str, str]] | None = None, **kwargs) -> dict[str, Any]:"""Custom serialization handling circular references and filtering sensitive data"""diff --git a/tests/ci/security/test_sensitive_data.py b/tests/ci/security/test_sensitive_data.pyindex 6160653d7..145505adc 100644--- a/tests/ci/security/test_sensitive_data.py+++ b/tests/ci/security/test_sensitive_data.py@@ -623,3 +623,47 @@ def test_password_field_without_type_attribute():attrs_str = DOMTreeSerializer._build_attributes_string(node, list(DEFAULT_INCLUDE_ATTRIBUTES), '')assert value in attrs_str, 'Input without type attribute should preserve its value'+++def test_history_filters_sensitive_data_inside_nested_lists(tmp_path):+ """+ Saved history must not leak sensitive values that sit below a nested-list+ boundary in an action parameter (e.g. a list of rows, each row a list).+ """+ from typing import Any++ from pydantic import create_model++ from browser_use.agent.views import AgentHistory, AgentHistoryList, AgentOutput+ from browser_use.browser.views import BrowserStateHistory+ from browser_use.tools.registry.views import ActionModel++ class NestedInputAction(BaseModel):+ rows: list[list[str]]+ lookup: dict[str, list[dict[str, str]]]++ InputActionModel = create_model('InputActionModel', __base__=ActionModel, input=(NestedInputAction | None, None))+ OutputModel = AgentOutput.type_with_custom_actions(InputActionModel)++ action = InputActionModel(+ input=NestedInputAction(+ rows=[['token-123']],+ lookup={'headers': [{'authorization': 'token-123'}]},+ )+ )+ history = AgentHistoryList[Any](+ history=[+ AgentHistory(+ model_output=OutputModel(memory='', action=[action]),+ result=[],+ state=BrowserStateHistory(url='https://example.test', title='t', tabs=[], interacted_element=[None]),+ )+ ]+ )++ filepath = tmp_path / 'history.json'+ history.save_to_file(filepath, sensitive_data={'api_key': 'token-123'})+ saved = filepath.read_text(encoding='utf-8')++ assert 'token-123' not in saved, 'Sensitive value leaked into the saved history file'+ assert saved.count('<secret>api_key</secret>') == 2