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
zach/chore: strip password field values from DOM snapshots sent to LLM
Ahmed Alye8d1681cd5de
diff --git a/browser_use/dom/serializer/serializer.py b/browser_use/dom/serializer/serializer.pyindex 5f70fa57c..f5a191977 100644--- a/browser_use/dom/serializer/serializer.py+++ b/browser_use/dom/serializer/serializer.py@@ -1175,11 +1175,24 @@ class DOMTreeSerializer:attributes_to_include['placeholder'] = 'mm/dd/yyyy'attributes_to_include['format'] = 'mm/dd/yyyy'+ # Never include values from password fields - they contain secrets that must not+ # leak into DOM snapshots sent to the LLM, where prompt injection could exfiltrate them.+ is_password_field = (+ node.tag_name+ and node.tag_name.lower() == 'input'+ and node.attributes+ and node.attributes.get('type', '').lower() == 'password'+ )+# Include accessibility propertiesif node.ax_node and node.ax_node.properties:+ # Properties that carry field values - must be excluded for password fields+ value_properties = {'value', 'valuetext'}for prop in node.ax_node.properties:try:if prop.name in include_attributes and prop.value is not None:+ if is_password_field and prop.name in value_properties:+ continue# Convert boolean to lowercase string, keep others as-isif isinstance(prop.value, bool):attributes_to_include[prop.name] = str(prop.value).lower()@@ -1193,10 +1206,12 @@ class DOMTreeSerializer:# Special handling for form elements - ensure current value is shown# For text inputs, textareas, and selects, prioritize showing the current value from AX treeif node.tag_name and node.tag_name.lower() in ['input', 'textarea', 'select']:+ if is_password_field:+ attributes_to_include.pop('value', None)# ALWAYS check AX tree - it reflects actual typed value, DOM attribute may not update- if node.ax_node and node.ax_node.properties:+ elif node.ax_node and node.ax_node.properties:for prop in node.ax_node.properties:- # Try valuetext first (human-readable display value)+ # Try valuetext first (human-readable display value)if prop.name == 'valuetext' and prop.value:value_str = str(prop.value).strip()if value_str:diff --git a/tests/ci/security/test_sensitive_data.py b/tests/ci/security/test_sensitive_data.pyindex a55ddc41c..43aaaa9a0 100644--- a/tests/ci/security/test_sensitive_data.py+++ b/tests/ci/security/test_sensitive_data.py@@ -436,3 +436,169 @@ def test_sensitive_data_filtered_with_domain_specific_format():# API key should be filtered outassert 'sk-secret-api-key-12345' not in combined_text, 'API key leaked into LLM messages!'assert '<secret>api_key</secret>' in combined_text, 'API key placeholder not found in messages'+++# ─── Tests for password field value redaction in DOM snapshots ────────────────+++def _make_dom_node(+ tag_name: str,+ attributes: dict[str, str],+ ax_value: str | None = None,+):+ """Create a minimal EnhancedDOMTreeNode for serializer testing."""+ from browser_use.dom.views import (+ EnhancedAXNode,+ EnhancedAXProperty,+ EnhancedDOMTreeNode,+ NodeType,+ )++ ax_node = None+ if ax_value is not None:+ ax_node = EnhancedAXNode(+ ax_node_id='ax-1',+ ignored=False,+ role='textbox',+ name=None,+ description=None,+ properties=[+ EnhancedAXProperty(name='valuetext', value=ax_value),+ ],+ child_ids=None,+ )++ return EnhancedDOMTreeNode(+ node_id=1,+ backend_node_id=1,+ node_type=NodeType.ELEMENT_NODE,+ node_name=tag_name.upper(),+ node_value='',+ attributes=attributes,+ is_scrollable=None,+ is_visible=True,+ absolute_position=None,+ target_id='target-1',+ frame_id=None,+ session_id=None,+ content_document=None,+ shadow_root_type=None,+ shadow_roots=None,+ parent_node=None,+ children_nodes=None,+ ax_node=ax_node,+ snapshot_node=None,+ )+++def test_password_field_value_excluded_from_dom_snapshot():+ """+ Password field values must never appear in DOM snapshots sent to the LLM.++ When a user types a password into <input type="password">, the accessibility tree+ stores the real value. The serializer extracts AX tree values for all form elements+ to show the LLM what's been typed. Without filtering, the password appears in the+ DOM text representation sent to the LLM on every subsequent step.++ Attack scenario:+ 1. Agent types password into <input type="password"> via sensitive_data placeholder+ 2. Next step: DOM snapshot extracts the typed value from the AX tree+ 3. Password appears in plaintext in the LLM context+ 4. Prompt injection on a later page can exfiltrate it+ """+ from browser_use.dom.serializer.serializer import DOMTreeSerializer+ from browser_use.dom.views import DEFAULT_INCLUDE_ATTRIBUTES++ secret_password = 'hubble_space_telescope'++ node = _make_dom_node(+ tag_name='input',+ attributes={'type': 'password', 'name': 'password', 'id': 'pw-field'},+ ax_value=secret_password,+ )++ attrs_str = DOMTreeSerializer._build_attributes_string(+ node, list(DEFAULT_INCLUDE_ATTRIBUTES), ''+ )++ assert secret_password not in attrs_str, (+ f'Password "{secret_password}" leaked into DOM serialization! '+ 'Password field values must be excluded from DOM snapshots sent to the LLM.'+ )+ # The type attribute should still be present so the LLM knows it's a password field+ assert 'type=password' in attrs_str, (+ 'Password field type attribute should be preserved'+ )+++def test_password_field_value_excluded_even_from_html_attributes():+ """+ Even if the DOM attribute 'value' is set (e.g. <input type="password" value="preset">),+ the serializer must strip it for password fields.+ """+ from browser_use.dom.serializer.serializer import DOMTreeSerializer+ from browser_use.dom.views import DEFAULT_INCLUDE_ATTRIBUTES++ preset_password = 'hubble_space_telescope'++ node = _make_dom_node(+ tag_name='input',+ attributes={'type': 'password', 'name': 'password', 'value': preset_password},+ ax_value=None, # no AX value, but HTML attribute has it+ )++ attrs_str = DOMTreeSerializer._build_attributes_string(+ node, list(DEFAULT_INCLUDE_ATTRIBUTES), ''+ )++ assert preset_password not in attrs_str, (+ f'Preset password "{preset_password}" leaked via HTML value attribute! '+ 'Password field values must be stripped regardless of source.'+ )+++def test_text_input_value_preserved():+ """Non-password input values should still be included (backward compatibility)."""+ from browser_use.dom.serializer.serializer import DOMTreeSerializer+ from browser_use.dom.views import DEFAULT_INCLUDE_ATTRIBUTES++ username = '[email protected]'++ node = _make_dom_node(+ tag_name='input',+ attributes={'type': 'text', 'name': 'username'},+ ax_value=username,+ )++ attrs_str = DOMTreeSerializer._build_attributes_string(+ node, list(DEFAULT_INCLUDE_ATTRIBUTES), ''+ )++ assert username in attrs_str, (+ 'Non-password input values should be preserved in DOM snapshots'+ )+++def test_password_field_without_type_attribute():+ """+ An input without an explicit type attribute defaults to 'text' — its value+ should NOT be stripped. Only explicit type="password" fields are protected.+ """+ from browser_use.dom.serializer.serializer import DOMTreeSerializer+ from browser_use.dom.views import DEFAULT_INCLUDE_ATTRIBUTES++ value = 'some_text_value'++ node = _make_dom_node(+ tag_name='input',+ attributes={'name': 'search'},+ ax_value=value,+ )++ attrs_str = DOMTreeSerializer._build_attributes_string(+ node, list(DEFAULT_INCLUDE_ATTRIBUTES), ''+ )++ assert value in attrs_str, (+ 'Input without type attribute should preserve its value'+ )