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
Keep sensitive live values out of the snapshot and surface checked state
Magnus Müller3b12a946ccd4
diff --git a/browser_use/dom/enhanced_snapshot.py b/browser_use/dom/enhanced_snapshot.pyindex 0e838dda9..a983828a4 100644--- a/browser_use/dom/enhanced_snapshot.py+++ b/browser_use/dom/enhanced_snapshot.py@@ -43,6 +43,30 @@ def _parse_computed_styles(strings: list[str], style_indices: list[int]) -> dictreturn styles+# Live values of these fields never leave the snapshot: they would otherwise be+# serialized by EnhancedDOMTreeNode.__json__ and could reach logs or the LLM.+_SENSITIVE_INPUT_TYPES = frozenset({'password', 'file', 'hidden'})+_SENSITIVE_AUTOCOMPLETE_PREFIXES = ('cc-', 'one-time-code')+++def _is_sensitive_input(strings: list[str], nodes: NodeTreeSnapshot, snapshot_index: int) -> bool:+ """True for password/file/hidden inputs and payment or one-time-code autocomplete fields."""+ attribute_lists = nodes.get('attributes')+ if not attribute_lists or snapshot_index >= len(attribute_lists):+ return False+ indices = attribute_lists[snapshot_index]+ for name_index, value_index in zip(indices[0::2], indices[1::2]):+ if not (0 <= name_index < len(strings) and 0 <= value_index < len(strings)):+ continue+ name = strings[name_index].lower()+ value = strings[value_index].lower()+ if name == 'type' and value in _SENSITIVE_INPUT_TYPES:+ return True+ if name == 'autocomplete' and value.startswith(_SENSITIVE_AUTOCOMPLETE_PREFIXES):+ return True+ return False++def build_snapshot_lookup(snapshot: CaptureSnapshotReturns,device_pixel_ratio: float = 1.0,@@ -98,7 +122,7 @@ def build_snapshot_lookup(rare = nodes.get(key)if rare:for idx, string_index in zip(rare.get('index', []), rare.get('value', [])):- if 0 <= string_index < len(strings):+ if 0 <= string_index < len(strings) and not _is_sensitive_input(strings, nodes, idx):input_value_by_index[idx] = strings[string_index]input_checked_set: set[int] = set(nodes['inputChecked']['index']) if 'inputChecked' in nodes else set()has_checked_data = 'inputChecked' in nodesdiff --git a/browser_use/dom/service.py b/browser_use/dom/service.pyindex 03e5c55c4..8eb38acb5 100644--- a/browser_use/dom/service.py+++ b/browser_use/dom/service.py@@ -814,11 +814,16 @@ class DomService:# Show the value a field currently holds, not only the static attribute.# JS, autofill, and framework bindings set the property without touching# the attribute, so the agent used to see pre-filled fields as empty (#5647).- if snapshot_data and snapshot_data.input_value is not None and node['nodeName'].upper() in ('INPUT', 'TEXTAREA'):- input_type = (attributes or {}).get('type', '').lower()- if input_type not in ('password', 'file', 'hidden'):+ if snapshot_data and node['nodeName'].upper() in ('INPUT', 'TEXTAREA'):+ if snapshot_data.input_value is not None:attributes = dict(attributes or {})attributes['value'] = snapshot_data.input_value+ if snapshot_data.input_checked is not None:+ attributes = dict(attributes or {})+ if snapshot_data.input_checked:+ attributes['checked'] = 'true'+ else:+ attributes.pop('checked', None)# DIAGNOSTIC: Log when interactive elements don't have snapshot dataif not snapshot_data and node['nodeName'].upper() in ['INPUT', 'BUTTON', 'SELECT', 'TEXTAREA', 'A']:diff --git a/tests/ci/test_dom_live_input_value.py b/tests/ci/test_dom_live_input_value.pyindex 5517ef880..d322269dd 100644--- a/tests/ci/test_dom_live_input_value.py+++ b/tests/ci/test_dom_live_input_value.py@@ -18,10 +18,18 @@ PAGE = """<!DOCTYPE html><label for="notes">Notes</label><textarea id="notes"></textarea><input id="secret" type="password">+ <input id="otp" type="text" autocomplete="one-time-code">+ <input id="card" type="text" autocomplete="cc-number">+ <input id="agree" type="checkbox" checked>+ <input id="news" type="checkbox"><script>document.getElementById('name').value = 'Ada Lovelace';document.getElementById('notes').value = 'call back tuesday';document.getElementById('secret').value = 'hunter2';+ document.getElementById('otp').value = '493021';+ document.getElementById('card').value = '4242424242424242';+ document.getElementById('agree').checked = false;+ document.getElementById('news').checked = true;</script></body></html>"""@@ -46,8 +54,15 @@ async def test_live_input_values_reach_the_agent(browser_session, http_server):assert by_id['name'].attributes.get('value') == 'Ada Lovelace'assert by_id['notes'].attributes.get('value') == 'call back tuesday'assert by_id['secret'].attributes.get('value') is None, 'password values must not be exposed'+ assert by_id['otp'].attributes.get('value') is None, 'one-time codes must not be exposed'+ assert by_id['card'].attributes.get('value') is None, 'card numbers must not be exposed'+ assert by_id['secret'].snapshot_node is not None and by_id['secret'].snapshot_node.input_value is None+ assert by_id['agree'].attributes.get('checked') is None, 'live unchecked state wins over the checked attribute'+ assert by_id['news'].attributes.get('checked') == 'true'llm_view = state.dom_state.llm_representation()assert 'Ada Lovelace' in llm_viewassert 'call back tuesday' in llm_viewassert 'hunter2' not in llm_view+ assert '493021' not in llm_view+ assert '4242424242424242' not in llm_view