Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3275.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A capability's stored failure reason no longer repeats a credential the user typed into the capability itself: when a server echoes a request header or an environment value back in its own error body, the value of a name that promises a credential (`key`, `token`, `secret`, `auth`, `password`, `passwd`, `credential`, `signature`, `cookie`, `session`, `bearer`, and `pat` as a whole segment — `X-Github-Pat` yes, `X-Patch-Id` no) is masked before the reason is stored, longest value first so one credential is never left as a surviving tail, so it can no longer be printed into the Bot's system prompt every turn ([#3275](https://github.com/ThinkFlowLab/FlyBot/issues/3275)).
26 changes: 25 additions & 1 deletion core/src/host/capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ export function expandCapabilityNames(capabilities, library) {
// system prompt every turn, so userinfo is dropped before the sentence is stored.
const withoutUserinfo = text => text.replace(/([a-z][a-z0-9+.-]*:\/\/)[^/\s@]+@/gi, '$1')

// #3275: a failing transport builds its error from the server's own response body, so a server that
// echoes a request header writes a value the user configured into the stored reason the system
// prompt prints every turn. `redactSecrets` replaces saved key values only, so a header or
// environment name that promises a credential masks its own value wherever the body repeats it. A
// value under six characters is left alone: replacing it everywhere would eat the words around it.
// The literal is not refused at input instead — it is legal today, an entry saved before this exists
// in the field, and what such a value may be kept at rest is the separate open question in #3276.
// `pat` counts as a whole segment: `X-Github-Pat` is a credential while `X-Patch-Id` and `X-Path-Id`
// are ordinary names that merely begin with the same three letters.
const CREDENTIAL_NAME = /(key|token|secret|auth|password|passwd|credential|signature|cookie|session|bearer)|(?<![a-z0-9])pat(?![a-z0-9])/i
const withoutCredentialValues = (text, config) => {
// Longest value first, the way `secrets.js` sorts its own before it builds one pattern: one
// credential can contain another (`Bearer abc123XYZ` carries the `abc123` of the key beside it),
// and replacing the short one first would leave the longer one's tail in the stored sentence.
// Sorting makes the answer the same whichever order the two entries were written in.
const values = Object.entries({ ...config?.headers, ...config?.env })
.filter(([name, value]) => CREDENTIAL_NAME.test(name) && typeof value === 'string' && value.length >= 6)
.map(([, value]) => value)
.sort((a, b) => b.length - a.length)
let out = text
for (const value of values) out = out.split(value).join('[hidden]')
return out
}

const same = (a, b) => JSON.stringify(a) === JSON.stringify(b)
const strings = value => {
if (value === undefined) return []
Expand Down Expand Up @@ -318,7 +342,7 @@ export function install(rt) {
} catch (error) {
await drop()
const joined = [error, error?.cause].filter(Boolean).map(part => String(part?.message ?? part)).join(': ') || 'Connection failed'
const message = withoutUserinfo(rt.redactSecrets(joined)).slice(0, 300)
const message = withoutCredentialValues(withoutUserinfo(rt.redactSecrets(joined)), config).slice(0, 300)
statusSet(bot.id, name, sessionId, { state: 'error', error: message })
}
}
Expand Down
131 changes: 131 additions & 0 deletions core/test/capabilities.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,134 @@ test('a plain HTTPS URL still saves', async t => {
assert.equal(saved.url, 'https://host/mcp')
assert.equal(env.rt.state.capabilities.items.plain.url, 'https://host/mcp')
})

// #3275: a `streamable-http` transport builds its error from the server's own response body
// (`Error POSTing to endpoint: <body>`), and a server that echoes a request header writes a value
// the user configured into a stored reason that the system prompt prints on every turn.
// `redactSecrets` replaces saved keys only, so a literal header value is masked by the name that
// promises a credential — and only that name, so an ordinary header in the same body survives.
const quoting = (name, headers) => ({
items: {
[name]: {
name, kind: 'mcp', description: 'Echo service', transport: 'streamable-http',
url: 'https://host/mcp', headers, approved: true, createdBy: 'worker', createdAt: 1, updatedAt: 1,
},
},
})

test('an error that echoes a configured header value stores it masked', async t => {
const state = team()
state.capabilities = quoting('echo', { 'X-Tenant-Key': 'abc123', Accept: 'application/json' })
const env = await setup({ state })
t.after(() => env.dispose())
const { ctx, rt, mounts, skills } = env
await rt.changeCapabilityAttachment('attach', { name: 'echo' }, 'worker')
const quoted = 'Error POSTing to endpoint: {"error":"unauthorized","echoed":"X-Tenant-Key: abc123","accept":"application/json"}'
ctx.emit('agent/created', { agent: testAgent('worker', mounts, skills, { awaitMount: async () => { throw new Error(quoted) } }) })
await rt.scheduleCapabilitiesReconcile()
const status = rt.capabilityStatusesView().worker.echo
assert.equal(status.state, 'error')
assert.equal(status.error, 'Error POSTing to endpoint: {"error":"unauthorized","echoed":"X-Tenant-Key: [hidden]","accept":"application/json"}', 'the exact stored sentence: the header name and an ordinary header survive, the credential value is gone')
assert.equal(JSON.stringify(rt.capabilityStatusesView()).includes('abc123'), false)
})

test('an error that echoes a configured environment value stores it masked', async t => {
const state = team()
state.capabilities = {
items: {
stdio: {
name: 'stdio', kind: 'mcp', description: 'Local server', transport: 'stdio', command: 'node', args: [],
env: { SERVICE_TOKEN: 'tok-abcdef', PATH_KEEP: '/usr/bin' }, approved: true, createdBy: 'worker', createdAt: 1, updatedAt: 1,
},
},
}
const env = await setup({ state })
t.after(() => env.dispose())
const { ctx, rt, mounts, skills } = env
await rt.changeCapabilityAttachment('attach', { name: 'stdio' }, 'worker')
const quoted = 'server exited with SERVICE_TOKEN=tok-abcdef'
ctx.emit('agent/created', { agent: testAgent('worker', mounts, skills, { awaitMount: async () => { throw new Error(quoted) } }) })
await rt.scheduleCapabilitiesReconcile()
const status = rt.capabilityStatusesView().worker.stdio
assert.equal(status.state, 'error')
assert.equal(status.error, 'server exited with SERVICE_TOKEN=[hidden]', 'the exact stored sentence')
assert.equal(JSON.stringify(rt.capabilityStatusesView()).includes('tok-abcdef'), false)
})

// #3275, from the security read at `57bdaa6a`: a cookie is a whole session credential, and the names
// that promise one have to say so — a server that echoes `Cookie: sid=…` otherwise writes the session
// into the prompt every turn.
test('an error that echoes a cookie value stores the whole session credential masked', async t => {
const state = team()
state.capabilities = quoting('cookie-echo', { Cookie: 'sid=supersecretsession42', Accept: 'application/json' })
const env = await setup({ state })
t.after(() => env.dispose())
const { ctx, rt, mounts, skills } = env
await rt.changeCapabilityAttachment('attach', { name: 'cookie-echo' }, 'worker')
const quoted = 'Error POSTing to endpoint: {"error":"unauthorized","echoed":"Cookie: sid=supersecretsession42","accept":"application/json"}'
ctx.emit('agent/created', { agent: testAgent('worker', mounts, skills, { awaitMount: async () => { throw new Error(quoted) } }) })
await rt.scheduleCapabilitiesReconcile()
const status = rt.capabilityStatusesView().worker['cookie-echo']
assert.equal(status.state, 'error')
assert.equal(status.error, 'Error POSTing to endpoint: {"error":"unauthorized","echoed":"Cookie: [hidden]","accept":"application/json"}', 'the whole cookie value is gone and the ordinary header survives')
assert.equal(JSON.stringify(rt.capabilityStatusesView()).includes('supersecretsession42'), false)
})

// #3275, same read: `X-Github-Pat` is a credential while `X-Patch-Id` and `X-Path-Id` are ordinary
// names that merely begin with those three letters — so `pat` counts as a whole segment, never as a
// prefix.
test('a PAT header name is masked while "patch" and "path" names are not', async t => {
const state = team()
state.capabilities = quoting('pat-echo', {
'X-Github-Pat': 'ghp-abcdef123456', 'X-Patch-Id': 'patch-9000', 'X-Path-Id': 'path-9000', Accept: 'application/json',
})
const env = await setup({ state })
t.after(() => env.dispose())
const { ctx, rt, mounts, skills } = env
await rt.changeCapabilityAttachment('attach', { name: 'pat-echo' }, 'worker')
const quoted = 'Error POSTing to endpoint: {"pat":"X-Github-Pat: ghp-abcdef123456","patch":"X-Patch-Id: patch-9000","path":"X-Path-Id: path-9000","accept":"application/json"}'
ctx.emit('agent/created', { agent: testAgent('worker', mounts, skills, { awaitMount: async () => { throw new Error(quoted) } }) })
await rt.scheduleCapabilitiesReconcile()
const status = rt.capabilityStatusesView().worker['pat-echo']
assert.equal(status.state, 'error')
assert.equal(status.error, 'Error POSTing to endpoint: {"pat":"X-Github-Pat: [hidden]","patch":"X-Patch-Id: patch-9000","path":"X-Path-Id: path-9000","accept":"application/json"}', 'only the PAT value is masked; the two lookalike names keep their values')
assert.equal(JSON.stringify(rt.capabilityStatusesView()).includes('ghp-abcdef123456'), false)
})

// #3275, same read: one value can contain another (`Bearer abc123XYZ` carries the `abc123` of the key
// beside it), so the values are replaced longest first — the way `secrets.js` sorts its own — and the
// answer does not depend on the order the two entries were written in.
for (const [label, headers] of [
['the key first', { 'X-Api-Key': 'abc123', Authorization: 'Bearer abc123XYZ' }],
['the authorization first', { Authorization: 'Bearer abc123XYZ', 'X-Api-Key': 'abc123' }],
]) {
test(`an error that echoes two credential values masks the longer one whole, ${label}`, async t => {
const state = team()
state.capabilities = quoting('pair-echo', headers)
const env = await setup({ state })
t.after(() => env.dispose())
const { ctx, rt, mounts, skills } = env
await rt.changeCapabilityAttachment('attach', { name: 'pair-echo' }, 'worker')
const quoted = 'Error POSTing to endpoint: {"auth":"Bearer abc123XYZ"}'
ctx.emit('agent/created', { agent: testAgent('worker', mounts, skills, { awaitMount: async () => { throw new Error(quoted) } }) })
await rt.scheduleCapabilitiesReconcile()
const status = rt.capabilityStatusesView().worker['pair-echo']
assert.equal(status.state, 'error')
assert.equal(status.error, 'Error POSTing to endpoint: {"auth":"[hidden]"}', 'the longer value is replaced whole, so no tail of it survives')
assert.equal(JSON.stringify(rt.capabilityStatusesView()).includes('XYZ'), false)
})
}

test('a plain reason is stored exactly as the transport gave it', async t => {
const state = team()
state.capabilities = quoting('plain-error', {})
const env = await setup({ state })
t.after(() => env.dispose())
const { ctx, rt, mounts, skills } = env
await rt.changeCapabilityAttachment('attach', { name: 'plain-error' }, 'worker')
ctx.emit('agent/created', { agent: testAgent('worker', mounts, skills, { awaitMount: async () => { throw new Error('401 bad credentials') } }) })
await rt.scheduleCapabilitiesReconcile()
const status = rt.capabilityStatusesView().worker['plain-error']
assert.equal(status.state, 'error')
assert.equal(status.error, '401 bad credentials')
})
Loading
Loading