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/3288.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 when two credential flags sit next to each other: `--api-key --api-key sk-live-…`, `--api-key --token sk-live-…` and `--api-key --token=sk-live-…` now read the second flag on its own terms, so the literal behind it is masked with `[hidden]` before the stored reason is cut at 300 characters, instead of the flag name being taken as the first flag's value and the literal left in the sentence every turn. The accepted text cost is unchanged where no credential flag follows: `--api-key --verbose` still stores `--api-key [hidden]`, and a `command` or `args` token that is one bare word is still not read as a literal ([#3288](https://github.com/ThinkFlowLab/FlyBot/issues/3288)).
13 changes: 11 additions & 2 deletions core/src/host/capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ const withoutUserinfo = text => text.replace(/([a-z][a-z0-9+.-]*:\/\/)[^/\s@]+@/
// #3281: a `command` holding a whole command line is split on whitespace and read the same way, while
// a `command` that is a single bare token stays unmasked, the same limit as a bare `args` token. After
// a credential-looking flag the next token is the value even when it begins with `-`, so
// `--api-key --verbose` stores `--api-key [hidden]` and a second `--api-key` is masked as a value;
// that over-mask is the accepted cost.
// `--api-key --verbose` stores `--api-key [hidden]`; that over-mask is the accepted cost.
// #3288: a token that is itself a credential flag is not that value — `--api-key --api-key sk-live-…`
// and `--api-key --token sk-live-…` are read on their own, so the literal behind the second flag is
// collected too instead of being skipped with the name.
const CREDENTIAL_NAME = /(key|token|secret|auth|password|passwd|credential|signature|cookie|session|bearer|hmac)|(?<![a-z0-9])(pat|sig|pass)(?![a-z0-9])/i
const FLAG = /^--?([\w-]+)$/
const INLINE_FLAG = /^--?([^=]+)=(.*)$/
Expand All @@ -87,6 +89,13 @@ const withoutCredentialValues = (text, config) => {
const flag = FLAG.exec(token)
const next = argv[index + 1]
if (!flag || !CREDENTIAL_NAME.test(flag[1]) || typeof next !== 'string') continue
// #3288: a second credential flag is not a value. Reading it on its own is what collects the
// literal behind it; swallowing it skipped that token and left the literal in the sentence.
const nextFlag = FLAG.exec(next)
const nextInline = INLINE_FLAG.exec(next)
const nextIsCredentialFlag = Boolean((nextFlag && CREDENTIAL_NAME.test(nextFlag[1]))
|| (nextInline && CREDENTIAL_NAME.test(nextInline[1])))
if (nextIsCredentialFlag) continue
collect(flag[1], next)
index += 1
}
Expand Down
88 changes: 88 additions & 0 deletions core/test/capabilities.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -925,3 +925,91 @@ test('a flag directly after a credential flag is masked as its value', async t =
assert.equal(status.state, 'error')
assert.equal(status.error, 'server exited: node server.js --api-key [hidden]', 'the accepted cost: a following flag is masked')
})

