diff --git a/.changeset/teach-secrets.md b/.changeset/teach-secrets.md new file mode 100644 index 00000000..c8b84b0b --- /dev/null +++ b/.changeset/teach-secrets.md @@ -0,0 +1,7 @@ +--- +"turno-browser": patch +--- + +Teach mode and flow recording: a card number or expiry field named like one (`cardnumber`, `card_number`, `ccNum`, `card-expiry`) now counts as a card field without an `autocomplete` value, so a flow keeps no value for it. The vault's values are read from the secret store when teach mode or a recording starts and held in memory only, so a vault value typed by hand keeps no default before `browser_login` has run: a user name as the whole typed value (case aside), a password also inside other text when it has at least 4 characters. A vault value the page shows is kept in no locator, fingerprint or element name, and a trace pauses before a vault value is typed. A recorded `browser_navigate` step keeps its address without query string or fragment. + +A taught vault sign-in now starts only at a password or one-time code input. It takes in the user name typed into the same form on that page across a box checked or a key, and no typing before a submit, so a sign-in that asks the user name first replays as shown. Later typing is taken in only into the fields it took in. The person's own submit ends the sign-in, and is dropped only when `browser_login` will submit: with the identity's policy at `"autoSubmit": false` the submit stays a step. What the person does in the sign-in's form before submitting (a box checked, Tab, a cookie banner's button) goes before `browser_login`, while Escape, a link or a step outside the form ends the sign-in; a show-password toggle is dropped, and the button of a page that signs in from script stays a step after `browser_login`. In the page, a text area or an editable element named like a secret now reports only its length, as an input does, and names are read word by word as the server reads them. diff --git a/apps/docs/content/docs/browser/flows.mdx b/apps/docs/content/docs/browser/flows.mdx index 191b2652..89901f14 100644 --- a/apps/docs/content/docs/browser/flows.mdx +++ b/apps/docs/content/docs/browser/flows.mdx @@ -63,8 +63,13 @@ A refusal costs the agent one look at the page; a wrong element costs a wrong ac Text typed into a field becomes a parameter, with what was typed as its default. A sensitive one keeps no value: a password field, a one-time code or card field, a field named like `password`, -`otp`, `pin` or `token`, a vault value typed by hand, or any parameter the agent lists in -`sensitive` when it stops. A vault sign-in is a `browser_login` step with the site name only. +`otp`, `pin`, `token` or `cardnumber`, a vault value typed by hand, or any parameter the agent +lists in `sensitive` when it stops. The vault's values are read before the first typed step is +kept and held in memory only; a vault user name counts as the whole typed value (case aside), a +vault password also inside other text when it has at least 4 characters. A vault user name typed +outside a sign-in only loses its default. A vault sign-in is a `browser_login` step with the site +name only. A `browser_navigate` step keeps its address without query string or fragment (they +can carry tokens). Replay needs the sensitive values again; a value written `keychain:` is read from the secret store (`turno-browser secret set `), so the agent never sees it. Into a password or one-time code field an agent's `browser_replay` types only such a value; a plain one is refused diff --git a/apps/docs/content/docs/browser/teach-mode.mdx b/apps/docs/content/docs/browser/teach-mode.mdx index d7bf1146..ee767fe0 100644 --- a/apps/docs/content/docs/browser/teach-mode.mdx +++ b/apps/docs/content/docs/browser/teach-mode.mdx @@ -56,12 +56,33 @@ The rules for values are those of `browser_record`: - What you type is a parameter, with what you typed as its default. The review can make it a fixed value instead, or a parameter with no value kept. -- A password, a one-time code (also a field named only `code` in a sign-in form), a card number, - a field named like one, or a value of the - [vault](/docs/browser/vault) never keeps its value: the flow asks for it at each run, and the - review cannot make it fixed. -- A sign-in on a site of the vault is one `browser_login` step with the site's name: the fields you - typed into become that step, and your own submit is dropped when the vault submits. +- A password, a one-time code (also a field named only `code` in a sign-in form), a card number + or expiry (by its `autocomplete` or by a name such as `cardnumber` or `card_number`), a field + named like one, or a value of the [vault](/docs/browser/vault) never keeps its value: the flow + asks for it at each run, and the review cannot make it fixed. The vault's values are read when + teach mode starts and held in memory only, so one you type by hand is known from the first + step. A vault user name counts only as the whole of what you typed (case aside); a vault + password also counts inside other text when it has at least 4 characters. +- A vault value on the page (a signed-in user's name on a button, in any case) is kept in no + locator, fingerprint or element name of the flow. +- A sign-in on a site of the vault is one `browser_login` step with the site's name. It starts + when you type into a password or one-time code field on a page of that site. The user name you + typed into the same form on that page becomes part of it too, across a box checked, a key (Tab, + Escape) or a button that submits nothing (a show-password toggle), and so does typing into the + same fields afterwards. Typing before a submit (a click on a submit button, or Enter in that + form), another field of the form, and typing on an earlier page stay steps of their own, so a + sign-in that asks the user name first replays as you did it. A vault user name typed outside a + sign-in (an email at checkout) only loses its default. +- Your own submit (the click on the sign-in button, or Enter) ends the sign-in. It is dropped + when `browser_login` submits the form itself. +- The sign-in stays open until your submit, a new page, Escape outside its fields, a link, or a + step outside its form (without a form: any click, or typing outside its fields). When + `browser_login` submits, what you do in the form meanwhile (a box checked, a choice, Tab, a + cookie banner's button) goes before the `browser_login` step, which sends the form. A user name + field of the form you type into first after the password becomes part of the sign-in too. A + click on a button that submits nothing waits: a show-password toggle is dropped, and another one + goes before `browser_login` too. When the sign-in ends without your submit, the last such click + (the button of a page that signs in from script) goes after it, since it is how you signed in. - Addresses keep no query string or fragment (they can carry tokens). ## Review and save diff --git a/packages/browser/src/fingerprint.ts b/packages/browser/src/fingerprint.ts index aa2463e0..f054a461 100644 --- a/packages/browser/src/fingerprint.ts +++ b/packages/browser/src/fingerprint.ts @@ -149,7 +149,7 @@ export async function fingerprintOf(locator: Locator, url: string): Promise): Fingerprint { const r = (t: string) => redactor.text(t); return { role: fp.role, diff --git a/packages/browser/src/host.ts b/packages/browser/src/host.ts index a53c3102..28830ed9 100644 --- a/packages/browser/src/host.ts +++ b/packages/browser/src/host.ts @@ -613,7 +613,12 @@ class SessionBackend extends EventEmitter implements ServerBackend { const key = session ?? DIRECT_SESSION; let recorder = this.recorders.get(key); if (!recorder) { - recorder = new FlowRecorder(identity, this.host.redactor, () => this.recordSettings?.flows === true); + recorder = new FlowRecorder( + identity, + this.host.redactor, + () => this.recordSettings?.flows === true, + this.host.vaultStore, + ); this.recorders.set(key, recorder); } return recorder; @@ -1649,7 +1654,7 @@ class SessionBackend extends EventEmitter implements ServerBackend { const result = await tool.handle(context, parsed.params); if (stock) this.watch.follow(session ?? DIRECT_SESSION, stock.currentPage()); if (name === "browser_login") { - if (!result.isError) recorder?.record("browser_login", { site: parsed.params.site }); + if (!result.isError) await recorder?.record("browser_login", { site: parsed.params.site }); await recording?.afterStep({ tool: name, ok: !result.isError, screenshot: false }); } return unchosenNow ? this.withUserTabNote(result, unchosenNow(), json) : result; @@ -1889,7 +1894,8 @@ class SessionBackend extends EventEmitter implements ServerBackend { const pending = recorder?.active ? await recorder.capture(name, args, page).catch(() => undefined) : undefined; const secretHook = this.secretHook(recording); if (secretHook && page && (name === "browser_type" || name === "browser_fill_form")) { - const secrets = await secretValues(page, name, args); + const vault = await recorder?.vaultValues(); + const secrets = await secretValues(page, name, args, vault && ((v) => vault.holds(v))); if (secrets.length) { for (const v of secrets) this.host.redactor.add(v, "sensitive value"); await secretHook(page); @@ -2808,6 +2814,7 @@ function liveServerFor( policy, guard, redactor, + ...(options.vaultStore ? { vaultStore: options.vaultStore } : {}), headless: options.config.browser.launchOptions?.headless === true, // Before a launch, the engine identity.json and the flags would pick. viewRefusal: (engine) => { diff --git a/packages/browser/src/live-server.ts b/packages/browser/src/live-server.ts index 752ab60e..56611eba 100644 --- a/packages/browser/src/live-server.ts +++ b/packages/browser/src/live-server.ts @@ -5,6 +5,7 @@ import type { AddressInfo } from "node:net"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { replaceFileSync } from "turno-core/paths"; +import type { SecretStore } from "turno-core/secrets"; import type { BrowserContext, CDPSession, Page } from "patchright-core"; import type { IdentityRef } from "./engine.js"; import { @@ -118,6 +119,8 @@ export interface LiveServerOptions { policy: LivePolicy; guard: VaultGuard; redactor: Redactor; + /** The secret store teach mode reads the vault's values from (tests inject a fake); default: this machine's. */ + vaultStore?: SecretStore; headless: boolean; /** Why the engine shows no pixels; `engine` is the running one, undefined before a launch. */ viewRefusal(engine: string | undefined): string | undefined; @@ -912,7 +915,14 @@ export class LiveServer implements BrowserWatch { } finally { this.teachStarting = false; } - const session = new TeachSession(b.context, this.o.identity, this.o.redactor, name, () => this.pushTeach()); + const session = new TeachSession( + b.context, + this.o.identity, + this.o.redactor, + name, + () => this.pushTeach(), + this.o.vaultStore, + ); // `overwrite` given at the start holds for the save, which the panel or Enter asks for later. this.teaching = { session, phase: "teaching", held, overwrite }; try { diff --git a/packages/browser/src/recorder.ts b/packages/browser/src/recorder.ts index ce1fc1b2..34e4d834 100644 --- a/packages/browser/src/recorder.ts +++ b/packages/browser/src/recorder.ts @@ -19,9 +19,11 @@ import { writeFlow, } from "./flows.js"; import { existsSync } from "node:fs"; +import type { SecretStore } from "turno-core/secrets"; import { ActionCache, actionCachePath, differences, fingerprintOf, redactFingerprint, refusal } from "./fingerprint.js"; import type { Redactor } from "./redact.js"; import { secretField } from "./sensitive.js"; +import { VaultValues, vaultValues } from "./vault.js"; import type { TurnoTool } from "./tools.js"; import { type ToolSchema, upstream } from "./upstream.js"; @@ -35,9 +37,10 @@ import { type ToolSchema, upstream } from "./upstream.js"; * * - Typed text is always a parameter. It keeps what was typed as its default only when the * field is not sensitive: a password field, an `autocomplete` of a password, a one-time code - * or a card, a field named like one (`password`, `otp`, `pin`, `cvv`, `token`...), or a value - * that is one of this server's vault values. The agent can mark more parameters sensitive - * when it stops a named recording, which is held in memory until then. + * or a card, a field named like one (`password`, `otp`, `pin`, `cvv`, `token`, `cardnumber`...), + * or a value of the identity's vault. The vault's values are read from the secret store before + * the first typed step is kept, and held in memory only. The agent can mark more parameters + * sensitive when it stops a named recording, which is held in memory until then. * - `browser_login` is recorded as its site name; the values are read from the vault again * at replay. * - Nothing else of a page (snapshots, results, values of other fields) is stored. @@ -332,6 +335,8 @@ export class FlowRecorder { private overwrite = false; private auto: Draft | undefined; private suspended = 0; + /** The vault's values, read once per named recording (and once for the identity's own). */ + private vaultLoad: Promise | undefined; /** The fingerprints of what the identity's actions worked on (`fingerprint.ts`). */ readonly cache: ActionCache; @@ -341,6 +346,8 @@ export class FlowRecorder { private readonly redactor: Redactor, /** identity.json `record.flows`: record every session as `auto-