---
title: "How to keep credentials out of a browser agent's context and logs"
description: "Keep credentials out of a browser agent by letting code, not the model, handle logins. Load a saved login state or fill password fields from a secret store in the harness, give the agent its own least-privilege account, and mask secrets in traces, videos and logs before they are stored."
url: "https://www.aiagentsandbox.io/keep-credentials-out-of-a-browser-agent/"
site: "AIAgentSandbox.io"
author: "AIAgentSandbox editors"
published: "2026-11-24"
updated: "2026-11-24"
---

# How to keep credentials out of a browser agent's context and logs

AIAgentSandbox editors. Published 2026-11-24, updated 2026-11-24.

Keep credentials out of a browser agent by letting code, not the model, handle logins. Load a saved login state or fill password fields from a secret store in the harness, give the agent its own least-privilege account, and mask secrets in traces, videos and logs before they are stored.

To keep credentials out of a browser agent's context and logs, never give them to the model. Let the harness, the code around the model, handle every login, and treat everything the agent sees or records as something that may be read later.

## Where can a credential leak from a browser agent?

A credential can leak from a browser agent through six places:

Table: Where browser agent credentials leak

| Place | How it leaks |
|---|---|
| The prompt and conversation | The model can repeat it, and it is sent to the model provider |
| Model and application logs | Prompts and tool calls are often logged in full |
| Traces and screenshots | A trace stores DOM snapshots, so a typed password can be in it |
| Videos and session replays | Every keystroke shown on screen is recorded |
| Saved login state files | Cookies and tokens in the file let anyone act as the user |
| A manipulated agent | A web page instructs the agent to type secrets into an attacker's form |

