@posthog/mcp 0.22.0 → 0.22.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,124 +3,20 @@
3
3
  // Copyright (c) 2025 AgentCat, Inc. (formerly MCPcat)
4
4
  // Licensed under the MIT License: https://github.com/agentcathq/agentcat-typescript-sdk/blob/main/LICENSE
5
5
 
6
+ import {
7
+ redactPii,
8
+ sanitizeCapturedValue as sanitizeCoreCapturedValue,
9
+ sanitizeFreeText as sanitizeCoreFreeText,
10
+ sanitizeFreeTextValue as sanitizeCoreFreeTextValue,
11
+ type TextSanitizationOptions,
12
+ } from '@posthog/core'
6
13
  import { MAX_STRING_LENGTH, TRUNCATION_SUFFIX } from './truncation'
7
14
 
8
15
  const CONTEXT_ARGUMENT_NAME = 'context'
9
- const REDACTED_VALUE = '[redacted]'
10
- const BINARY_REDACTED_VALUE = '[binary data redacted - not supported by PostHog MCP analytics]'
11
- const BASE64_PATTERN = /^[A-Za-z0-9+/\n\r]+=*$/
12
- const BASE64URL_PATTERN = /^[A-Za-z0-9_-]+={0,2}$/
13
- const BASE64URL_SPECIFIC_CHAR_PATTERN = /[-_]/
14
- const BASE64_DATA_URL_PREFIX_PATTERN = /^data:[^,\s]*;base64,/i
15
- const BASE64_DATA_URL_PAYLOAD_PATTERN = /^[A-Za-z0-9+/_-]+={0,2}$/
16
- const SIZE_GATE = 10_240
17
- const SCANNED_HEAD_LENGTH = 4 * MAX_STRING_LENGTH
18
- // Twice what truncation keeps, so a match split where the head was cut sits past
19
- // the part truncation keeps.
20
- const MIN_REDACTED_HEAD_LENGTH = 2 * MAX_STRING_LENGTH
21
- const POSTHOG_TOKEN_PATTERN = /\bph[a-z]_[A-Za-z0-9_-]{20,}\b/g
22
- const SENSITIVE_KEY_PATTERN =
23
- /^(authorization|cookie|set-cookie|x-api-key|api[-_]?key|api[-_]?token|access[-_]?token|refresh[-_]?token|token|password|secret|client[-_]?secret|private[-_]?key)$/i
24
-
25
- // Deliberately no leading `\b`: `_` is a word character but not scheme-legal, so
26
- // `resource_https://user:pw@host` has no boundary to anchor to and would keep its
27
- // credentials. Leftmost matching makes `foo.https://x` match from `f` with scheme
28
- // `foo.https`, which redacts the same URL and is therefore harmless.
29
- //
30
- // `'` is a valid URI sub-delimiter, so the terminal class must not stop at one:
31
- // excluding it truncated `https://example.com/o'reilly?token=x` at the path and
32
- // shipped the token in the clear. Only the characters that are never valid
33
- // unencoded in a URI are excluded.
34
- //
35
- // The authority is optional too. An MCP resource URI frequently has none
36
- // (`resource:guide?token=x`, `file:/guide.md`), and requiring `//` let those
37
- // through untouched; `[^\s<>"]+` absorbs a `//host` when there is one. The cost
38
- // is over-matching ordinary prose (`Error:foo`, `at12:30`, `C:\path`), which is
39
- // harmless: a match with nothing to redact is returned byte-for-byte, so the
40
- // text around it is never rewritten.
41
- const URL_PATTERN = /[a-z][a-z0-9+.-]{0,63}:[^\s<>"]+/gi
42
- // A character no URL match can contain, so a cut just after one splits no URL.
43
- const URL_END_PATTERN = /[\s<>"]/
44
- /** The same pattern without `g`, for asking whether a value holds a URL at all. */
45
- const URL_PATTERN_ONCE = new RegExp(URL_PATTERN.source, 'i')
46
- /** Finds the first scheme that brings a real authority, i.e. what the pattern required before it went authority-less. */
47
- const URL_AUTHORITY_SEARCH = /[a-z][a-z0-9+.-]{0,63}:\/\//i
48
- /** The same, anchored: does this match *open* with an authority? */
49
- const URL_AUTHORITY_PATTERN = new RegExp(`^${URL_AUTHORITY_SEARCH.source}`, 'i')
50
- /** Every authority start in a value, in order. */
51
- const URL_AUTHORITY_SEARCH_ALL = new RegExp(URL_AUTHORITY_SEARCH.source, 'gi')
52
- /** The field separators. `?` and `#` are structural only as delimiters — see {@link isStructural}. */
53
- const URL_FIELD_CHARACTERS = '=&'
54
- /** The legacy field separator. Never split on, only checked; see {@link sanitizeUrlFieldValue}. */
55
- const LEGACY_FIELD_SEPARATOR = ';'
56
- // The terminal class above also absorbs the prose punctuation that follows a URL
57
- // in a sentence, `'` included now that a URL can contain one. See
58
- // `splitTrailingPunctuation`.
59
- const URL_TRAILING_PUNCTUATION = ".,;:!?)]}'"
60
- const MAX_URL_LENGTH = 8192
61
- const MAX_URL_QUERY_FIELDS = 128
62
- // A query key is sensitive when any `-`/`_`/`.`/`/`/`;`-delimited segment names
63
- // a credential, so compound names (`private_token`, `oauth_signature`,
64
- // `subscription-key`, `X-Amz-Security-Token`, `Key-Pair-Id`) are covered without
65
- // enumerating every vendor's spelling. `/` is a separator because a fragment's
66
- // field list can be written `#/token=…`, and `;` because a legacy `;`-separated
67
- // pair parses into a single key (`download;token`) — either would otherwise hide
68
- // the name. Over-redacting a benign `sort_key` — or `sort/key`, or `sort;key` —
69
- // is the accepted trade for an analytics payload.
70
- const SENSITIVE_QUERY_KEY_SEGMENT_PATTERN =
71
- /(^|[-_./;])(auth|token|secret|password|passwd|pwd|credential|signature|sig|key|hmac|sas|bearer|jwt|session|sessionid)([-_./;]|$)/i
72
- // `code` — the OAuth authorization code — is matched only as a whole key: as a
73
- // segment it would eat `country_code`, `zip_code`, and `lang_code`.
74
- const SENSITIVE_QUERY_KEY_EXACT_PATTERN = /^(code|AWSAccessKeyId|GoogleAccessId|Policy)$/i
75
-
76
- // PII redaction for the agent-narrated intent string only. `$mcp_intent` is free
77
- // text the calling LLM writes into the injected `context` argument, so it can
78
- // carry personal data the model read aloud despite being told not to. We redact
79
- // well-defined *structured identifiers* — the kind regex can match with high
80
- // precision. Person names and postal addresses are deliberately out of scope:
81
- // they need an NER model that a client SDK cannot ship, and naive patterns would
82
- // over-redact ordinary prose. Patterns are ordered so an earlier pass never eats
83
- // digits a later pass needs (email before phone, IPs before phone, cards before
84
- // the generic phone pass). See `redactPii`.
85
- // Horizontal Unicode spaces (NBSP, narrow NBSP, ideographic space, ...) are what
86
- // appear when text is copied from web pages or PDFs. `redactPii` normalizes them
87
- // to an ASCII space first so the separator-based card/phone/SSN candidates match
88
- // them instead of leaking the identifier they group.
89
- const UNICODE_SPACE_PATTERN = /[\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]/g
90
- // Quantifiers are bounded to RFC-ish limits (local-part <=64, domain <=255,
91
- // TLD <=24) rather than open-ended `+`. Unbounded `+` here is quadratic: on a
92
- // long run of local-part chars with no valid `.tld`, `replace` rescans from
93
- // every start position. `$mcp_intent` is attacker-influenceable free text seen
94
- // before truncation, so an open-ended pattern is a reachable event-loop stall.
95
- const EMAIL_PATTERN = /[A-Za-z0-9._%+-]{1,64}@[A-Za-z0-9.-]{1,255}\.[A-Za-z]{2,24}/g
96
- const IPV4_PATTERN = /\b(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\b/g
97
- // Four forms: full 8-group, `::`-terminated (`2001:db8::`), a middle `::`
98
- // (`2001:db8::8a2e:1`), and a leading `::` (`::1`). The compressed branches use a
99
- // `(?<![\w:])` boundary so a hex-looking C++ scope like `std::bad` — whose left
100
- // side is not a valid hex group — is not mistaken for an address, while a
101
- // genuinely address-shaped `dead::beef` still matches.
102
- const IPV6_PATTERN =
103
- /\b(?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}\b|(?<![\w:])(?:[0-9A-Fa-f]{1,4}:){1,7}:(?![\w:])|(?<![\w:])(?:[0-9A-Fa-f]{1,4}:){1,6}:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4}){0,5}(?![\w])|(?<![\w:])::(?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4}){0,6})(?![\w])/g
104
- // A separator (space, dot, or dash) is required between the 3-2-4 groups so bare
105
- // 9-digit IDs are never mistaken for an SSN.
106
- const US_SSN_PATTERN = /\b\d{3}[ .-]\d{2}[ .-]\d{4}\b/g
107
- // A run of >=13 digits optionally grouped by a single space, dot, dash, or
108
- // slash. This only marks the numeric region; `redactCardInMatch` then looks for
109
- // the actual card as a run of whole separator-delimited groups that passes Luhn,
110
- // so an adjacent field such as an expiry (`4111 1111 1111 1111 12/30`) is not
111
- // absorbed into a failing check that would leak the card.
112
- const CREDIT_CARD_CANDIDATE_PATTERN = /\b\d(?:[ ./-]?\d){12,}\b/g
113
- // Matches each separator-delimited digit group inside a card candidate.
114
- const DIGIT_GROUP_PATTERN = /\d+/g
115
- // Phone matching is structural rather than "any 10–15 digits", so dates
116
- // (`2024-01-15 12:30`) and dotted versions are not mistaken for numbers. Two
117
- // forms: a North-American 3-3-4 grouping, and an international number that must
118
- // start with `+` and a country code. The area code is either `(415)` (the
119
- // separator after it is optional, so `(415)555-0142` matches) or a bare `415`
120
- // that must be followed by a separator (space, dot, dash, or slash) — so a bare
121
- // digit run is never taken for a phone number.
122
- const PHONE_NANP_PATTERN = /(?<![\w+])(?:\+?1[ ./-]?)?(?:\(\d{3}\)[ ./-]?|\d{3}[ ./-])\d{3}[ ./-]\d{4}(?![\w])/g
123
- const PHONE_INTL_PATTERN = /(?<!\w)\+\d{1,3}(?:[ ./()-]{0,2}\d){7,13}(?![\w])/g
16
+ const SANITIZATION_OPTIONS: TextSanitizationOptions = {
17
+ maxStringLength: MAX_STRING_LENGTH,
18
+ truncationSuffix: TRUNCATION_SUFFIX,
19
+ }
124
20
 
