mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-28 04:41:52 +01:00
fix(tool-gateway): preserve safe remote MCP error messages (#22025)
Preserve bounded, redacted and screened upstream JSON-RPC error messages for remote MCP tools and context reads. Audit blocked instructions, retain received error semantics, and redact URL/session and decoded credential echoes. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
904c01d1a3
commit
cd656330fe
9 files changed
+460
-32
No files matched your search
@@ -238,6 +238,35 @@ function encodedSensitiveVariants(values: readonly string[]): string[] {
|
||||
.sort((left, right) => right.length - left.length);
|
||||
}
|
||||
|
||||
/** Decode candidate spans rather than enumerating secret encodings. This also
|
||||
* finds credentials embedded inside an encoded diagnostic or authorization value.
|
||||
* Bound nesting so untrusted provider text cannot cause unbounded decoding work. */
|
||||
function redactEncodedSensitiveOccurrences(text: string, variants: readonly string[], depth = 0): string {
|
||||
if (depth >= 3 || variants.length === 0) return text;
|
||||
const containsSecret = (decoded: string) => variants.some(value => decoded.includes(value))
|
||||
|| redactEncodedSensitiveOccurrences(decoded, variants, depth + 1) !== decoded;
|
||||
const percentRedacted = text.replace(/[^\s<>"`]+/g, token => {
|
||||
if (!/%[0-9a-f]{2}/i.test(token)) return token;
|
||||
// Try URI and form encoding separately: '+' is literal in URI components.
|
||||
for (const candidate of [token, token.replaceAll("+", " ")]) {
|
||||
const decoded = candidate.replace(/(?:%[0-9a-f]{2})+/gi, span => {
|
||||
try { return decodeURIComponent(span); }
|
||||
catch { return span; } // Keep malformed UTF-8 without hiding valid spans.
|
||||
});
|
||||
if (containsSecret(decoded)) return REDACTED;
|
||||
}
|
||||
return token;
|
||||
});
|
||||
return percentRedacted.replace(/[A-Za-z0-9+/_-]+={0,2}/g, token => {
|
||||
if (token.length < 4) return token;
|
||||
const canonical = token.replaceAll("-", "+").replaceAll("_", "/").replace(/=+$/, "");
|
||||
const bytes = Buffer.from(canonical, "base64");
|
||||
// Buffer accepts invalid base64; only inspect a lossless canonical decode.
|
||||
if (bytes.toString("base64").replace(/=+$/, "") !== canonical) return token;
|
||||
return containsSecret(bytes.toString("utf8")) ? REDACTED : token;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Redacts sensitive-shaped fields, then removes submitted credential values
|
||||
* wherever an upstream validator/provider echoed them into otherwise-safe
|
||||
@@ -255,7 +284,7 @@ export function redactSensitiveValueOccurrences(
|
||||
if (typeof entry === "string") {
|
||||
return variants.reduce(
|
||||
(text, sensitiveValue) => text.replaceAll(sensitiveValue, REDACTED),
|
||||
entry,
|
||||
redactEncodedSensitiveOccurrences(entry, variants),
|
||||
);
|
||||
}
|
||||
if (entry === null || typeof entry !== "object") return entry;
|
||||
|
||||
Reference in new issue
Block a user