The OWASP Top 10 for LLM applications lists two of these risks directly. LLM02, Sensitive Information Disclosure, covers credentials among the data a model application can expose. LLM01, Prompt Injection, covers the last row: "Indirect prompt injections occur when an LLM accepts input from external sources, such as websites or files" ([OWASP LLM01:2025](https://genai.owasp.org/llmrisk/llm01-prompt-injection/), [OWASP LLM02:2025](https://genai.owasp.org/llmrisk/llm022025-sensitive-information-disclosure/), read 2026-11-24).

Research shows the last row is practical. The EIA study placed hidden elements on real websites and reported "up to 70% ASR in stealing specific PII" from a capable web agent ([EIA, arXiv 2409.11295](https://arxiv.org/abs/2409.11295), read 2026-11-24).

## Step 1: How do I log an agent in without the model seeing the password?

Log the agent in with code, before or during the task, so the password never enters the model's context. There are two patterns.

**Pattern A: a saved login state.**

1. Sign in once in a clean browser context, by script or by hand.
2. Save it with Playwright's `context.storageState({ path })`.
3. Start each agent session with `browser.newContext({ storageState })`, which Playwright says "can be used to initialize context with logged-in information" ([Playwright Browser API](https://playwright.dev/docs/api/class-browser), read 2026-11-24).

The agent opens the site already signed in. It never sees a password.

**Pattern B: a login tool.**

1. Give the agent a tool such as `login(site)` instead of a password.
2. When the agent calls it, the harness reads the secret from your secret store and fills the field with `locator.fill()`, outside the model loop.
3. The tool returns only "signed in" or "failed" to the model.
4. Check the page's origin before filling, so the tool only fills the password on the real site's address.

Pattern B also handles one-time codes: the tool can fetch the code from an authenticator or ask a person, and the model sees neither.

## Step 2: How do I protect the saved login state file?

Protect the login state file like a password, because it works like one. Playwright's authentication guide says "The browser state file may contain sensitive cookies and headers that could be used to impersonate you or your test account" and "We strongly discourage checking them into private or public repositories" ([Playwright authentication](https://playwright.dev/docs/auth), read 2026-11-24).

1. Keep it in your secret store, not in the image or the repository.
2. Mount it into the session at start and delete the session afterwards.
3. Give it a short life: refresh it on a schedule and revoke old sessions on the site.
4. Use one file per agent identity, never one shared account for all agents.

## Step 3: Which account should the agent use?

The agent should use its own account with the least privilege the task needs, not a person's account. OWASP's first mitigation for prompt injection is to "Enforce privilege control and least privilege access", and to "Restrict the model's access privileges to the minimum necessary for its intended operations". It also advises: "Provide the application with its own API tokens for extensible functionality" (OWASP LLM01:2025, read 2026-11-24).

In practice:

- Create a service account per agent or per task type.
- Give it read access if the task only reads.
- Turn on the site's own alerts and limits for that account.
- Never reuse a person's browser profile, which carries every site they are signed in to. Chrome no longer allows its remote debugging switches on the default profile, partly because of "an increase in attackers using Chrome Remote Debugging to extract cookies" ([Chrome remote debugging changes](https://developer.chrome.com/blog/remote-debugging-port), read 2026-11-24).

## Step 4: How do I keep secrets out of traces, videos and logs?

Keep secrets out of recordings by masking at capture time, not by cleaning up afterwards.

1. **Start recording after login.** With pattern A, the login happened before the session. With pattern B, pause tracing and video around the login tool call.
2. **Mask inputs in DOM replays.** rrweb masks password fields by default (`maskInputOptions` defaults to `{ password: true }`), can mask every input with `maskAllInputs`, and skips any element with the class `rr-block`: "An element with the class name `.rr-block` will not be recorded" ([rrweb guide](https://github.com/rrweb-io/rrweb/blob/master/guide.md), read 2026-11-24).
3. **Treat Playwright traces as sensitive.** A trace holds "a set of complete DOM snapshots for each action" and the network requests ([Playwright trace viewer](https://playwright.dev/docs/trace-viewer), read 2026-11-24). Cookies and authorisation headers in those requests are in the trace. Store traces with the same access rules as the login state.
4. **Redact before logging.** Filter known secret values and patterns (tokens, `Authorization` headers, `Set-Cookie`) out of prompt logs, tool-call logs and network logs before they leave the session.
5. **Keep recordings for a limited time.** Delete them on a schedule.

## Step 5: How do I stop a manipulated agent from sending secrets out?

Stop a manipulated agent from sending secrets out by limiting where the browser can connect and by requiring approval for risky actions. OWASP lists "Require human approval for high-risk actions" among its mitigations (OWASP LLM01:2025, read 2026-11-24). Combine that with a network allow-list, so that even if an agent is tricked into posting data, the browser cannot reach the attacker's site. Our guide to [restricting a browser agent's network access](https://www.aiagentsandbox.io/restrict-browser-agent-network-access/) shows how.

## What is a quick checklist for browser agent credentials?

Use this checklist before an agent touches a real account:

1. No password, token or cookie appears in the prompt, the system message or tool results.
2. Logins happen through a saved state file or a login tool run by code.
3. The agent has its own least-privilege account.
4. Login state files live in a secret store and expire.
5. Recordings start after login, and password fields are masked.
6. Logs are filtered for secrets before storage.
7. The browser can only reach the sites the task needs.
8. Purchases, deletions and messages to new recipients need a person's approval.

## What we read for this article

- [OWASP LLM01:2025 Prompt Injection](https://genai.owasp.org/llmrisk/llm01-prompt-injection/), read 2026-11-24
- [OWASP LLM02:2025 Sensitive Information Disclosure](https://genai.owasp.org/llmrisk/llm022025-sensitive-information-disclosure/), read 2026-11-24
- [EIA: Environmental Injection Attack on Generalist Web Agents (arXiv 2409.11295)](https://arxiv.org/abs/2409.11295), read 2026-11-24
- [Playwright authentication](https://playwright.dev/docs/auth), read 2026-11-24
- [Playwright Browser API](https://playwright.dev/docs/api/class-browser), read 2026-11-24
- [Playwright trace viewer](https://playwright.dev/docs/trace-viewer), read 2026-11-24
- [rrweb guide](https://github.com/rrweb-io/rrweb/blob/master/guide.md), read 2026-11-24
- [Chrome remote debugging changes](https://developer.chrome.com/blog/remote-debugging-port), read 2026-11-24

## FAQ

### Why should I not put a password in the agent's prompt?

Everything in the prompt can be repeated by the model, stored in logs and sent to the model provider. A page carrying a prompt injection can also ask the agent to type what it knows into a form. A password the model never saw cannot be leaked that way.

### How can an agent log in without seeing the password?

The harness logs in for it. Either load a saved login state file into a new browser context, or let the agent ask for a login and have code fill the password field from a secret store, outside the model's view.

### Are Playwright storageState files sensitive?

Yes. Playwright's documentation says the file may contain sensitive cookies and headers that could be used to impersonate the account, and strongly discourages checking it into any repository.

### How do I keep passwords out of session recordings?

Mask them at capture time. rrweb masks password inputs by default and can mask all inputs or block marked elements. For screenshots and videos, keep login steps out of the recording or blur the fields.
