Skip to content

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

Refactor sensitive data handling across examples

Magnus Müller719761313c4d
diff --git a/docs/customize/examples/sensitive-data.mdx b/docs/customize/examples/sensitive-data.mdx
index 89b86e243..940fd6e64 100644
--- a/docs/customize/examples/sensitive-data.mdx
+++ b/docs/customize/examples/sensitive-data.mdx
@@ -18,11 +18,11 @@ company_credentials = {'x_user': '[email protected]', 'x_pass': 'your
sensitive_data = company_credentials
# Option 2: Secrets per domain with regex
-# sensitive_data: dict[str, str | dict[str, str]] = {
+# sensitive_data = {
# 'https://*.example-staging.com': company_credentials,
# 'http*://test.example.com': company_credentials,
# 'https://example.com': company_credentials,
-# 'https://*.google.com': {'g_email': '[email protected]', 'g_pass': 'google_password'},
+# 'https://google.com': {'g_email': '[email protected]', 'g_pass': 'google_password'},
# }
diff --git a/examples/custom-functions/2fa.py b/examples/custom-functions/2fa.py
index c9a41dfaa..6273ebbdd 100644
--- a/examples/custom-functions/2fa.py
+++ b/examples/custom-functions/2fa.py
@@ -1,4 +1,3 @@
-import asyncio
import logging
import os
import sys
@@ -9,58 +8,27 @@ from dotenv import load_dotenv
load_dotenv()
-import pyotp # type: ignore
-from browser_use import ActionResult, Agent, ChatOpenAI, Tools
+from browser_use import Agent
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
-tools = Tools()
+secret_key = os.environ.get('OTP_SECRET_KEY')
+if not secret_key:
+ # For this example copy the code from the website https://authenticationtest.com/totpChallenge/
+ # For real 2fa just copy the secret key when you setup 2fa, you can get this e.g. in 1Password
+ secret_key = 'JBSWY3DPEHPK3PXP'
[email protected]('Get 2FA code from when OTP is required')
-async def get_otp_2fa() -> ActionResult:
- """
- Custom action to retrieve 2FA/MFA code from OTP secret key using pyotp.
- The OTP secret key should be set in the environment variable OTP_SECRET_KEY.
- """
- secret_key = os.environ.get('OTP_SECRET_KEY')
- if not secret_key:
- raise ValueError('OTP_SECRET_KEY environment variable is not set')
+sensitive_data: dict[str, str] = {'otp_secret': secret_key}
- totp = pyotp.TOTP(secret_key, digits=6)
- code = totp.now()
- return ActionResult(extracted_content=code)
+task = """Steps:
+1. Go to https://authenticationtest.com/totpChallenge/ and log in.
+2. Use the the secret otp_secret to generate the 2FA code."""
-async def main():
- # Example task using the 1Password 2FA action
- task = """
- Steps:
- 1. Go to https://authenticationtest.com/totpChallenge/ and try to log in.
- 2. If prompted for 2FA code:
- 2.1. Use the get_2fa_code action to retrieve the 2FA code.
- 2.2. Submit the code provided by the get_2fa_code action.
-
- Considerations:
- - ALWAYS use the get_2fa_code action to retrieve the 2FA code if needed.
- - NEVER skip the 2FA step if the page requires it.
- - NEVER extract the code from the page.
- - NEVER use a code that is not generated by the get_2fa_code action.
- - NEVER hallucinate the 2FA code, always use the get_2fa_code action to get it.
-
- You are completely FORBIDDEN to use any other method to get the 2FA code.
- """
- model = ChatOpenAI(model='gpt-4.1-mini')
- agent = Agent(task=task, llm=model, tools=tools)
-
- result = await agent.run()
- print(f'Task completed with result: {result}')
-
-
-if __name__ == '__main__':
- asyncio.run(main())
+Agent(task=task, sensitive_data=sensitive_data).run_sync()
diff --git a/examples/features/secure.py b/examples/features/secure.py
index 045f4e2bc..8310a0028 100644
--- a/examples/features/secure.py
+++ b/examples/features/secure.py
@@ -69,7 +69,7 @@ browser_profile = BrowserProfile(allowed_domains=['*google.com', 'browser-use.co
# Sensitive data (optional) - {key: sensitive_information} - we filter out the sensitive_information from any input to the LLM, it will only work with placeholder.
# By default we pass screenshots to the LLM which can contain your information. Set use_vision=False to disable this.
# If you trust your LLM endpoint, you don't need to worry about this.
-sensitive_data: dict[str, str | dict[str, str]] = {'company_name': 'browser-use'}
+sensitive_data = {'company_name': 'browser-use'}
# Create Agent
diff --git a/examples/features/sensitive_data.py b/examples/features/sensitive_data.py
index ac33225ff..b44d8d2c8 100644
--- a/examples/features/sensitive_data.py
+++ b/examples/features/sensitive_data.py
@@ -25,13 +25,13 @@ company_credentials = {'company_username': '[email protected]', 'company_password
# Map the same credentials to multiple domains for secure access control
# Type annotation to satisfy pyright
-sensitive_data: dict[str, str | dict[str, str]] = {
+sensitive_data = {
'https://example.com': company_credentials,
'https://admin.example.com': company_credentials,
'https://*.example-staging.com': company_credentials,
'http*://test.example.com': company_credentials,
- # You can also add domain-specific credentials
- 'https://google.com': {'g_email': '[email protected]', 'g_pass': 'google_password'},
+ # # You can also add domain-specific credentials
+ # 'https://google.com': {'g_email': '[email protected]', 'g_pass': 'google_password'},
'this_email_works_on_all_domains': '[email protected]',
}
# Update task to use one of the credentials above