125
21
  type JsonRecord = Record<string, unknown>
126
22
 
@@ -128,565 +24,18 @@ function isRecord(value: unknown): value is JsonRecord {
128
24
  return !!value && typeof value === 'object' && !Array.isArray(value)
129
25
  }
130
26
 
131
- function shouldRedactKey(key: string): boolean {
132
- return SENSITIVE_KEY_PATTERN.test(key)
133
- }
134
-
135
- function shouldRedactQueryKey(key: string): boolean {
136
- return (
137
- shouldRedactKey(key) || SENSITIVE_QUERY_KEY_SEGMENT_PATTERN.test(key) || SENSITIVE_QUERY_KEY_EXACT_PATTERN.test(key)
138
- )
139
- }
140
-
141
- function isBase64DataUrl(value: string): boolean {
142
- const prefix = BASE64_DATA_URL_PREFIX_PATTERN.exec(value)
143
- if (!prefix) {
144
- return false
145
- }
146
-
147
- let payload: string
148
- try {
149
- payload = decodeURIComponent(value.slice(prefix[0].length))
150
- } catch {
151
- return false
152
- }
153
-
154
- return BASE64_DATA_URL_PAYLOAD_PATTERN.test(payload.replace(/[\r\n]/g, ''))
155
- }
156
-
157
- function exceedsUrlFieldLimit(fields: string): boolean {
158
- return fields.split('&', MAX_URL_QUERY_FIELDS + 1).length > MAX_URL_QUERY_FIELDS
159
- }
160
-
161
- /** How much of the sanitizer one call is allowed to apply. */
162
- interface UrlSanitizeMode {
163
- /** Sanitize a URL found inside a retained field value. Off past depth 1. */
164
- allowNestedUrls: boolean
165
- /** Split prose punctuation off the end. Off when the match is a whole string. */
166
- stripPunctuation: boolean
167
- }
168
-
169
- /** Whether any `;`-separated piece of `value` names a credential. */
170
- function hasLegacySensitiveField(value: string): boolean {
171
- return value.split(LEGACY_FIELD_SEPARATOR).some((piece) => shouldRedactQueryKey(piece.split('=', 1)[0]))
172
- }
173
-
174
- /** The sanitized value of one query or fragment field. */
175
- function sanitizeUrlFieldValue(key: string, value: string, allowNestedUrls: boolean): string {
176
- if (shouldRedactQueryKey(key)) {
177
- return REDACTED_VALUE
178
- }
179
- // `;` once separated fields the way `&` does, so a value carrying one may hold
180
- // a credential field inside it. Splitting on it is not an option — that cuts a
181
- // credential's own value in two and publishes the tail as a bare key
182
- // (`?password=prefix;rest`) — so instead the whole value goes when any piece
183
- // of it names a credential.
184
- if (value.includes(LEGACY_FIELD_SEPARATOR) && hasLegacySensitiveField(value)) {
185
- return REDACTED_VALUE
186
- }
187
- if (!URL_PATTERN_ONCE.test(value)) {
188
- return value
189
- }
190
- // One level of nesting is the whole budget. Past it a URL-bearing value is
191
- // dropped rather than trusted: nothing deeper would sanitize it, so a gateway
192
- // address wrapping a gateway address would otherwise ship its credentials.
193
- return allowNestedUrls
194
- ? sanitizeUrlsInString(value, { allowNestedUrls: false, stripPunctuation: true })
195
- : REDACTED_VALUE
196
- }
197
-
198
- /**
199
- * Redacts the values of credential-named fields in one `&`-separated field list
200
- * — a query string or a fragment. `serialized` is only meaningful when `changed`
201
- * is true, so an untouched part keeps its original encoding instead of being
202
- * re-serialized. `lastFieldSensitive` says whether whatever follows the list
203
- * could be more of a credential, which is what decides both the fragment tail
204
- * and restored prose punctuation; see {@link sanitizeUrl}.
205
- */
206
- function sanitizeUrlFields(
207
- fields: string,
208
- allowNestedUrls: boolean
209
- ): { changed: boolean; lastFieldSensitive: boolean; serialized: string } {
210
- const sanitized = new URLSearchParams()
211
- let changed = false
212
- let lastFieldSensitive = false
213
- for (const [key, value] of new URLSearchParams(fields)) {
214
- const sanitizedValue = sanitizeUrlFieldValue(key, value, allowNestedUrls)
215
- // `changed` is compared, not inferred from the key: the PostHog-token pass
216
- // runs first, so a value can already read `[redacted]`, and calling that a
217
- // change would re-serialize the field only to percent-encode it.
218
- changed ||= sanitizedValue !== value
219
- // Sensitivity is the separate question — whether what follows this field
220
- // could be more of a credential — and an already-redacted value is still a
221
- // credential's field.
222
- lastFieldSensitive = shouldRedactQueryKey(key) || sanitizedValue !== value
223
- sanitized.append(key, sanitizedValue)
224
- }
225
- return { changed, lastFieldSensitive, serialized: sanitized.toString() }
226
- }
227
-
228
- /**
229
- * Splits the prose punctuation that follows a URL in a sentence off the end of a
230
- * match, so it is not parsed as part of the address and can be re-appended
231
- * verbatim to whatever the sanitizer returns.
232
- *
233
- * Walked back character by character rather than matched with a `$`-anchored
234
- * pattern: a captured URI is attacker-influenceable, and on a long punctuation
235
- * run that does not end the match a backtracking pattern is quadratic.
236
- */
237
- function splitTrailingPunctuation(value: string): { address: string; suffix: string } {
238
- let end = value.length
239
- while (end > 0 && URL_TRAILING_PUNCTUATION.includes(value[end - 1])) {
240
- end--
241
- }
242
- return { address: value.slice(0, end), suffix: value.slice(end) }
243
- }
244
-
245
- /**
246
- * A fragment's tail, given how its head came out.
247
- *
248
- * A `?` inside a credential looks exactly like the one between a route and its
249
- * fields. So when the head's last field is a credential's, everything after that
250
- * `?` may be the rest of its value — `#password=prefix?rest` — and the
251
- * tail goes whole rather than being read on its own terms. An empty tail has
252
- * nothing to hide and stays empty. Either way it counts as a rewritten trailing
253
- * field, so prose punctuation after the URL goes with it for the same reason.
254
- */
255
- function sanitizeFragmentTail(
256
- tail: string,
257
- headLastFieldSensitive: boolean,
258
- allowNestedUrls: boolean
259
- ): { value: string; lastFieldSensitive: boolean } {
260
- if (!headLastFieldSensitive) {
261
- return sanitizeFragmentPart(tail, allowNestedUrls)
262
- }
263
- return { value: tail === '' ? '' : REDACTED_VALUE, lastFieldSensitive: true }
264
- }
265
-
266
- /**
267
- * Splits a fragment at its first `?`. `tail` is null when there is none.
268
- *
269
- * Nothing is assumed about either part. Shape alone cannot tell a router's route
270
- * from a field list — `#/docs/id=1?token=…` puts an `=` in the route, and
271
- * `#/token=…&next=https://x/?page=1` puts a `/` and a `?` in a field list — so
272
- * each part is read as fields when it holds an `=` and as text otherwise.
273
- */
274
- function splitFragmentParts(hash: string): { head: string; tail: string | null } {
275
- const fragment = hash.slice(1)
276
- const separator = fragment.indexOf('?')
277
- if (separator < 0) {
278
- return { head: fragment, tail: null }
279
- }
280
- return { head: fragment.slice(0, separator), tail: fragment.slice(separator + 1) }
281
- }
282
-
283
- /** One fragment part, and whether its last field was rewritten. */
284
- function sanitizeFragmentPart(part: string, allowNestedUrls: boolean): { value: string; lastFieldSensitive: boolean } {
285
- if (!part.includes('=')) {
286
- return { value: sanitizeFragmentText(part, allowNestedUrls), lastFieldSensitive: false }
287
- }
288
- const fields = sanitizeUrlFields(part, allowNestedUrls)
289
- // Verbatim unless something was actually rewritten: only the part that changed
290
- // should change encoding.
291
- return { value: fields.changed ? fields.serialized : part, lastFieldSensitive: fields.lastFieldSensitive }
292
- }
293
-
294
- /**
295
- * Sanitizes the part of a fragment that is text rather than fields — a plain
296
- * `#intro`, or the route in front of a field list. It is text, and text can
297
- * carry an address, so it gets the URL pass.
298
- *
299
- * Depth is capped the way a field value's is: at the nested level a URL-bearing
300
- * text is dropped rather than descended into. That is also what stops
301
- * `resource:x#resource:x#…` from recursing once per `#`.
302
- */
303
- function sanitizeFragmentText(text: string, allowNestedUrls: boolean): string {
304
- if (!allowNestedUrls) {
305
- return URL_PATTERN_ONCE.test(text) ? REDACTED_VALUE : text
306
- }
307
- return sanitizeUrlsInString(text, { allowNestedUrls: false, stripPunctuation: true })
308
- }
309
-
310
- /**
311
- * Every offset inside `value` where an adjacent address starts; see
312
- * {@link sanitizeUrl}.
313
- *
314
- * An authority is in *value position* — left alone, because the field pass
315
- * already gives it the nested pass — when a field name's `=` is the nearest
316
- * structural character behind it. Structural means a field separator (`=`, `&`,
317
- * `;`) or one of the URL's three delimiters; see {@link isDelimiter}. Before any
318
- * delimiter no `=` has been seen, so `…/redirect=https://user:pw@host` is two
319
- * addresses rather than one field.
320
- *
321
- * Decided in one forward walk, not a backward scan per authority. The scan was
322
- * quadratic on a value that opens its fields once and then runs thousands of
323
- * addresses together, because every one of them scanned back to the same `?`.
324
- */
325
- function findEmbeddedAuthorityIndexes(value: string): number[] {
326
- // The search tries up to 64 scheme characters at every position, which a long
327
- // base64 `data:` URI turns into seconds; without `://` it cannot match at all.
328
- if (!value.includes('://')) {
329
- return []
330
- }
331
- const starts = [...value.matchAll(URL_AUTHORITY_SEARCH_ALL)].map((match) => match.index)
332
- const indexes: number[] = []
333
- const delimiters = { query: false, fragment: false, fragmentQuery: false }
334
- let pending = 0
335
- let inFields = false
336
- let lastStructural = ''
337
- for (let cursor = 0; cursor < value.length && pending < starts.length; cursor++) {
338
- if (starts[pending] === cursor) {
339
- if (cursor > 0 && lastStructural !== '=') {
340
- indexes.push(cursor)
341
- }
342
- pending++
343
- }
344
- const character = value[cursor]
345
- if (isDelimiter(character, delimiters, lastStructural)) {
346
- lastStructural = character
347
- inFields = true
348
- } else if (inFields && URL_FIELD_CHARACTERS.includes(character)) {
349
- lastStructural = character
350
- }
351
- }
352
- return indexes
353
- }
354
-
355
- /**
356
- * Whether this `?` or `#` is one of a URL's three delimiters, consuming it if so.
357
- *
358
- * A URL has exactly one query delimiter, one fragment delimiter, and — matching
359
- * how the fragment is split — one delimiter between the fragment's own head and
360
- * tail. Every other `?` or `#` is ordinary text inside a value, and reading one
361
- * as structural would cut `?token=prefix?https://secret/…` in half at a `?` that
362
- * belongs to the token.
363
- */
364
- function isDelimiter(
365
- character: string,
366
- seen: { query: boolean; fragment: boolean; fragmentQuery: boolean },
367
- lastStructural: string
368
- ): boolean {
369
- if (character === '#') {
370
- return seen.fragment ? false : (seen.fragment = true)
371
- }
372
- if (character !== '?') {
373
- return false
374
- }
375
- if (seen.fragment) {
376
- // Inside a fragment, a `?` right after a field name may belong to that
377
- // field's value rather than open the tail. Leaving it unstructural keeps a
378
- // following address attached, so the fragment split and its fail-closed tail
379
- // rule get to decide — they can see the whole credential, this cannot.
380
- if (lastStructural === '=') {
381
- return false
382
- }
383
- return seen.fragmentQuery ? false : (seen.fragmentQuery = true)
384
- }
385
- return seen.query ? false : (seen.query = true)
386
- }
387
-
388
- /**
389
- * Redacts the credentials embedded in one URL-shaped match: the userinfo, plus
390
- * the values of credential-named query and fragment fields. A retained value that
391
- * is itself a URL — a gateway's `?url=` passthrough — gets the same pass one
392
- * level deep.
393
- *
394
- * One match can hold more than one address: a prose word in front of it
395
- * (`Failed URL:https://…`, `a:b:https://…`), several run together
396
- * (`…/doc,https://…`), or one parked in another's query or fragment
397
- * (`…/?download)[b](https://…`). The later address always begins inside what
398
- * would parse as the earlier one's path, query key, or fragment — none of which
399
- * is examined for userinfo — so it is cut out and sanitized on its own.
400
- *
401
- * The exception is an authority in value position (see
402
- * {@link findEmbeddedAuthorityIndexes}), which the field pass already gives the
403
- * nested pass. Cutting there would take a gateway's `?url=https://…` out of the
404
- * field it belongs to.
405
- *
406
- * One pass, no recursion: after the cuts, every authority left inside a piece is
407
- * either at its start or in value position, so no piece can need cutting again.
408
- *
409
- * Every piece but the last is sanitized without the punctuation split: it is
410
- * followed by an address rather than by prose, so its last character is a
411
- * separator, not a sentence's. (It matters — stripping the `:` off `URL:` would
412
- * leave `URL`, which `new URL()` rejects, and the prose word would become
413
- * `[redacted]`.)
414
- */
415
- function sanitizeUrl(value: string, mode: UrlSanitizeMode): string {
416
- const embedded = findEmbeddedAuthorityIndexes(value)
417
- if (embedded.length === 0) {
418
- return sanitizeSingleUrl(value, mode)
419
- }
420
- let result = ''
421
- let start = 0
422
- for (const index of embedded) {
423
- result += sanitizeSingleUrl(value.slice(start, index), { ...mode, stripPunctuation: false })
424
- start = index
425
- }
426
- return result + sanitizeSingleUrl(value.slice(start), mode)
427
- }
27
+ export { redactPii }
428
28
 
