Skip to content

Commit 87dc33e

Browse files
committed
fix(mattermost): use truncateUtf16Safe for log text and error truncation
1 parent 3f2466c commit 87dc33e

1 file changed

Lines changed: 27 additions & 24 deletions

File tree

extensions/mattermost/src/mattermost/slash-http.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from "openclaw/plugin-sdk/number-runtime";
1313
import { safeEqualSecret } from "openclaw/plugin-sdk/security-runtime";
1414
import { isPrivateNetworkOptInEnabled } from "openclaw/plugin-sdk/ssrf-runtime";
15+
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
1516
import type { ResolvedMattermostAccount } from "../mattermost/accounts.js";
1617
import { getMattermostRuntime } from "../runtime.js";
1718
import {
@@ -142,35 +143,37 @@ function isDeletedMattermostCommand(command: { delete_at?: number }): boolean {
142143

143144
function sanitizeCommandLookupError(error: unknown): string {
144145
const raw = error instanceof Error ? error.message : String(error);
145-
return raw
146-
.replace(/[\r\n\t]/gu, " ")
147-
.replace(/https?:\/\/[^\s)\]}]+/giu, (urlText) => {
148-
try {
149-
const url = new URL(urlText);
150-
if (url.username || url.password) {
151-
url.username = "redacted";
152-
url.password = "redacted";
153-
}
154-
for (const key of url.searchParams.keys()) {
155-
if (SECRET_LOG_KEYS.has(key.toLowerCase())) {
156-
url.searchParams.set(key, "redacted");
146+
return truncateUtf16Safe(
147+
raw
148+
.replace(/[\r\n\t]/gu, " ")
149+
.replace(/https?:\/\/[^\s)\]}]+/giu, (urlText) => {
150+
try {
151+
const url = new URL(urlText);
152+
if (url.username || url.password) {
153+
url.username = "redacted";
154+
url.password = "redacted";
157155
}
156+
for (const key of url.searchParams.keys()) {
157+
if (SECRET_LOG_KEYS.has(key.toLowerCase())) {
158+
url.searchParams.set(key, "redacted");
159+
}
160+
}
161+
return url.toString();
162+
} catch {
163+
return urlText;
158164
}
159-
return url.toString();
160-
} catch {
161-
return urlText;
162-
}
163-
})
164-
.replace(/(^|[^\w-])(Bearer|Token)\s+[A-Za-z0-9._~+/=-]+/giu, "$1$2 [redacted]")
165-
.replace(
166-
/\b(token|authorization|access_token|refresh_token|client_secret|botToken)\b(\s*["']?\s*(?:=|:)\s*["']?)[^"',\s;}]+/giu,
167-
"$1$2[redacted]",
168-
)
169-
.slice(0, 300);
165+
})
166+
.replace(/(^|[^\w-])(Bearer|Token)\s+[A-Za-z0-9._~+/=-]+/giu, "$1$2 [redacted]")
167+
.replace(
168+
/\b(token|authorization|access_token|refresh_token|client_secret|botToken)\b(\s*["']?\s*(?:=|:)\s*["']?)[^"',\s;}]+/giu,
169+
"$1$2[redacted]",
170+
),
171+
300,
172+
);
170173
}
171174

172175
function sanitizeMattermostLogValue(value: string): string {
173-
return value.replace(/[\r\n\t]/gu, " ").slice(0, 200);
176+
return truncateUtf16Safe(value.replace(/[\r\n\t]/gu, " "), 200);
174177
}
175178

176179
async function withCommandLookupTimeout<T>(task: (signal: AbortSignal) => Promise<T>): Promise<T> {

0 commit comments

Comments
 (0)