Files
Pamela Chia 27578cdfb5 fix(telemetry): send search query shape instead of raw text (#53500)
Search telemetry sent whatever people typed, and search boxes are a
place where people paste credentials and personal data. I replace the
raw text with a description of it. #53336 added the raw `query` to
`kb_search_submitted`; docs search and the command menu already sent it.

**Changed:**
- **I send a description of the search term**: `kb_search_submitted`,
`docs_search_v2_search_submitted`, `docs_search_v2_result_clicked` and
`command_menu_search_submitted` now send `queryLength` and
`queryWordCount`, measured after trimming and collapsing repeated
whitespace.
- **Text only for keywords**: the term itself (`query`, `value`, and
`search_query` on `command_menu_command_clicked`) is sent only when it
is up to 4 letters-only parts of at most 16 letters each, 40 characters
in total. Keys, tokens, emails, IDs, project refs and connection strings
never match. Over the last 30 days that shape covers about 96% of
command menu searches, 88% of docs searches and 73% of KB searches, so
term-level analysis such as no-result queries keeps most of its data.
- **I removed `keywords` from the auth users search event**:
`auth_users_search_submitted` no longer carries it. Those terms identify
customers' end users, and I confirmed nothing downstream reads them.
- **The `keywords` URL parameter is redacted wherever telemetry copies a
URL**: the Auth users page keeps the search term in its URL, so I
replace the value with `keywords=redacted` in the page URL, query string
and referrer we send, and in the posthog-js `before_send` hook for the
URL properties the SDK fills in on its own (`$session_entry_url`,
`$initial_current_url`, `$referrer`, `$initial_referrer`). The URL in
the browser is unchanged.

**Note:** a letters-only name or passphrase still matches the keyword
shape, and longer questions lose their text. I opened this in place of
#53435, which removed `query` from the KB event only.

## To test

Tested on the PR previews (docs, kb, studio-staging), reading the
request bodies sent to `/platform/telemetry/event`:
- [x] Docs: open search and type `auth`, expect
`docs_search_v2_search_submitted` with `query: "auth"`, `queryLength: 4`
and `queryWordCount: 1`. Observed exactly that, with `hasResults: true`.
- [x] Docs: type `person@example.com`, expect the same event with
`queryLength: 18` and no `query`. Observed, and no telemetry body in
that step contained the address.
- [x] KB: search `how do I reset my database password`, expect
`kb_search_submitted` with `queryLength: 35`, `queryWordCount: 7` and no
`query`. Observed.
- [x] KB: search `storage policies` with two spaces, expect
`kb_search_submitted` with `query: "storage policies"` and `queryLength:
16`. Observed, with `queryWordCount: 2`.
- [x] Studio: open the command menu and type `abcdefghijklmnopqrst`,
expect `command_menu_search_submitted` with no `value`, then type `auth`
and expect `value: "auth"`. Observed both, with `queryLength` 20 and 4.
- [x] Studio: on Auth > Users, search `person@example.com`, expect
`auth_users_search_submitted` with no `keywords`. Observed: `trigger`,
`sort_column`, `sort_order`, `providers` and `filter_column` only.
- [x] Studio: with `?keywords=person%40example.com` in the Auth > Users
URL, reload, then trigger an event on the page, expect `page_url` and
`ph.search` in the request body to carry `keywords=redacted` and the
address bar to keep the real term. Observed on all three events from
that page, and the address bar kept the term.
- [x] Studio: from that URL, do a full page load to another project page
and trigger an event, expect `ph.referrer` to carry `keywords=redacted`
while `document.referrer` keeps the real term. Observed on all three
events from the new page.
- [x] Studio: after that session, query the staging analytics project
for events from the preview host, expect no property value to carry the
address or anything but `keywords=redacted`. Observed: none of the
events in the test window carried the address, and the SDK's own `$set`,
`$pageview` and `$pageleave` events arrived with `keywords=redacted` in
`$current_url`, `$referrer` and `$initial_current_url`. The session
started on a page without the parameter, so `$session_entry_url` was not
exercised here; the unit test on the `before_send` hook covers it.

## Linear
- fixes GROWTH-1501
2026-11-18 23:29:35 +09:00

191 lines
5.6 KiB
TypeScript

import { AuthUsersSearchSubmittedEvent } from 'common/telemetry-constants'
import { SearchIcon, XIcon } from 'lucide-react'
import { parseAsString, parseAsStringEnum, useQueryState } from 'nuqs'
import { Dispatch, forwardRef, SetStateAction } from 'react'
import {
Button,
cn,
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectSeparator,
SelectTrigger,
SelectValue,
Tooltip,
TooltipContent,
TooltipTrigger,
} from 'ui'
import { Input } from 'ui-patterns/DataInputs/Input'
import {
PHONE_NUMBER_LEFT_PREFIX_REGEX,
SpecificFilterColumn,
UUIDV4_LEFT_PREFIX_REGEX,
} from './Users.constants'
import { onSearchInputEscape } from '@/lib/keyboard'
import { useTrack } from '@/lib/telemetry/track'
const getSearchPlaceholder = (column: SpecificFilterColumn): string => {
switch (column) {
case 'id':
return 'Search by user ID'
case 'email':
return 'Search by email'
case 'name':
return 'Search by name'
case 'phone':
return 'Search by phone'
case 'freeform':
return 'Search by user ID, email, phone or name'
default:
return 'Search users...'
}
}
interface UsersSearchProps {
search: string
setSearch: Dispatch<SetStateAction<string>>
improvedSearchEnabled?: boolean
telemetryProps: Omit<AuthUsersSearchSubmittedEvent['properties'], 'trigger'>
onSelectFilterColumn: (value: SpecificFilterColumn) => void
}
export const UsersSearch = forwardRef<HTMLInputElement, UsersSearchProps>(function UsersSearch(
{ search, setSearch, improvedSearchEnabled = false, telemetryProps, onSelectFilterColumn },
ref
) {
const [, setSelectedId] = useQueryState(
'show',
parseAsString.withOptions({ history: 'push', clearOnDefault: true })
)
const [, setFilterKeywords] = useQueryState('keywords', { defaultValue: '' })
const [specificFilterColumn] = useQueryState<SpecificFilterColumn>(
'filter',
parseAsStringEnum<SpecificFilterColumn>([
'id',
'email',
'phone',
'name',
'freeform',
]).withDefault('email')
)
const track = useTrack()
const searchInvalid =
!search ||
specificFilterColumn === 'freeform' ||
specificFilterColumn === 'email' ||
specificFilterColumn === 'name'
? false
: specificFilterColumn === 'id'
? !search.match(UUIDV4_LEFT_PREFIX_REGEX)
: !search.match(PHONE_NUMBER_LEFT_PREFIX_REGEX)
const onSubmitSearch = () => {
const s = search.trim().toLocaleLowerCase()
setFilterKeywords(s)
setSelectedId(null)
track('auth_users_search_submitted', {
trigger: 'search_input',
...telemetryProps,
})
}
return (
<div className="flex items-center">
<div className="text-xs h-[26px] flex items-center px-1.5 border border-strong rounded-l-md bg-surface-300">
<SearchIcon size={14} />
</div>
<Select
value={specificFilterColumn}
onValueChange={(v) => onSelectFilterColumn(v as typeof specificFilterColumn)}
>
<SelectTrigger
size="tiny"
className={cn(
'w-[130px] bg-transparent! rounded-none -ml-px',
specificFilterColumn === 'freeform' && 'text-warning'
)}
>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem value="id" className="text-xs">
User ID
</SelectItem>
<SelectItem value="email" className="text-xs">
Email address
</SelectItem>
{improvedSearchEnabled && (
<SelectItem value="name" className="text-xs">
Name
</SelectItem>
)}
<SelectItem value="phone" className="text-xs">
Phone number
</SelectItem>
{!improvedSearchEnabled && (
<>
<SelectSeparator />
<Tooltip>
<TooltipTrigger>
<SelectItem value="freeform" className="text-xs">
Unified search
</SelectItem>
</TooltipTrigger>
<TooltipContent side="right" className="w-64 text-center">
Search by all columns at once, including mid-string search. May impact database
performance if you have many users.
</TooltipContent>
</Tooltip>
</>
)}
</SelectGroup>
</SelectContent>
</Select>
<Input
ref={ref}
size="tiny"
containerClassName="w-[245px] rounded-l-none -ml-px"
className={cn(
'bg-transparent',
searchInvalid ? 'text-red-900 dark:border-red-900' : '',
search.length > 1 && 'pr-6'
)}
placeholder={getSearchPlaceholder(specificFilterColumn)}
value={search}
onChange={(e) => setSearch(e.target.value)}
onKeyDown={(e) => {
if (e.code === 'Enter' || e.code === 'NumpadEnter') {
if (!searchInvalid) onSubmitSearch()
return
}
onSearchInputEscape(search, () => {
setSearch('')
setFilterKeywords('')
})(e)
}}
actions={
search ? (
<Button
size="tiny"
variant="text"
className="p-0 h-5 w-5"
icon={<XIcon className={cn(searchInvalid ? 'text-red-900' : '')} />}
onClick={() => {
setSearch('')
setFilterKeywords('')
}}
/>
) : null
}
/>
</div>
)
})