[Bug]: API key mask comes out longer than the key and reveals 8 of its 9 characters
Version
main @ c2ad42e3; multimodal/tarko/agent-server/package.json and multimodal/tarko/agent-server-next/package.json
are both 0.3.0.
Issue Description
sanitizeApiKey() is the only thing standing between a configured credential and the response of
GET /api/v1/agent/options, and it is meant to keep the masked form the same shape as the key. For keys of 9 and 10
characters it produces an 11-character string, and for a 9-character key it leaves a single character hidden:
multimodal/tarko/agent-server/src/utils/config-sanitizer.ts:111-124 (a byte-identical copy of the whole file lives at
multimodal/tarko/agent-server-next/src/utils/config-sanitizer.ts — diff reports no difference)
function sanitizeApiKey(apiKey?: string): string | undefined {
if (!apiKey) return undefined;
if (apiKey.length <= 8) {
return '*'.repeat(apiKey.length);
}
// Show first 4 and last 4 characters, mask the middle
const start = apiKey.substring(0, 4);
const end = apiKey.substring(apiKey.length - 4);
const middle = '*'.repeat(Math.max(apiKey.length - 8, 3));
return `${start}${middle}${end}`;
}The Math.max(apiKey.length - 8, 3) floor pins the middle at three stars once the key is shorter than 11 characters,
while the two 4-character windows are still emitted verbatim, so the result is 4 + 3 + 4 = 11 characters regardless
of the key being 9 or 10 long.
Measured by running that function verbatim in Node 24 (keys are a, ab, abc, … so each position is identifiable):
| key length | masked form | masked length | key characters still visible |
|---|---|---|---|
| 8 | ******** |
8 | 0 |
| 9 | abcd***fghi |
11 | 8 of 9 (e is the only hidden one) |
| 10 | abcd***ghij |
11 | 8 of 10 |
| 11 | abcd***hijk |
11 | 8 of 11 |
| 12 | abcd****ijkl |
12 | 8 of 12 |
Across lengths 1-40 the output length differs from the key length at exactly two points: 9 → 11 and 10 → 11.
Two things go wrong, and they come from the same line:
- The masked value is longer than the secret it masks. Anything that shows this string as the configured key — the Web UI's model panel, a screenshot in a bug report, a support conversation — now describes a key that does not exist, and a user comparing lengths cannot tell a truncated key from a correct one.
- The masking band is discontinuous. An 8-character key is 100 % hidden; adding one character to make it 9 drops that to 11 % hidden. Short keys are exactly the ones used against local proxies, mock providers and self-hosted gateways, which is where a partially-revealed key in an HTTP response matters.
How to reach it: sanitizeAgentOptions() calls it for options.model.apiKey at config-sanitizer.ts:29-31,
sanitizeAgentOptions is the body of getAgentOptions (src/api/controllers/system.ts:26), and that handler is bound
by registerSystemRoutes on GET /api/v1/agent/options (src/api/routes/system.ts:21, registered from
src/api/routes/index.ts:20). Nothing in front of it authenticates the caller: setupAPI()
(src/api/index.ts:85-124) applies security headers (:96), a CORS origin whitelist (:99), the JSON body parser and
CSRF protection (:108) — which covers state-changing verbs, not this GET — and then calls registerAllRoutes(app)
(:124), so the Web UI's own model panel is reading this string back over the network:
apiService.getAgentOptions(): Promise<SanitizedAgentOptions> (agent-ui/src/common/services/apiService.ts:468, path
constant at agent-ui/src/common/constants/index.ts:37) feeds AgentConfigViewer
(agent-ui/src/standalone/sidebar/AgentConfigViewer.tsx:26). The same endpoint exists in the second copy of the
package at agent-server-next/src/routes/system.ts:22 → src/controllers/system.ts:35. Configured like
// tarko.config.ts / --model API_KEY_ENV_NAME
model: { provider: 'openai', id: 'gpt-4', apiKey: 'abcdefghi' }the endpoint answers "apiKey": "abcd***fghi".
Expected behavior: the masked string is the same length as the key for every input, and the first-4/last-4 window is only used where it actually leaves something masked.
Additional context
The 4 + 4 window plus the 3-star floor needs 12 characters before it behaves coherently (4 + 4 + 4). So the
one-line-shaped fix is to move the "hide it all" band from <= 8 to <= 11 and drop the floor:
if (apiKey.length <= 11) {
return '*'.repeat(apiKey.length);
}
const middle = '*'.repeat(apiKey.length - 8);which keeps output length equal to input length at every length, keeps the ≤ 8 behaviour exactly as it is today, and
leaves long real-world keys (sk-proj-…, 44 characters here) unchanged. It also makes the Math.max(..., 3) floor
unnecessary rather than wrong, so the middle mask is exactly the hidden character count.
There is currently no test for this unit — multimodal/tarko/agent-server/tests/utils/ holds only
model-utils.test.ts, and neither package references sanitizeApiKey in a test — so a regression test for the length
invariant is cheap to add alongside the fix.
Source: bytedance/UI-TARS-desktop