Skip to content

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

fix sensitive_data tests

Nick Sweeting48d88bff3318
diff --git a/tests/ci/test_agent_sensitive_data.py b/tests/ci/test_agent_sensitive_data.py
index 1806242e4..7edbc2f35 100644
--- a/tests/ci/test_agent_sensitive_data.py
+++ b/tests/ci/test_agent_sensitive_data.py
@@ -39,54 +39,39 @@ def message_manager():
def test_replace_sensitive_data_with_missing_keys(registry, caplog):
"""Test that _replace_sensitive_data handles missing keys gracefully"""
- import logging
-
# Create a simple Pydantic model with sensitive data placeholders
params = SensitiveParams(text='Please enter <secret>username</secret> and <secret>password</secret>')
- # Case 1: All keys present
+ # Case 1: All keys present - both placeholders should be replaced
sensitive_data = {'username': 'user123', 'password': 'pass456'}
result = registry._replace_sensitive_data(params, sensitive_data)
- assert 'user123' in result.text
- assert 'pass456' in result.text
- # Both keys should be replaced
+ assert result.text == 'Please enter user123 and pass456'
+ assert '<secret>' not in result.text # No secret tags should remain
- # Case 2: One key missing
+ # Case 2: One key missing - only available key should be replaced
sensitive_data = {'username': 'user123'} # password is missing
- # We'll capture the warning by mocking the logger
- with caplog.at_level(logging.WARNING, logger='browser_use.controller.registry.service'):
- result = registry._replace_sensitive_data(params, sensitive_data)
+ result = registry._replace_sensitive_data(params, sensitive_data)
+ assert result.text == 'Please enter user123 and <secret>password</secret>'
assert 'user123' in result.text
- assert '<secret>password</secret>' in result.text
- # Verify the behavior - the warning should have been logged
- # The test is just verifying the replacement behavior, not the logging
- # since logging capture can be unreliable in parallel tests
+ assert '<secret>password</secret>' in result.text # Missing key's tag remains
- # Case 3: Multiple keys missing
+ # Case 3: Multiple keys missing - all tags should be preserved
sensitive_data = {} # both keys missing
result = registry._replace_sensitive_data(params, sensitive_data)
+ assert result.text == 'Please enter <secret>username</secret> and <secret>password</secret>'
assert '<secret>username</secret>' in result.text
assert '<secret>password</secret>' in result.text
- # Verify both tags are preserved when keys are missing
- caplog.clear()
- # Case 4: One key empty
+ # Case 4: One key empty - empty values are treated as missing
sensitive_data = {'username': 'user123', 'password': ''}
result = registry._replace_sensitive_data(params, sensitive_data)
+ assert result.text == 'Please enter user123 and <secret>password</secret>'
assert 'user123' in result.text
- assert '<secret>password</secret>' in result.text
- # Empty value should be treated the same as missing key
- assert 'Missing' in caplog.text and 'password' in caplog.text
- caplog.clear()
+ assert '<secret>password</secret>' in result.text # Empty value's tag remains
def test_simple_domain_specific_sensitive_data(registry, caplog):
"""Test the basic functionality of domain-specific sensitive data replacement"""
- # Set log level to capture warnings
- import logging
-
- caplog.set_level(logging.WARNING, logger='browser_use.controller.registry.service')
-
# Create a simple Pydantic model with sensitive data placeholders
params = SensitiveParams(text='Please enter <secret>username</secret> and <secret>password</secret>')
@@ -98,18 +83,17 @@ def test_simple_domain_specific_sensitive_data(registry, caplog):
# Without a URL, domain-specific secrets should NOT be exposed
result = registry._replace_sensitive_data(params, sensitive_data)
+ assert result.text == 'Please enter <secret>username</secret> and <secret>password</secret>'
assert '<secret>username</secret>' in result.text # Should NOT be replaced without URL
assert '<secret>password</secret>' in result.text # Password is missing in sensitive_data
- assert 'username' in caplog.text # Both should be logged as missing
- assert 'password' in caplog.text
- caplog.clear()
+ assert 'example_user' not in result.text # Domain-specific value should not appear
# Test with a matching URL - domain-specific secrets should be exposed
result = registry._replace_sensitive_data(params, sensitive_data, 'https://example.com/login')
+ assert result.text == 'Please enter example_user and <secret>password</secret>'
assert 'example_user' in result.text # Should be replaced with matching URL
assert '<secret>password</secret>' in result.text # Password is still missing
- assert 'password' in caplog.text # Only password should be logged as missing
- caplog.clear()
+ assert '<secret>username</secret>' not in result.text # Username tag should be replaced
def test_match_url_with_domain_pattern():
diff --git a/tests/ci/test_browser_session_start.py b/tests/ci/test_browser_session_start.py
index b5d73ffef..986b0a249 100644
--- a/tests/ci/test_browser_session_start.py
+++ b/tests/ci/test_browser_session_start.py
@@ -479,17 +479,8 @@ class TestBrowserSessionStart:
finally:
await session.kill()
- async def test_user_data_dir_not_allowed_to_corrupt_default_profile(self, caplog):
+ async def test_user_data_dir_not_allowed_to_corrupt_default_profile(self):
"""Test user_data_dir handling for different browser channels and version mismatches."""
- import logging
-
- # Temporarily enable propagation for browser_use logger to capture logs
- browser_use_logger = logging.getLogger('browser_use')
- original_propagate = browser_use_logger.propagate
- browser_use_logger.propagate = True
-
- caplog.set_level(logging.WARNING, logger='browser_use')
-
# Test 1: Chromium with default user_data_dir and default channel should work fine
session = BrowserSession(
browser_profile=BrowserProfile(
@@ -509,32 +500,28 @@ class TestBrowserSessionStart:
finally:
await session.kill()
- # Clear any previous log records
- caplog.clear()
-
- # Test 2: Chrome with default user_data_dir should show warning and change dir
- with caplog.at_level(logging.WARNING):
- profile2 = BrowserProfile(
- headless=True,
- user_data_dir=CONFIG.BROWSER_USE_DEFAULT_USER_DATA_DIR,
- channel=BrowserChannel.CHROME,
- keep_alive=False,
- )
+ # Test 2: Chrome with default user_data_dir should automatically change dir
+ profile2 = BrowserProfile(
+ headless=True,
+ user_data_dir=CONFIG.BROWSER_USE_DEFAULT_USER_DATA_DIR,
+ channel=BrowserChannel.CHROME,
+ keep_alive=False,
+ )
- # The validator should have changed the user_data_dir
+ # The validator should have changed the user_data_dir to avoid corruption
assert profile2.user_data_dir != CONFIG.BROWSER_USE_DEFAULT_USER_DATA_DIR
assert profile2.user_data_dir == CONFIG.BROWSER_USE_DEFAULT_USER_DATA_DIR.parent / 'default-chrome'
- # Check warning was logged
- warning_found = any(
- 'Changing user_data_dir=' in record.message and 'CHROME' in record.message for record in caplog.records
- )
- assert warning_found, (
- f'Expected warning about changing user_data_dir was not found. Captured logs: {[record.message for record in caplog.records if "browser_use" in record.name]}'
+ # Test 3: Edge with default user_data_dir should also change
+ profile3 = BrowserProfile(
+ headless=True,
+ user_data_dir=CONFIG.BROWSER_USE_DEFAULT_USER_DATA_DIR,
+ channel=BrowserChannel.MSEDGE,
+ keep_alive=False,
)
- # Restore original propagate setting
- browser_use_logger.propagate = original_propagate
+ assert profile3.user_data_dir != CONFIG.BROWSER_USE_DEFAULT_USER_DATA_DIR
+ assert profile3.user_data_dir == CONFIG.BROWSER_USE_DEFAULT_USER_DATA_DIR.parent / 'default-msedge'
# only run if `/Applications/Brave Browser.app` is installed
@pytest.mark.skipif(