From a7384c82aedaf11fcefb62a7a6073442faa3ce96 Mon Sep 17 00:00:00 2001 From: Lumea-art Date: Thu, 12 Nov 2026 21:11:57 +0800 Subject: [PATCH] fix(agent-sdk): mask every evt_/emk_ token shape the Go CLI masks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit redactError only matched exactly 32 alphanumerics, while cli/internal/output/redact.go masks emk_/evt_ with [A-Za-z0-9_-]{20,}. Any shorter, longer or dash/underscore-bearing token therefore survived — fully or partially — in plugin logs, host model context and user-visible errors. Share one shape with the Go side and keep the prefix-stable mask. Regression test covers 24-char, 36-char and dash/underscore payloads for both prefixes. --- plugins/agent-sdk/src/client.js | 14 +++++++------- plugins/agent-sdk/tests/client.test.js | 26 +++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/plugins/agent-sdk/src/client.js b/plugins/agent-sdk/src/client.js index 81093e4..f98d2ca 100644 --- a/plugins/agent-sdk/src/client.js +++ b/plugins/agent-sdk/src/client.js @@ -25,11 +25,12 @@ const noop = { info() {}, warn() {} }; * accidentally lands in an error message (defense-in-depth, the same * shape evercli's output.redact uses). */ -// Token regex: case-insensitive on both alphabet AND prefix. Current -// issuance is lowercase hex but defense-in-depth covers a future -// alphabet change without us having to chase every log/error sink. -const evtRe = /evt_[a-zA-Z0-9]{32}/g; -const emkRe = /emk_[a-zA-Z0-9]{32}/g; +// Token shape mirrors evercli's output.redact: a 20+ character payload +// over [A-Za-z0-9_-], not only the current 32-char lowercase hex. A future +// backend key format (Base32, base64url, longer payloads) must not turn +// every error sink into a credential leak. Prefixes stay case-sensitive, +// exactly like the Go side. +const tokenRe = /(?:evt_|emk_)[A-Za-z0-9_-]{20,}/g; // AWS S3 presigned-POST / GET URLs carry short-lived but real // credentials. The query-string fields below are issued for ~15 min @@ -61,8 +62,7 @@ export function redactError(msg) { // also scrub. const text = msg instanceof Error ? msg.message : String(msg); return text - .replace(evtRe, (m) => m.slice(0, 8) + "_REDACTED") - .replace(emkRe, (m) => m.slice(0, 8) + "_REDACTED") + .replace(tokenRe, (m) => m.slice(0, 8) + "_REDACTED") .replace(s3SigParamRe, (_, name) => name + "=[REDACTED]") .replace(awsKeyIDRe, "[REDACTED-AWSKEY]"); } diff --git a/plugins/agent-sdk/tests/client.test.js b/plugins/agent-sdk/tests/client.test.js index aafe09a..85bf859 100644 --- a/plugins/agent-sdk/tests/client.test.js +++ b/plugins/agent-sdk/tests/client.test.js @@ -1,7 +1,7 @@ import { test, describe, beforeEach } from "node:test"; import assert from "node:assert/strict"; import http from "node:http"; -import { createClient, EvermeError } from "../src/client.js"; +import { createClient, EvermeError, redactError } from "../src/client.js"; /** * Helper: spin up a tiny HTTP server, return its base URL + a @@ -170,3 +170,27 @@ describe("request id propagation", () => { ); }); }); + +describe("redactError", () => { + // The Go CLI masks evt_/emk_ with [A-Za-z0-9_-]{20,}; the SDK used to + // require exactly 32 alphanumerics, so a backend key-format change would + // leak a live credential into host logs and model context. + test("masks every token shape the Go side masks", () => { + const shapes = [ + "emk_" + "a".repeat(24), + "emk_" + "a".repeat(18) + "-" + "b".repeat(17), + "emk_" + "a".repeat(36), + "evt_" + "b".repeat(24), + "evt_" + "b".repeat(18) + "_" + "c".repeat(17), + ]; + for (const token of shapes) { + const out = redactError(`backend rejected ${token} (request req-1)`); + assert.ok(!out.includes(token), `full token must not survive redaction: ${out}`); + assert.match( + out, + /^backend rejected (?:emk|evt)_[A-Za-z0-9_-]{4}_REDACTED \(request req-1\)$/, + "prefix is kept and the payload is masked", + ); + } + }); +});