A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Resolve secret references in HTTP MCP capability URLs
Fei24113abcac7b6568
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c8b2fec5..bc6ec3db 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,10 @@ All notable changes to DS Bot are listed here. The format follows [Keep a Change## [Unreleased]+### Fixed++- An HTTP MCP capability with `$DSH_SECRET_<NAME>` in its URL sent the reference itself to the server and showed ready until the first tool call failed. The URL now gets the secret's value, URL-encoded, and a capability whose Bot may not use the secret shows an error. ([#125](https://github.com/Fei2411/DeepSeek-Bot/issues/125))+## [0.1.5] - 2026-10-13### Addeddiff --git a/ds-bot/README.md b/ds-bot/README.mdindex 0d8b5b23..20a40df7 100644--- a/ds-bot/README.md+++ b/ds-bot/README.md@@ -145,7 +145,7 @@ When a Bot needs an API key, token or password, it calls `request_secret`, and a- **Fill in**: the field hides what you type, is not part of a form and is not autofilled. Choose "All Bots" or "Only this Bot" and select Save. A value must have at least 4 characters (a 4-digit PIN works). A value of 8 characters or more is hidden wherever it appears; a shorter one only where no letter or digit touches it, so ordinary words stay intact.- **Allow**: if the secret is already saved but this Bot is not allowed to use it, the card only asks whether to allow it. You can also replace the value there.-- **Use**: the value goes only into the Bot's shell environment as `DSH_SECRET_<NAME>` (`$DSH_SECRET_NAME` in bash, `$env:DSH_SECRET_NAME` in PowerShell). `list_secrets` lists the names a Bot can use.+- **Use**: the value goes into the Bot's shell environment as `DSH_SECRET_<NAME>` (`$DSH_SECRET_NAME` in bash, `$env:DSH_SECRET_NAME` in PowerShell). `list_secrets` lists the names a Bot can use. An MCP capability can also take `$DSH_SECRET_<NAME>` in its env, its headers and its URL (path or query); in the URL the value is URL-encoded. Use headers when the server accepts them.- **Masking**: before a tool result is saved and before each request goes to the model, every copy of a secret value is replaced with `[secret:<NAME>]`.- **Manage**: Settings → Secrets lists every secret by name, purpose, the Bot that asked and the last use. You can change who may use it, replace the value, delete it or add one. Deleting a Bot removes it from every secret.- **Storage**: `<DSH_HOME>/bot/secrets.json`, file mode 600, written atomically. Values are never in `state.json` or logs.diff --git a/ds-bot/src/host/capabilities.js b/ds-bot/src/host/capabilities.jsindex de5f272e..e8f97661 100644--- a/ds-bot/src/host/capabilities.js+++ b/ds-bot/src/host/capabilities.js@@ -1,4 +1,5 @@import { createHash } from 'node:crypto'+import { encodeUrlComponent } from './secrets.js'const MCP_CLIENT = '@deepseek-ai/dsh-mcp-client'const NAME = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/@@ -246,22 +247,23 @@ export function install(rt) {}return pluginPromise}+ function secretFor(name, botId) {+ const value = rt.canUseSecret(name, botId) ? rt.secretValue(name) : undefined+ if (value === undefined) throw new Error(`Secret ${name} is missing or unavailable for this Bot.`)+ return value+ }function resolveValues(values, botId) {const resolved = {}for (const [key, raw] of Object.entries(values ?? {})) {- resolved[key] = String(raw).replace(SECRET_REFERENCE, (_match, name) => {- if (!rt.canUseSecret(name, botId)) throw new Error(`Secret ${name} is missing or unavailable for this Bot.`)- const value = rt.secretValue(name)- if (value === undefined) throw new Error(`Secret ${name} is missing or unavailable for this Bot.`)- return value- })+ resolved[key] = String(raw).replace(SECRET_REFERENCE, (_match, name) => secretFor(name, botId))}return resolved}+ const resolveUrl = (url, botId) => String(url).replace(SECRET_REFERENCE, (_match, name) => encodeUrlComponent(secretFor(name, botId)))const configFor = (item, botId) => {if (item.kind === 'skill') return { name: item.name, description: item.description, content: item.content }if (item.transport === 'stdio') return { transport: 'stdio', serverName: item.name, command: item.command, args: item.args ?? [], env: resolveValues(item.env, botId), failOnStartupError: true }- return { transport: 'streamable-http', serverName: item.name, url: item.url, headers: resolveValues(item.headers, botId), failOnStartupError: true }+ return { transport: 'streamable-http', serverName: item.name, url: resolveUrl(item.url, botId), headers: resolveValues(item.headers, botId), failOnStartupError: true }}const disposeMount = async mount => {if (mount?.fiber) await Promise.resolve().then(() => mount.fiber.dispose()).catch(() => {})diff --git a/ds-bot/src/host/secrets.js b/ds-bot/src/host/secrets.jsindex 54f8683d..2813a29d 100644--- a/ds-bot/src/host/secrets.js+++ b/ds-bot/src/host/secrets.js@@ -27,8 +27,13 @@ export function normalizeName(raw) {return String(raw ?? '').trim().replace(/^\$(env:)?/i, '').replace(/^DSH_SECRET_/i, '').replace(/[\s-]+/g, '_').toUpperCase()}+// encodeURIComponent leaves ' as is, but new URL() turns it into %27 in a query, so the+// URL sent out would hold a form the redactor does not know. Encoding these characters+// too keeps the sent URL equal to the text the redactor looks for.+export const encodeUrlComponent = value => encodeURIComponent(value).replace(/[!'()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase())+// One function that replaces every known value in a string, longest first so a value-// that contains another is replaced whole. JSON-escaped forms are covered too.+// that contains another is replaced whole. JSON-escaped and URL-encoded forms are covered too.export function redactor(entries) {const pairs = []for (const { name, value } of entries) {@@ -38,6 +43,9 @@ export function redactor(entries) {pairs.push([value, label, short])const escaped = JSON.stringify(value).slice(1, -1)if (escaped !== value) pairs.push([escaped, label, short])+ // encodeURIComponent throws on a lone surrogate; such a value cannot sit in a URL anyway.+ const encoded = value.isWellFormed() ? encodeUrlComponent(value) : value+ if (encoded !== value && encoded !== escaped) pairs.push([encoded, label, short])}pairs.sort((a, b) => b[0].length - a[0].length)if (pairs.length === 0) return text => textdiff --git a/ds-bot/src/host/tools.js b/ds-bot/src/host/tools.jsindex ffaf7827..2d982b8d 100644--- a/ds-bot/src/host/tools.js+++ b/ds-bot/src/host/tools.js@@ -532,7 +532,7 @@ export function install(rt) {command: { type: 'string', description: 'Program that starts a local MCP server' },args: { type: 'array', items: { type: 'string' }, description: 'Arguments for command' },env: { type: 'object', additionalProperties: true, description: 'Environment variables for command; write $DSH_SECRET_<NAME> to insert a saved secret instead of the value' },- url: { type: 'string', description: 'MCP server URL' },+ url: { type: 'string', description: 'MCP server URL; write $DSH_SECRET_<NAME> in the path or query to insert a saved secret, URL-encoded. Prefer headers for keys' },headers: { type: 'object', additionalProperties: true, description: 'HTTP headers; write $DSH_SECRET_<NAME> to insert a saved secret instead of the value' },content: { type: 'string', description: 'Skill instructions' },items: { type: 'array', items: { type: 'string' }, description: 'MCP server and skill names in the pack' },diff --git a/ds-bot/test/capabilities.test.js b/ds-bot/test/capabilities.test.jsindex bbcb8ec9..39a96156 100644--- a/ds-bot/test/capabilities.test.js+++ b/ds-bot/test/capabilities.test.js@@ -163,7 +163,7 @@ test('capability tool descriptions and attach results explain approval', async tcommand: 'Program that starts a local MCP server',args: 'Arguments for command',env: 'Environment variables for command; write $DSH_SECRET_<NAME> to insert a saved secret instead of the value',- url: 'MCP server URL',+ url: 'MCP server URL; write $DSH_SECRET_<NAME> in the path or query to insert a saved secret, URL-encoded. Prefer headers for keys',headers: 'HTTP headers; write $DSH_SECRET_<NAME> to insert a saved secret instead of the value',content: 'Skill instructions',},@@ -425,3 +425,51 @@ test('mounts agent-only MCP and skills, remounts changed config, resolves scopedassert.equal(mounts.findLast(mount => mount.sessionId === 'chief' && mount.plugin.inject?.includes('skills')).disposed, true)assert.equal(rt.capabilityStatusesView().chief.team, undefined)})++const exaSecret = (value, scope = ['chief']) => ({ name: 'EXA_API_KEY', value, purpose: '', scope, requestedBy: null, createdAt: 1, updatedAt: 1 })+async function mountUrlCapability(t, { value, url, bots = ['chief'], awaitMount }) {+ const env = await setup({ secret: exaSecret(value) })+ t.after(() => env.dispose())+ const { ctx, rt, mounts, skills } = env+ await rt.capabilitySave({ name: 'exa', kind: 'mcp', description: '', transport: 'streamable-http', url, headers: {} }, { botId: 'chief' })+ for (const bot of bots) await rt.changeCapabilityAttachment('attach', { name: 'exa', bot: rt.state.bots[bot].name }, 'chief')+ for (const bot of bots) ctx.emit('agent/created', { agent: testAgent(bot, mounts, skills, { awaitMount }) })+ await rt.scheduleCapabilitiesReconcile()+ const mountFor = bot => mounts.find(mount => mount.sessionId === bot && mount.config?.serverName === 'exa')+ return { ...env, mountFor }+}++test('secret references in an HTTP MCP URL are resolved, and an unavailable one is an error', async t => {+ const env = await mountUrlCapability(t, { value: 'fake-exa-key', url: 'https://mcp.exa.test/mcp?apiKey=$DSH_SECRET_EXA_API_KEY', bots: ['chief', 'worker'] })+ const { rt, mountFor } = env+ assert.equal(mountFor('chief').config.url, 'https://mcp.exa.test/mcp?apiKey=fake-exa-key')+ assert.equal(rt.capabilityStatusesView().chief.exa.state, 'ready')+ assert.equal(JSON.stringify(rt.state.capabilities).includes('fake-exa-key'), false)+ assert.equal(mountFor('worker'), undefined)+ assert.equal(rt.capabilityStatusesView().worker.exa.state, 'error')+ assert.match(rt.capabilityStatusesView().worker.exa.error, /EXA_API_KEY/)+})++test('a secret in an HTTP MCP URL query is encoded as one parameter value', async t => {+ const value = "mock key/+&?=#'!()* 中文"+ const env = await mountUrlCapability(t, { value, url: 'https://mcp.exa.test/mcp?apiKey=$DSH_SECRET_EXA_API_KEY&mode=test' })+ const url = new URL(env.mountFor('chief').config.url)+ assert.equal(url.searchParams.get('apiKey'), value)+ assert.equal(url.searchParams.get('mode'), 'test')+ assert.equal([...url.searchParams].length, 2)+ const saved = JSON.stringify(env.rt.state.capabilities)+ assert.equal(saved.includes(value), false)+ assert.equal(saved.includes(encodeURIComponent(value)), false)+})++test('a failed HTTP MCP mount does not show the encoded secret from its URL', async t => {+ const value = "mock key/+&?=#'!()* 中文"+ const env = await mountUrlCapability(t, {+ value,+ url: 'https://mcp.exa.test/mcp?apiKey=$DSH_SECRET_EXA_API_KEY',+ awaitMount: async mount => { throw new Error('Connection failed', { cause: new Error(`fetch ${mount.config.url}`) }) },+ })+ const status = env.rt.capabilityStatusesView().chief.exa+ assert.equal(status.state, 'error')+ assert.equal(status.error, 'Connection failed: fetch https://mcp.exa.test/mcp?apiKey=[secret:EXA_API_KEY]')+})diff --git a/ds-bot/test/secrets.test.js b/ds-bot/test/secrets.test.jsindex 668c2c56..5a3a334f 100644--- a/ds-bot/test/secrets.test.js+++ b/ds-bot/test/secrets.test.js@@ -5,7 +5,7 @@ import { tmpdir } from 'node:os'import { join } from 'node:path'import { test } from 'node:test'import { apply } from '../index.js'-import { canUse, normalizeName, redactDeep, redactor, SECRET_NAME } from '../src/host/secrets.js'+import { canUse, encodeUrlComponent, normalizeName, redactDeep, redactor, SECRET_NAME } from '../src/host/secrets.js'import { fakeContext } from './fake-ctx.js'// Test values are random on every run, so a leak can only come from this run.@@ -44,6 +44,15 @@ test('redaction replaces the longest value first, JSON-escaped forms too, and skassert.equal(redactDeep(clean, redact), clean)})+test('redaction replaces URL-encoded values', () => {+ const value = `${fresh()} /+&?=#'!()* 中文`+ const redact = redactor([{ name: 'TOKEN', value }])+ assert.equal(redact(`https://example.test/mcp?key=${encodeUrlComponent(value)}&mode=1`), 'https://example.test/mcp?key=[secret:TOKEN]&mode=1')+ assert.equal(redact(new URL(`https://example.test/mcp?key=${encodeUrlComponent(value)}`).href), 'https://example.test/mcp?key=[secret:TOKEN]')+ const broken = `${fresh()}\uD800`+ assert.equal(redactor([{ name: 'BROKEN', value: broken }])(`x ${broken}`), 'x [secret:BROKEN]')+})+test('a short key (4 to 7 characters) is replaced only where it stands alone', () => {const redact = redactor([{ name: 'PIN', value: '4821' }, { name: 'PW', value: 'cat$1' }, { name: 'SECRET_WORD', value: 'secret' }])assert.equal(redact('pin 4821, again "4821"'), 'pin [secret:PIN], again "[secret:PIN]"')