429
- /** {@link sanitizeUrl} for a value already known to hold exactly one address. */
430
- function sanitizeSingleUrl(value: string, mode: UrlSanitizeMode): string {
431
- // The length bound caps the work an attacker-shaped address can force, so it
432
- // only applies to a match that has an authority. A long authority-less match
433
- // is usually a data URI; it is parsed like any other, which is linear in
434
- // `new URL()` and still capped by the field bound below, and comes back
435
- // byte-for-byte when it holds nothing to redact.
436
- if (value.length > MAX_URL_LENGTH && URL_AUTHORITY_PATTERN.test(value)) {
437
- return REDACTED_VALUE
438
- }
439
-
440
- const { address, suffix } = mode.stripPunctuation ? splitTrailingPunctuation(value) : { address: value, suffix: '' }
441
- let url: URL
442
- try {
443
- url = new URL(address)
444
- } catch {
445
- return REDACTED_VALUE + suffix
446
- }
447
-
448
- const query = url.search.slice(1)
449
- const hasFragment = url.hash !== ''
450
- const fragment = splitFragmentParts(url.hash)
451
- if (exceedsUrlFieldLimit(query) || exceedsUrlFieldLimit(fragment.head) || exceedsUrlFieldLimit(fragment.tail ?? '')) {
452
- return REDACTED_VALUE + suffix
453
- }
454
-
455
- let changed = false
456
- if (url.username || url.password) {
457
- url.username = REDACTED_VALUE
458
- url.password = ''
459
- changed = true
460
- }
461
- const sanitizedQuery = sanitizeUrlFields(query, mode.allowNestedUrls)
462
- if (sanitizedQuery.changed) {
463
- url.search = sanitizedQuery.serialized
464
- changed = true
465
- }
466
- const sanitizedHead = sanitizeFragmentPart(fragment.head, mode.allowNestedUrls)
467
- const sanitizedTail =
468
- fragment.tail === null
469
- ? null
470
- : sanitizeFragmentTail(fragment.tail, sanitizedHead.lastFieldSensitive, mode.allowNestedUrls)
471
- const sanitizedHash = sanitizedTail === null ? sanitizedHead.value : `${sanitizedHead.value}?${sanitizedTail.value}`
472
- if (sanitizedHash !== url.hash.slice(1)) {
473
- url.hash = sanitizedHash
474
- changed = true
475
- }
476
-
477
- // The punctuation split off the end may be the tail of the very credential
478
- // just replaced (`?password=fakepass!!!`) rather than the sentence's. When the
479
- // last field of the URL's trailing part is a credential's the punctuation goes
480
- // with it; losing a comma from the surrounding prose is the accepted cost of
481
- // not shipping `!!!`.
482
- const trailingFields = hasFragment ? (sanitizedTail ?? sanitizedHead) : sanitizedQuery
483
- return (changed ? url.toString() : address) + (trailingFields.lastFieldSensitive ? '' : suffix)
484
- }
485
-
486
- /**
487
- * Replaces every URL-shaped match in `text`.
488
- *
489
- * A match that IS the whole string is an address rather than prose — a captured
490
- * `$mcp_resource_name`, a `params.uri`, a nested `?url=` value — so nothing is
491
- * split off its end: a trailing `!` there belongs to the URL.
492
- */
493
- function sanitizeUrlsInString(text: string, mode: UrlSanitizeMode): string {
494
- return text.replace(URL_PATTERN, (match) =>
495
- sanitizeUrl(match, { ...mode, stripPunctuation: mode.stripPunctuation && match.length !== text.length })
496
- )
497
- }
498
-
499
- /** Whether a string is large enough, and shaped enough like base64, to be a blob. */
500
- function isBinaryBlob(value: string): boolean {
501
- return (
502
- value.length >= SIZE_GATE &&
503
- (BASE64_PATTERN.test(value) ||
504
- isBase64DataUrl(value) ||
505
- (BASE64URL_SPECIFIC_CHAR_PATTERN.test(value) && BASE64URL_PATTERN.test(value)))
506
- )
507
- }
508
-
509
- /** Replaces PostHog API keys wherever they appear in a string. */
510
- function redactCredentials(value: string): string {
511
- return value.replace(POSTHOG_TOKEN_PATTERN, REDACTED_VALUE)
512
- }
513
-
514
- /** Replaces every URL-shaped match, credentials and all. */
515
- function redactUrls(value: string): string {
516
- return sanitizeUrlsInString(value, { allowNestedUrls: true, stripPunctuation: true })
517
- }
518
-
519
- /**
520
- * Runs `redact` over only the head of `value` that truncation can keep, so a
521
- * multi-megabyte tool result costs about the same as the part captured. The head
522
- * ends where no URL crosses it, because a URL is redacted or kept on the whole
523
- * address. Redaction shrinks text, so a head that ends up too short to cover
524
- * truncation's cut falls back to the whole string. Either way the kept part
525
- * reads exactly as if the whole string were redacted.
526
- */
527
- function redactCapturedHead(value: string, redact: (value: string) => string): string {
528
- if (value.length > SCANNED_HEAD_LENGTH) {
529
- const head = redact(value.slice(0, headLength(value)))
530
- if (head.length >= MIN_REDACTED_HEAD_LENGTH) {
531
- return head + TRUNCATION_SUFFIX
532
- }
533
- }
534
- return redact(value)
535
- }
536
-
537
- /** The scanned head, shortened to end just after the last character that ends a URL. */
538
- function headLength(value: string): number {
539
- let end = SCANNED_HEAD_LENGTH
540
- while (end > 0 && !URL_END_PATTERN.test(value[end - 1])) {
541
- end--
542
- }
543
- return end
544
- }
545
-
546
- function sanitizeString(value: string): string {
547
- return isBinaryBlob(value)
548
- ? BINARY_REDACTED_VALUE
549
- : redactCapturedHead(value, (head) => redactUrls(redactCredentials(head)))
550
- }
551
-
552
- /**
553
- * Sanitizes agent-narrated free text (`$mcp_intent`, the `send_feedback`
554
- * fields): structured PII on top of the passes every captured string gets.
555
- *
556
- * Every step of the order is load-bearing. The binary gate reads the value as it
557
- * arrived, because splicing `[redacted]` into a blob — a Luhn-valid run inside
558
- * base64 is enough — stops it looking like base64 and would ship it whole.
559
- * Credentials go before PII, because a PII pattern can cut a token in half (a
560
- * phone-shaped run inside one) and leave the halves behind. PII goes before the
561
- * URL pass, because the URL rewrite percent-encodes the `@` that the email
562
- * pattern anchors on.
563
- */
564
29
  export function sanitizeFreeText(value: string): string {
565
- if (isBinaryBlob(value)) {
566
- return BINARY_REDACTED_VALUE
567
- }
568
- return redactCapturedHead(value, (head) => redactUrls(redactPii(redactCredentials(head))))
569
- }
570
-
571
- function passesLuhn(digits: string): boolean {
572
- let sum = 0
573
- let double = false
574
- for (let index = digits.length - 1; index >= 0; index--) {
575
- let digit = digits.charCodeAt(index) - 48
576
- if (digit < 0 || digit > 9) {
577
- return false
578
- }
579
- if (double) {
580
- digit *= 2
581
- if (digit > 9) {
582
- digit -= 9
583
- }
584
- }
585
- sum += digit
586
- double = !double
587
- }
588
- return sum % 10 === 0
589
- }
590
-
591
- // A card candidate can span more than one card plus adjacent fields (e.g. an
592
- // expiry, or a second card). This redacts every card in it: scanning
593
- // left-to-right over the whole separator-delimited digit groups, at each start it
594
- // takes the longest run whose joined digits are 13–19 long and pass Luhn, redacts
595
- // just that span, and resumes after it; a group that begins no valid run is kept
596
- // and skipped. Checking group-aligned runs rather than arbitrary digit windows
597
- // keeps the false-positive rate at Luhn's own ~1-in-10, instead of letting a
598
- // chance-valid sub-window of an ordinary long ID trigger redaction.
599
- function redactCardInMatch(match: string): string {
600
- const groups: { digits: string; start: number; end: number }[] = []
601
- for (let m = DIGIT_GROUP_PATTERN.exec(match); m !== null; m = DIGIT_GROUP_PATTERN.exec(match)) {
602
- groups.push({ digits: m[0], start: m.index, end: m.index + m[0].length })
603
- }
604
- let output = ''
605
- let cursor = 0
606
- let first = 0
607
- while (first < groups.length) {
608
- let digits = ''
609
- let matchedLast = -1
610
- for (let last = first; last < groups.length; last++) {
611
- digits += groups[last].digits
612
- if (digits.length > 19) {
613
- break
614
- }
615
- if (digits.length >= 13 && passesLuhn(digits)) {
616
- matchedLast = last
617
- }
618
- }
619
- if (matchedLast >= 0) {
620
- output += match.slice(cursor, groups[first].start) + REDACTED_VALUE
621
- cursor = groups[matchedLast].end
622
- first = matchedLast + 1
623
- } else {
624
- first++
625
- }
626
- }
627
- return output + match.slice(cursor)
628
- }
629
-
630
- /**
631
- * Redacts structured personal identifiers (emails, IP addresses, credit-card
632
- * numbers, US SSNs, and phone numbers) from a free-text string. Intended for the
633
- * agent-narrated `$mcp_intent` value only — not for structured tool parameters or
634
- * responses, where the same shapes are often legitimate data. Horizontal Unicode
635
- * spaces are first normalized to an ASCII space so copy-pasted identifiers still
636
- * match. Returns a new string; leaves the input's identifiers untouched when
637
- * nothing matches.
638
- */
639
- export function redactPii(value: string): string {
640
- let result = value.replace(UNICODE_SPACE_PATTERN, ' ')
641
- result = result.replace(EMAIL_PATTERN, REDACTED_VALUE)
642
- result = result.replace(IPV4_PATTERN, REDACTED_VALUE)
643
- result = result.replace(IPV6_PATTERN, REDACTED_VALUE)
644
- result = result.replace(CREDIT_CARD_CANDIDATE_PATTERN, redactCardInMatch)
645
- result = result.replace(US_SSN_PATTERN, REDACTED_VALUE)
646
- result = result.replace(PHONE_NANP_PATTERN, REDACTED_VALUE)
647
- result = result.replace(PHONE_INTL_PATTERN, REDACTED_VALUE)
648
- return result
30
+ return sanitizeCoreFreeText(value, SANITIZATION_OPTIONS)
649
31
  }
