Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(mcp): strip non-Latin1 chars in sanitizeHeaderValue (#76350)

Ben Whiteb03e53248fdc
diff --git a/services/mcp/src/lib/utils.ts b/services/mcp/src/lib/utils.ts
index b7f7a656b15a..82c1f756f10b 100644
--- a/services/mcp/src/lib/utils.ts
+++ b/services/mcp/src/lib/utils.ts
@@ -51,9 +51,16 @@ export function sanitizeHeaderValue(value?: string): string | undefined {
if (!value) {
return undefined
}
- // Strip control characters, then trim and truncate
+ // Strip control characters and any codepoint outside the Latin-1 range
+ // (0x00–0xFF), then trim and truncate. HTTP header values are ByteStrings,
+ // so a character > 0xFF — e.g. an emoji or non-Latin script in a
+ // client-supplied name or user-agent — makes the outbound `fetch` throw
+ // `TypeError: Cannot convert argument to a ByteString ...`. Left unstripped
+ // that surfaces during init as `Failed to get user: ...` and is counted as
+ // a server-side `mcp_init_total{status="error"}`, spuriously tripping the
+ // MCPServerHighInitErrorRate alert on what is really bad client input.
const sanitised = value
- .replace(/[\x00-\x1f\x7f]/g, '')
+ .replace(/[\x00-\x1f\x7f]|[^\x00-\xff]/g, '')
.trim()
.slice(0, MAX_HEADER_VALUE_LENGTH)
return sanitised || undefined
diff --git a/services/mcp/tests/unit/utils.test.ts b/services/mcp/tests/unit/utils.test.ts
index 10df047524ff..59a82620a82d 100644
--- a/services/mcp/tests/unit/utils.test.ts
+++ b/services/mcp/tests/unit/utils.test.ts
@@ -173,6 +173,9 @@ describe('utils', () => {
['passthrough', 'posthog/wizard 1.0', 'posthog/wizard 1.0'],
['strips control chars', 'agent\x00with\x1fnulls', 'agentwithnulls'],
['strips DEL character', 'hello\x7fworld', 'helloworld'],
+ ['strips non-Latin1 chars (emoji)', 'Claude \u{1F916} Desktop', 'Claude Desktop'],
+ ['strips non-Latin1 scripts', 'クライアント', undefined],
+ ['keeps Latin-1 high range', 'café', 'café'],
['truncates to max length', 'a'.repeat(1500), 'a'.repeat(1000)],
['trims whitespace', ' spaces ', 'spaces'],
['strips then trims', '\x00 hello \x1f', 'hello'],