Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Refuse a Bot's photo from DSH's or the team's data

Fei241162a777a8d401
diff --git a/ds-bot/src/host/avatar.js b/ds-bot/src/host/avatar.js
index 0841c6bf..78de0137 100644
--- a/ds-bot/src/host/avatar.js
+++ b/ds-bot/src/host/avatar.js
@@ -2,7 +2,7 @@
// into the data URL a Bot keeps in avatar.image. There is no image library in the
// host, so images are not resized; a small square image is asked for, and every kind
// of input is capped at what the stored format holds.
-import { readFile, stat } from 'node:fs/promises'
+import { readFile, realpath, stat } from 'node:fs/promises'
import { homedir } from 'node:os'
import { join, resolve } from 'node:path'
import { MAX_AVATAR_IMAGE } from './constants.js'
@@ -44,7 +44,7 @@ const expandHome = (text) => {
return text
}
-async function readLocal(path, baseDir) {
+async function readLocal(path, baseDir, isData) {
const full = resolve(baseDir ?? process.cwd(), expandHome(path))
let info
try {
@@ -54,6 +54,8 @@ async function readLocal(path, baseDir) {
throw new Error(`Cannot read the image at ${full}: ${error?.code === 'EACCES' ? 'permission denied' : error.message}`)
}
if (!info.isFile()) throw new Error(`${full} is not an image file. Pass the path of an image file.`)
+ // Where a link leads decides, so a link cannot reach into DSH's data either.
+ if (await isData?.(await realpath(full))) throw new Error(`${full} is in DeepSeek Harness data, so it cannot be a Bot's photo. Pass an image from another folder.`)
if (info.size > MAX_BYTES) throw new Error(tooLarge(info.size))
let bytes
try {
@@ -80,13 +82,14 @@ function readDataUrl(value) {
}
// Returns the data URL for avatar.image, or throws with a line the user can read.
-// `baseDir` is where a relative local path starts. Links are not fetched: the host
-// would reach addresses, such as local services, that dsh's own web fetch refuses.
-export async function readAvatar(input, { baseDir } = {}) {
+// `baseDir` is where a relative local path starts, and `isData` says whether a real path
+// lies in data a Bot may not read from. Links are not fetched: the host would reach
+// addresses, such as local services, that dsh's own web fetch refuses.
+export async function readAvatar(input, { baseDir, isData } = {}) {
const value = typeof input === 'string' ? input.trim() : ''
if (value === '') throw new Error(`Give an image path or a data URL. ${HINT}`)
if (/^data:/i.test(value)) return readDataUrl(value)
if (/^https?:\/\//i.test(value)) throw new Error('An http(s) link is not downloaded here. Save the image to a file and pass its path, or pass a data URL.')
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(value)) throw new Error(`The avatar must be a local image path or a data URL. ${HINT}`)
- return readLocal(value, baseDir)
+ return readLocal(value, baseDir, isData)
}
diff --git a/ds-bot/src/host/folders.js b/ds-bot/src/host/folders.js
index c7a735dc..f3691f9f 100644
--- a/ds-bot/src/host/folders.js
+++ b/ds-bot/src/host/folders.js
@@ -67,6 +67,15 @@ export function install(rt) {
return false
}
+ // A file a Bot names to read into the team, such as a photo, may not come from DSH's or
+ // the team's data; the team workspace, which can lie in it, is the exception.
+ async function inData(path) {
+ const team = await realTeam()
+ if (team !== undefined && within(path, team)) return false
+ for (const root of dataRoots()) if (within(path, await real(root))) return true
+ return false
+ }
+
// Saved as the real path, which is what a Session's cwd shows. Undefined means the
// team workspace.
async function cleanWorkDir(input) {
@@ -197,5 +206,5 @@ export function install(rt) {
}
}
- Object.assign(rt, { teamFolder, defaultTeamFolder, workDirOf, sessionFolder, isFolder, real, folderAt, cleanWorkDir, workFolder, setTeamWorkspace, sessionsOfBot, approveFolder, folderEndpoint })
+ Object.assign(rt, { teamFolder, defaultTeamFolder, workDirOf, sessionFolder, isFolder, real, folderAt, cleanWorkDir, workFolder, inData, setTeamWorkspace, sessionsOfBot, approveFolder, folderEndpoint })
}
diff --git a/ds-bot/src/host/tools.js b/ds-bot/src/host/tools.js
index 19ddb8c1..46b17489 100644
--- a/ds-bot/src/host/tools.js
+++ b/ds-bot/src/host/tools.js
@@ -264,7 +264,7 @@ export function install(rt) {
await rt.load()
if (!rt.isMain(rt.selfOf(exec.agent?.id))) return 'Only the Main Bot can create Bots. Ask the Main Bot instead.'
try {
- const avatar = args.avatar ? await readAvatar(String(args.avatar), { baseDir: rt.sessionFolder?.(exec.agent?.session ?? exec.agent) }) : undefined
+ const avatar = args.avatar ? await readAvatar(String(args.avatar), { baseDir: rt.sessionFolder?.(exec.agent?.session ?? exec.agent), isData: rt.inData }) : undefined
const workDir = args.work_dir ? await rt.cleanWorkDir(args.work_dir) : undefined
const name = String(args.name ?? '').trim()
await rt.approveFolder(exec, { path: workDir, who: { en: `the new Bot ${name}`, zh: `新 Bot ${name} ` } })
@@ -309,7 +309,7 @@ export function install(rt) {
let image
if (args.avatar !== undefined) {
const input = typeof args.avatar === 'string' ? args.avatar.trim() : args.avatar
- image = input === null || input === '' ? null : await readAvatar(String(input), { baseDir: rt.sessionFolder?.(exec.agent?.session ?? exec.agent) })
+ image = input === null || input === '' ? null : await readAvatar(String(input), { baseDir: rt.sessionFolder?.(exec.agent?.session ?? exec.agent), isData: rt.inData })
}
const moving = typeof args.work_dir === 'string'
const workDir = moving ? await rt.cleanWorkDir(args.work_dir) : undefined
diff --git a/ds-bot/test/roster.test.js b/ds-bot/test/roster.test.js
index aca17c01..03a51925 100644
--- a/ds-bot/test/roster.test.js
+++ b/ds-bot/test/roster.test.js
@@ -831,6 +831,23 @@ test('a photo through update_bot keeps the Bot\'s other avatar fields', async (t
assert.deepEqual((await writer()).avatar, { shape: 'fox' })
})
+test('a photo in the team\'s data is refused, and one in the team workspace there is read', async (t) => {
+ const { ctx, call, stop, home } = start(undefined, { models: ['deepseek-v4-flash'] })
+ t.after(stop)
+ const chief = (await call('state', {})).value.mainBotId
+ const exec = { agent: { id: chief } }
+ const tool = avatar => ctx.registeredTools.get('update_bot').execute({ avatar }, exec)
+ writeFileSync(join(home, 'photo.png'), PNG_BYTES)
+ assert.match(await tool(join(home, 'photo.png')), /is in DeepSeek Harness data, so it cannot be a Bot's photo/)
+ assert.equal((await call('state', {})).value.bots.find(bot => bot.id === chief).avatar?.image, undefined)
+ // The default team workspace lies in the team's data, and the Bots' own files are there.
+ const workspace = ctx.workspaceRegistry.get((await call('state', {})).value.workspaceId)?.path ?? join(home, 'workspace')
+ mkdirSync(workspace, { recursive: true })
+ writeFileSync(join(workspace, 'photo.png'), PNG_BYTES)
+ assert.match(await tool(join(workspace, 'photo.png')), /^Updated /)
+ assert.equal((await call('state', {})).value.bots.find(bot => bot.id === chief).avatar.image, PNG_DATA_URL)
+})
+
test('a photo that cannot be read changes nothing, and a non-Main Bot cannot set another Bot\'s', async (t) => {
const { ctx, call, stop, sent } = start(undefined, { models: ['deepseek-v4-flash'] })
t.after(stop)