650
32
 
651
33
  export function sanitizeCapturedValue(value: unknown): unknown {
652
- return sanitizeValueWith(value, sanitizeString)
34
+ return sanitizeCoreCapturedValue(value, SANITIZATION_OPTIONS)
653
35
  }
654
36
 
655
- /**
656
- * {@link sanitizeCapturedValue} with the intent-grade string pass
657
- * ({@link sanitizeFreeText}) on every string leaf, so nested agent-narrated
658
- * values get structured-PII redaction in the load-bearing order too.
659
- */
660
37
  export function sanitizeFreeTextValue(value: unknown): unknown {
661
- return sanitizeValueWith(value, sanitizeFreeText)
662
- }
663
-
664
- function sanitizeValueWith(value: unknown, sanitizeStringFn: (value: string) => string): unknown {
665
- if (value == null) {
666
- return value
667
- }
668
-
669
- if (typeof value === 'string') {
670
- return sanitizeStringFn(value)
671
- }
672
-
673
- if (Array.isArray(value)) {
674
- return value.map((item) => sanitizeValueWith(item, sanitizeStringFn))
675
- }
676
-
677
- if (value instanceof Date) {
678
- return value
679
- }
680
-
681
- if (typeof value !== 'object') {
682
- return value
683
- }
684
-
685
- const result: JsonRecord = {}
686
- for (const [key, nestedValue] of Object.entries(value)) {
687
- result[key] = shouldRedactKey(key) ? REDACTED_VALUE : sanitizeValueWith(nestedValue, sanitizeStringFn)
688
- }
689
- return result
38
+ return sanitizeCoreFreeTextValue(value, SANITIZATION_OPTIONS)
690
39
  }
691
40
 
692
41
  function buildCapturedMcpArguments(argumentsValue: unknown): unknown {