A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Harden browser downloads, deletes, and uploads
Fei241137ec4c39028e
diff --git a/browser-plugins/test/write-safety.test.js b/browser-plugins/test/write-safety.test.jsindex 9375f1dd..e4c63c43 100644--- a/browser-plugins/test/write-safety.test.js+++ b/browser-plugins/test/write-safety.test.js@@ -1,6 +1,6 @@import assert from 'node:assert/strict'import { createServer } from 'node:http'-import { mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'+import { mkdir, mkdtemp, readFile, readdir, rm, symlink, writeFile } from 'node:fs/promises'import { tmpdir } from 'node:os'import { join } from 'node:path'import { test } from 'node:test'@@ -39,7 +39,7 @@ function pageHtml() {}async function fixtureServer() {- const counters = { submit: 0, filter: 0, hang: 0, unlabeled: 0, upload: 0, delete: 0 }+ const counters = { submit: 0, filter: 0, hang: 0, unlabeled: 0, upload: 0, delete: 0, deleteA: 0, deleteB: 0 }const sockets = new Set()const server = createServer((request, response) => {const path = new URL(request.url ?? '/', `http://${request.headers.host}`).pathname@@ -63,8 +63,16 @@ async function fixtureServer() {request.on('end', () => html('Uploaded', '<p>Uploaded</p>'))} else if (path === '/delete' && request.method === 'POST') {counters.delete += 1+ const item = new URL(request.url ?? '/', `http://${request.headers.host}`).searchParams.get('item')+ if (item === 'A') counters.deleteA += 1+ if (item === 'B') counters.deleteB += 1request.resume()request.on('end', () => { response.writeHead(204); response.end() })+ } else if (path === '/delete-items') {+ html('Delete items', `+ <div><p>Item A</p><button type="button" onclick="fetch('/delete?item=A', {method:'POST'})">Delete</button></div>+ <div><p>Item B</p><button type="button" onclick="fetch('/delete?item=B', {method:'POST'})">Delete</button></div>+ `)} else if (path === '/filter') {counters.filter += 1html('Filtered', '<p>Filtered</p>')@@ -108,11 +116,16 @@ async function fixtureServer() {}}+function findRefs(snapshot, label) {+ return snapshot.split('\n')+ .filter(value => value.match(/"([^"]*)"/)?.[1] === label && /\[ref=e\d+\]/.test(value))+ .map(line => line.match(/\[ref=(e\d+)\]/)?.[1])+}+function findRef(snapshot, label) {- const line = snapshot.split('\n').find(value => value.match(/"([^"]*)"/)?.[1] === label && /\[ref=e\d+\]/.test(value))- const match = line?.match(/\[ref=(e\d+)\]/)- assert.ok(match, `no snapshot ref for ${label}`)- return match[1]+ const ref = findRefs(snapshot, label)[0]+ assert.ok(ref, `no snapshot ref for ${label}`)+ return ref}async function waitFor(check) {@@ -123,7 +136,7 @@ async function waitFor(check) {}}-async function setup(t, actionTimeout = 500) {+async function setup(t, actionTimeout = 500, rootsForProfile) {const fixture = await fixtureServer()let profileRootlet session@@ -142,6 +155,7 @@ async function setup(t, actionTimeout = 500) {downloadDir: join(profileRoot, 'downloads'),playwrightUrl,actionTimeout,+ uploadRoots: rootsForProfile ? async () => rootsForProfile(profileRoot) : undefined,})return { fixture, profileRoot, session }}@@ -196,6 +210,35 @@ test('delete requires a checked second click even when confirm is passed first',await waitFor(() => fixture.counters.delete === 1)})+test('delete confirmation is bound to the clicked element', async (t) => {+ const { fixture, session } = await setup(t)+ const actions = []+ const gate = action => { actions.push(action) }+ let snapshot = await session.open(new URL('delete-items', fixture.url).href)+ let refs = findRefs(snapshot, 'Delete')+ assert.equal(refs.length, 2)++ let result = await gateScope.run(gate, () => session.click(refs[0]))+ assert.match(result, /This would delete "Delete".*Nothing was deleted yet/)+ assert.equal(fixture.counters.deleteA, 0)+ assert.equal(fixture.counters.deleteB, 0)++ refs = findRefs(result, 'Delete')+ result = await gateScope.run(gate, () => session.click(refs[1], { confirm: true }))+ assert.match(result, /This would delete "Delete".*Nothing was deleted yet/)+ assert.equal(fixture.counters.deleteA, 0)+ assert.equal(fixture.counters.deleteB, 0)+ assert.equal(actions.length, 0)++ refs = findRefs(result, 'Delete')+ result = await gateScope.run(gate, () => session.click(refs[1], { confirm: true }))+ await waitFor(() => fixture.counters.deleteB === 1)+ assert.equal(fixture.counters.deleteA, 0)+ assert.equal(fixture.counters.deleteB, 1)+ assert.equal(actions.length, 1)+ assert.equal(actions[0].kind, 'delete')+})+test('payment buttons are refused and card details are never typed', async (t) => {const { fixture, session } = await setup(t)const snapshot = await session.open(fixture.url)@@ -359,10 +402,16 @@ test('browser_task driver tools inherit the outer authorization gate', async (t)await waitFor(() => fixture.counters.submit === 1)})-test('uploads are gated before setting files', async (t) => {- const { fixture, profileRoot, session } = await setup(t)- const file = join(profileRoot, 'notes.txt')+test('uploads are confined to allowed roots and gated before setting files', async (t) => {+ const { fixture, profileRoot, session } = await setup(t, 500, root => [join(root, 'allowed')])+ const allowedRoot = join(profileRoot, 'allowed')+ await mkdir(allowedRoot)+ const file = join(allowedRoot, 'notes.txt')+ const outside = join(profileRoot, 'outside.txt')await writeFile(file, 'hello')+ await writeFile(outside, 'private')+ const linked = join(allowedRoot, 'outside-link.txt')+ await symlink(outside, linked)const actions = []let reject = trueconst gate = action => {@@ -372,9 +421,20 @@ test('uploads are gated before setting files', async (t) => {const snapshot = await session.open(fixture.url)const ref = findRef(snapshot, 'Upload file')+ await assert.rejects(+ gateScope.run(gate, () => session.upload(ref, [outside])),+ /Uploads must come from .*outside\.txt is outside them\. Nothing was uploaded\./,+ )+ await assert.rejects(+ gateScope.run(gate, () => session.upload(ref, [linked])),+ /Uploads must come from .*outside-link\.txt is outside them\. Nothing was uploaded\./,+ )+ assert.equal(actions.length, 0)+await assert.rejects(gateScope.run(gate, () => session.upload(ref, [file])), /approval denied/)assert.equal(actions.length, 1)assert.equal(actions[0].kind, 'upload')+ assert.equal(actions[0].label, 'notes.txt')reject = falseconst result = await gateScope.run(gate, () => session.upload(ref, [file]))assert.match(result, /notes\.txt/)@@ -382,6 +442,29 @@ test('uploads are gated before setting files', async (t) => {assert.equal(fixture.counters.upload, 0)})+test('standalone uploads without configured roots are refused', async (t) => {+ const { fixture, profileRoot, session } = await setup(t)+ const file = join(profileRoot, 'notes.txt')+ await writeFile(file, 'hello')+ const snapshot = await session.open(fixture.url)+ await assert.rejects(+ session.upload(findRef(snapshot, 'Upload file'), [file]),+ /Uploads must come from an allowed folder.*Nothing was uploaded\./,+ )+ assert.doesNotMatch(await session.snapshotText(), /notes\.txt/)+})++test('opening a direct attachment URL saves the download instead of throwing', async (t) => {+ const { fixture, profileRoot, session } = await setup(t)+ const actions = []+ const result = await gateScope.run(action => { actions.push(action) }, () => session.open(new URL('download', fixture.url).href))+ assert.match(result, /Downloaded "report\.txt" to .*report\.txt\./)+ assert.equal(actions.length, 1)+ assert.equal(actions[0].kind, 'download')+ assert.equal(actions[0].host, '127.0.0.1')+ assert.equal(await readFile(join(profileRoot, 'downloads', 'report.txt'), 'utf8'), 'download contents')+})+test('downloads are gated, saved without overwriting, or cancelled on refusal', async (t) => {const { fixture, profileRoot, session } = await setup(t)const downloadDir = join(profileRoot, 'downloads')diff --git a/docs/plans/2026-10-21-browser-plugins.md b/docs/plans/2026-10-21-browser-plugins.mdindex b8854294..ba74a866 100644--- a/docs/plans/2026-10-21-browser-plugins.md+++ b/docs/plans/2026-10-21-browser-plugins.md@@ -19,8 +19,9 @@ Bot 是长期身份。登录应该跟着 Bot 走,而不是跟着某一段对- 页面正文仍然是数据,不当成用户的指令。- 浏览、点击、输入、选择、筛选、登录表单和普通设置默认放行,不按 DSH 权限预设分级。- 只有发帖、上传、下载、删除需要用户授权。Bot 在设置的「浏览器」页获得匹配网站和操作类型的预授权时直接执行;否则走 DSH 审批卡片,审批只允许本次操作。预授权可设过期时间。-- 删除无论有没有预授权,都先返回核对快照;Bot 必须在检查目标后用 `browser_click` 的 `confirm: true` 再点一次,且页面 URL 和标签都必须匹配,才进入正常授权流程。+- 删除无论有没有预授权,都先返回核对快照;Bot 必须在检查目标后用 `browser_click` 的 `confirm: true` 再点同一个元素,且页面 URL 和标签都必须匹配,才进入正常授权流程。- 付款、购买、转账等操作永远不由 Bot 执行;付款详情也不由 Bot 输入。Bot 调用 `browser_handoff`(或脚本里的 `handoff`),由用户完成。登录或验证码也可交给用户;交接后点击、输入、打开页面都会停住,直到 `browser_resume`。不另设域名名单,也不拦截内网地址。+- 上传只允许使用 Bot 的工作目录或 `browser-downloads/<botId>/` 中的文件,链接目标在校验前解析。- 已授权的发帖、上传、下载、删除若结果未知,会按来源记录,要求先用快照核实;页面仍未观察前,不允许重复同来源的操作。下载文件保存到 Bot 独立的 `browser-downloads/<botId>/` 目录。- 三个插件:- `refs`:打开、快照、按编号点击和填写、截图。每一步留在会话里,可以核对。快照会读进开放的 Shadow DOM 和同源 iframe。diff --git a/ds-bot/src/host/browser-plugins.js b/ds-bot/src/host/browser-plugins.jsindex fee8f137..a3b1de92 100644--- a/ds-bot/src/host/browser-plugins.js+++ b/ds-bot/src/host/browser-plugins.js@@ -89,11 +89,18 @@ export function install(rt) {const headless = typeof rt.config.browserHeadless === 'boolean'? rt.config.browserHeadless: !process.env.DISPLAY+ const downloadRoot = join(rt.home, 'browser-downloads')mountBrowserPlugin(rt.ctx, {toolDefs: plugin.tools ?? plugin.toolDefs,defineTool,profileRoot: join(rt.home, 'browser-profiles'),- downloadRoot: join(rt.home, 'browser-downloads'),+ downloadRoot,+ async uploadRoots(botId) {+ await rt.load()+ const requested = rt.workDirOf({ botId })+ const working = requested !== undefined && await rt.isFolder(requested) ? requested : rt.teamFolder()+ return [working, join(downloadRoot, botId)].filter(Boolean)+ },headless,playwrightUrl,executablePath,diff --git a/ds-bot/src/host/browser/mount.js b/ds-bot/src/host/browser/mount.jsindex 5fc4abfb..fb3888dd 100644--- a/ds-bot/src/host/browser/mount.js+++ b/ds-bot/src/host/browser/mount.js@@ -18,9 +18,9 @@ export const browserOutput = {/*** @param {any} ctx- * @param {{ toolDefs: any[], defineTool: Function, keyOf: (exec: any) => Promise<string|undefined>, profileRoot: string, downloadRoot?: string, headless: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number, authorize?: (exec: any, botId: string, action: any) => Promise<void>, complete?: Function }} options+ * @param {{ toolDefs: any[], defineTool: Function, keyOf: (exec: any) => Promise<string|undefined>, profileRoot: string, downloadRoot?: string, uploadRoots?: (botId: string) => Promise<string[]>, headless: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number, authorize?: (exec: any, botId: string, action: any) => Promise<void>, complete?: Function }} options*/-export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRoot, downloadRoot, headless, playwrightUrl, executablePath, actionTimeout, authorize, complete }) {+export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRoot, downloadRoot, uploadRoots, headless, playwrightUrl, executablePath, actionTimeout, authorize, complete }) {const sessions = new Map()ctx.effect(() => async () => {const open = [...sessions.values()]@@ -47,6 +47,7 @@ export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRoplaywrightUrl,executablePath,actionTimeout,+ uploadRoots: uploadRoots ? () => uploadRoots(botId) : undefined,})sessions.set(botId, session)}diff --git a/ds-bot/src/host/browser/session.js b/ds-bot/src/host/browser/session.jsindex bd039f64..21f4db9b 100644--- a/ds-bot/src/host/browser/session.js+++ b/ds-bot/src/host/browser/session.js@@ -1,8 +1,9 @@// One Chromium profile per Bot. A Bot's own chat and every group Session it speaks in// share this directory, so a new conversation part keeps the login. Another Bot never// opens it: profiles are not copied across the team.-import { mkdir, readFile, stat, writeFile } from 'node:fs/promises'-import { basename, extname, isAbsolute, join } from 'node:path'+import { randomUUID } from 'node:crypto'+import { mkdir, readFile, realpath, stat, writeFile } from 'node:fs/promises'+import { basename, extname, isAbsolute, join, relative as relativePath } from 'node:path'import { gateScope } from './gate.js'const ACTION_TIMEOUT = 5_000@@ -85,9 +86,9 @@ async function loadChromium(playwrightUrl) {}/**- * @param {{ botId: string, profileRoot: string, headless?: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number }} options+ * @param {{ botId: string, profileRoot: string, headless?: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number, uploadRoots?: () => Promise<string[]> }} options*/-export async function openBotBrowser({ botId, profileRoot, downloadDir, headless = true, playwrightUrl, executablePath, actionTimeout = ACTION_TIMEOUT }) {+export async function openBotBrowser({ botId, profileRoot, downloadDir, headless = true, playwrightUrl, executablePath, actionTimeout = ACTION_TIMEOUT, uploadRoots }) {const userDataDir = join(profileRoot, botId)await mkdir(userDataDir, { recursive: true })const chromium = await loadChromium(playwrightUrl)@@ -104,10 +105,10 @@ export async function openBotBrowser({ botId, profileRoot, downloadDir, headlessconst saved = JSON.parse(await readFile(cookieFile, 'utf8'))if (Array.isArray(saved) && saved.length > 0) await context.addCookies(saved)} catch { /* no cookies yet */ }- return createSession({ context, page, botId, userDataDir, cookieFile, downloadDir, actionTimeout })+ return createSession({ context, page, botId, userDataDir, cookieFile, downloadDir, actionTimeout, uploadRoots })}-function createSession({ context, page, botId, userDataDir, cookieFile, downloadDir, actionTimeout }) {+function createSession({ context, page, botId, userDataDir, cookieFile, downloadDir, actionTimeout, uploadRoots }) {const pages = new Set()const unknownByOrigin = new Map()const unknownPageOrigins = new WeakMap()@@ -118,6 +119,7 @@ function createSession({ context, page, botId, userDataDir, cookieFile, downloadlet lastImage = ''let activeDownloads = nulllet pendingDelete = null+ let pendingDeleteLocator = nullconst ready = () => {if (hold) throw new Error(`The browser is waiting for the user (${hold.reason}). Ask them to ${hold.note || 'take over'}, then call browser_resume.`)@@ -137,8 +139,10 @@ function createSession({ context, page, botId, userDataDir, cookieFile, downloadtarget.on('dialog', dialog => { dialog.dismiss().catch(() => {}) })target.on('crash', () => crashedPages.add(target))target.on('download', download => {- if (activeDownloads) activeDownloads.items.push({ download, page: target })- else download.cancel().catch(() => {})+ if (activeDownloads) {+ activeDownloads.items.push({ download, page: target })+ activeDownloads.notify?.()+ } else download.cancel().catch(() => {})})target.on('close', () => {pages.delete(target)@@ -254,13 +258,28 @@ function createSession({ context, page, botId, userDataDir, cookieFile, download}async function authorizeWrite(action, gate = gateScope.getStore()) {- if (action.kind !== 'delete') pendingDelete = null+ if (action.kind !== 'delete') await clearPendingDelete()const previous = unknownByOrigin.get(action.origin)if (previous && !previous.observed) throw pendingWriteError(previous)if (previous) unknownByOrigin.delete(action.origin)await gate?.(action)}+ async function clearPendingDelete() {+ const pending = pendingDelete+ const locator = pendingDeleteLocator+ pendingDelete = null+ pendingDeleteLocator = null+ if (pending && locator) {+ await locator.evaluate((el, nonce) => {+ if (el.getAttribute('data-dsbot-delete-check') === nonce) el.removeAttribute('data-dsbot-delete-check')+ }, pending.nonce).catch(error => {+ // Navigation may have replaced the target since the confirmation snapshot.+ return error+ })+ }+ }+function paymentError(action) {return new Error(`This looks like a payment ("${action.label}" on ${action.host}). The user makes payments themselves: call browser_handoff with reason "payment" and stop. Nothing was done.`)}@@ -288,7 +307,8 @@ function createSession({ context, page, botId, userDataDir, cookieFile, downloadconst filename = download.suggestedFilename()let actiontry {- const url = new URL(page.url())+ const pageUrl = new URL(page.url())+ const url = pageUrl.origin === 'null' ? new URL(download.url()) : pageUrlaction = { kind: 'download', label: filename.slice(0, 80), origin: url.origin, host: url.hostname }await authorizeWrite(action, gate)} catch (error) {@@ -319,14 +339,28 @@ function createSession({ context, page, botId, userDataDir, cookieFile, downloadasync function captureAction(perform) {const parent = activeDownloads- const batch = parent ?? { gate: gateScope.getStore(), items: [] }+ const batch = parent ?? { gate: gateScope.getStore(), items: [], notify: undefined }if (!parent) activeDownloads = batchlet resultlet failuretry {- result = await perform()- } catch (error) {- failure = error+ try {+ result = await perform()+ } catch (error) {+ failure = error+ }+ if (!parent && failure && batch.items.length === 0+ && String(failure?.message ?? failure).includes('Download is starting')) {+ await new Promise(resolve => {+ const finish = () => {+ clearTimeout(timer)+ batch.notify = undefined+ resolve()+ }+ const timer = setTimeout(finish, Math.max(actionTimeout, ACTION_TIMEOUT))+ batch.notify = finish+ })+ }} finally {if (!parent) activeDownloads = null}@@ -336,6 +370,10 @@ function createSession({ context, page, botId, userDataDir, cookieFile, download}const messages = []for (const item of batch.items) messages.push(await finishDownload(item, batch.gate))+ if (failure && batch.items.length > 0) {+ const snapshot = await snapshotText().catch(() => '')+ return snapshot ? `${snapshot}\n\n${messages.join('\n')}` : messages.join('\n')+ }if (failure) throw failurereturn appendDownloadResults(result, messages)}@@ -457,18 +495,18 @@ function createSession({ context, page, botId, userDataDir, cookieFile, downloadurl: () => activePage()?.url() ?? '',async open(url) {ready()- pendingDelete = null+ await clearPendingDelete()const target = activePage()return finishAction(() => target.goto(String(url), { waitUntil: 'domcontentloaded', timeout: 30_000 }), target)},tabs,async selectTab(index) {ready()- pendingDelete = nullconst open = openPages()if (!Number.isInteger(index) || index < 0 || index >= open.length) {throw new Error(`No tab at index ${index}.`)}+ await clearPendingDelete()current = open[index]await settle()return snapshotText()@@ -480,6 +518,7 @@ function createSession({ context, page, botId, userDataDir, cookieFile, downloadthrow new Error(`No tab at index ${index}.`)}if (open.length === 1) throw new Error('Cannot close the last browser tab.')+ await clearPendingDelete()await open[index].close()await settle()return snapshotText()@@ -512,16 +551,21 @@ function createSession({ context, page, botId, userDataDir, cookieFile, downloadthrow error}if (action?.kind === 'payment') {- pendingDelete = null+ await clearPendingDelete()throw paymentError(action)}if (action?.kind === 'delete') {const pending = pendingDelete- if (!(confirm && pending?.url === target.url() && pending.label === action.label)) {- pendingDelete = { url: target.url(), label: action.label }+ const elementNonce = await locator.getAttribute('data-dsbot-delete-check')+ if (!(confirm && pending?.url === target.url() && pending.label === action.label && elementNonce === pending.nonce)) {+ await clearPendingDelete()+ const nonce = randomUUID()+ await locator.evaluate((el, value) => el.setAttribute('data-dsbot-delete-check', value), nonce)+ pendingDelete = { url: target.url(), label: action.label, nonce }+ pendingDeleteLocator = locatorreturn `This would delete "${action.label}" on ${action.host}. Nothing was deleted yet. Check in this snapshot that it is the right item, then call browser_click again on it with confirm: true.\n\n${await snapshotText()}`}- pendingDelete = null+ await clearPendingDelete()}if (action) return performWrite(action, target, () => locator.click({ timeout: actionTimeout }))return finishAction(() => locator.click({ timeout: actionTimeout }))@@ -571,16 +615,32 @@ function createSession({ context, page, botId, userDataDir, cookieFile, downloadif (!Array.isArray(paths) || paths.length === 0 || paths.length > 10) {throw new Error('Upload requires between 1 and 10 file paths.')}+ const roots = await uploadRoots?.() ?? []+ if (roots.length === 0) {+ throw new Error(`Uploads must come from an allowed folder. ${paths[0]} is outside them. Nothing was uploaded.`)+ }+ if (downloadDir) await mkdir(downloadDir, { recursive: true })+ const realRoots = await Promise.all(roots.map(root => realpath(root)))const files = []+ const labels = []for (const path of paths) {if (typeof path !== 'string' || !isAbsolute(path)) throw new Error('Each upload path must be absolute.')const info = await stat(path)if (!info.isFile()) throw new Error(`Upload path is not a regular file: ${path}`)- files.push(path)+ const realFile = await realpath(path)+ const root = realRoots.find(candidate => {+ const relative = relativePath(candidate, realFile)+ return !relative.startsWith('..') && !isAbsolute(relative)+ })+ if (!root) {+ throw new Error(`Uploads must come from ${realRoots.join(', ')}. ${path} is outside them. Nothing was uploaded.`)+ }+ files.push(realFile)+ labels.push(relativePath(root, realFile))}const { target, locator } = await refLocator(ref)const url = new URL(target.url())- const label = files.map(path => basename(path)).join(', ').slice(0, 80) || 'uploaded files'+ const label = labels.join(', ').slice(0, 80) || 'uploaded files'await authorizeWrite({ kind: 'upload', label, origin: url.origin, host: url.hostname })return finishAction(() => locator.setInputFiles(files, { timeout: actionTimeout }), target)},@@ -592,7 +652,7 @@ function createSession({ context, page, botId, userDataDir, cookieFile, download},async back() {ready()- pendingDelete = null+ await clearPendingDelete()const target = activePage()return finishAction(() => target.goBack({ waitUntil: 'domcontentloaded', timeout: 15_000 }).catch(() => {}), target)},