// #3288: two credential flags in a row (`--api-key --api-key sk-live-…`) are how a pasted command
// line repeats an option. Read from the first flag alone, the second flag name is taken as its value
// and the scan skips it, so the literal behind it is never collected and the stored reason quotes it.
// A token that is itself a credential flag is therefore read on its own instead of swallowed.
test('a repeated credential flag leaves the literal behind it masked', async t => {
const state = team()
state.capabilities = {
items: {
repeated: {
name: 'repeated', kind: 'mcp', description: 'Local server', transport: 'stdio', command: 'node',
args: ['server.js', '--api-key', '--api-key', 'sk-live-abcdef123456'],
env: {}, approved: true, createdBy: 'worker', createdAt: 1, updatedAt: 1,
},
},
}
state.bots.worker.capabilities = ['repeated']
const warnings = []
const env = await setup({ state, beforeLoad: ({ ctx }) => { ctx.logger.warn = (...args) => warnings.push(args.join(' ')) } })
t.after(() => env.dispose())
const { ctx, rt, mounts, skills } = env
const quoted = 'spawn node ENOENT: node server.js --api-key --api-key sk-live-abcdef123456'
ctx.emit('agent/created', { agent: testAgent('worker', mounts, skills, { awaitMount: async () => { throw new Error(quoted) } }) })
await rt.scheduleCapabilitiesReconcile()
const masked = 'spawn node ENOENT: node server.js --api-key --api-key [hidden]'
const surfaces = await surfacesFor(env, 'repeated', warnings)
for (const [surface, text] of Object.entries(surfaces)) {
assert.equal(text.includes('sk-live-abcdef123456'), false, `${surface} carries no literal`)
}
const status = rt.capabilityStatusesView().worker.repeated
assert.equal(status.state, 'error')
assert.equal(status.error, masked, 'the literal behind the repeated flag is masked')
assert.equal(surfaces.listed.includes(masked), true)
assert.equal(surfaces.prompt.includes(masked), true)
})

// #3288: the same read with a different credential flag next door (`--api-key --token sk-live-…`).
// The second flag name is not a value either, so its own value is collected and the sentence keeps
// two flag names and one masked literal.
test('a different credential flag after a credential flag keeps its own value masked', async t => {
const state = team()
state.capabilities = {
items: {
pair: {
name: 'pair', kind: 'mcp', description: 'Local server', transport: 'stdio', command: 'node',
args: ['server.js', '--api-key', '--token', 'sk-live-abcdef123456'],
env: {}, approved: true, createdBy: 'worker', createdAt: 1, updatedAt: 1,
},
},
}
state.bots.worker.capabilities = ['pair']
const env = await setup({ state })
t.after(() => env.dispose())
const { ctx, rt, mounts, skills } = env
const quoted = 'spawn node server.js ENOENT: --api-key --token sk-live-abcdef123456'
ctx.emit('agent/created', { agent: testAgent('worker', mounts, skills, { awaitMount: async () => { throw new Error(quoted) } }) })
await rt.scheduleCapabilitiesReconcile()
const status = rt.capabilityStatusesView().worker.pair
assert.equal(status.state, 'error')
assert.equal(status.error, 'spawn node server.js ENOENT: --api-key --token [hidden]', 'the second flag is read as a flag')
assert.equal(JSON.stringify(rt.capabilityStatusesView()).includes('sk-live-abcdef123456'), false)
})

// #3288: the `--flag=value` form of the same pair. The inline flag is read on its own, so its value
// is collected instead of the whole token being stored as a value that was never masked.
test('a credential flag written as flag=value after a credential flag keeps its value masked', async t => {
const state = team()
state.capabilities = {
items: {
inline: {
name: 'inline', kind: 'mcp', description: 'Local server', transport: 'stdio', command: 'node',
args: ['server.js', '--api-key', '--token=sk-live-abcdef123456'],
env: {}, approved: true, createdBy: 'worker', createdAt: 1, updatedAt: 1,
},
},
}
state.bots.worker.capabilities = ['inline']
const env = await setup({ state })
t.after(() => env.dispose())
const { ctx, rt, mounts, skills } = env
const quoted = 'spawn node ENOENT: --api-key --token=sk-live-abcdef123456'
ctx.emit('agent/created', { agent: testAgent('worker', mounts, skills, { awaitMount: async () => { throw new Error(quoted) } }) })
await rt.scheduleCapabilitiesReconcile()
const status = rt.capabilityStatusesView().worker.inline
assert.equal(status.state, 'error')
assert.equal(status.error, 'spawn node ENOENT: --api-key --token=[hidden]', 'an inline credential flag keeps its value masked')
assert.equal(JSON.stringify(rt.capabilityStatusesView()).includes('sk-live-abcdef123456'), false)
})
Loading
Loading