Skip to content
Open
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
14 changes: 7 additions & 7 deletions plugins/agent-sdk/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]");
}
Expand Down
26 changes: 25 additions & 1 deletion plugins/agent-sdk/tests/client.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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",
);
}
});
});