From ffa44906c0da4de196ab31e9a0d2d1010ed306ae Mon Sep 17 00:00:00 2001 From: Karin Hendrikse <30577427+khendrikse@users.noreply.github.com> Date: Wed, 28 Oct 2026 11:58:08 +0100 Subject: [PATCH] fix: clean properties we send --- src/commands/base-command.ts | 42 ++++++++++++++++------------- tests/integration/telemetry.test.ts | 36 ++++++++++++++----------- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/src/commands/base-command.ts b/src/commands/base-command.ts index a4c467d7fd4..c91f082a25d 100644 --- a/src/commands/base-command.ts +++ b/src/commands/base-command.ts @@ -87,23 +87,25 @@ const COMMANDS_WITHOUT_WORKSPACE_OPTIONS = new Set([ */ const COMMANDS_WITH_FEATURE_FLAGS = new Set(['build', 'dev', 'deploy']) -/** - * Names of options whose values should be scrubbed - */ -const SCRUBBED_OPTIONS = new Set(['auth']) - -const getScrubbedOptions = (command: BaseCommand): Record => - Object.entries(command.optsWithGlobals()).reduce( - (acc: Record, [key, value]) => ({ - ...acc, - [key]: { - source: command.getOptionValueSourceWithGlobals(key), - value: SCRUBBED_OPTIONS.has(key) ? '********' : value, - }, - }), - {}, +// Each value sent becomes an Amplitude property; free-text values can carry user secrets. +const TELEMETRY_FLAGS_WITH_VALUES = new Set(['command']) + +const USER_SET_OPTION_SOURCES = new Set(['cli', 'env']) + +const getUserSetFlagNames = (command: BaseCommand): string[] => + Object.keys(command.optsWithGlobals()).filter((key) => + USER_SET_OPTION_SOURCES.has(command.getOptionValueSourceWithGlobals(key) ?? ''), ) +const getAllowlistedFlagValues = (command: BaseCommand): Record => { + const options: Record = command.optsWithGlobals() + return Object.fromEntries( + getUserSetFlagNames(command) + .filter((key) => TELEMETRY_FLAGS_WITH_VALUES.has(key)) + .map((key) => [key, { value: options[key] }]), + ) +} + /** Formats a help list correctly with the correct indent */ const formatHelpList = (textArray: string[]) => textArray.join('\n').replace(/^/gm, ' '.repeat(HELP_INDENT_WIDTH)) @@ -699,7 +701,10 @@ export default class BaseCommand extends Command { if (api.accessToken && !flags.offline && needsFeatureFlagsToResolveConfig && actionCommand.siteId) { try { // FIXME(serhalp): Remove `any` and fix errors. API types exist now. - const site = await (api as any).getSite({ siteId: actionCommand.siteId, feature_flags: 'cli' }) + const site = await (api as any).getSite({ + siteId: actionCommand.siteId, + feature_flags: 'cli', + }) actionCommand.featureFlags = site.feature_flags actionCommand.accountId = site.account_id } catch { @@ -757,8 +762,9 @@ export default class BaseCommand extends Command { monorepo: Boolean(this.project.workspace), packageManager: this.project.packageManager?.name, buildSystem: this.project.buildSystems.map(({ id }) => id), - opts: getScrubbedOptions(actionCommand), - args: actionCommand.args, + flags: getUserSetFlagNames(actionCommand), + opts: getAllowlistedFlagValues(actionCommand), + argCount: actionCommand.args.length, }) // set the project and the netlify api object on the command, diff --git a/tests/integration/telemetry.test.ts b/tests/integration/telemetry.test.ts index 6e8286f4575..bdba4217676 100644 --- a/tests/integration/telemetry.test.ts +++ b/tests/integration/telemetry.test.ts @@ -105,8 +105,9 @@ await withMockApi(routes, () => { didEnableCompileCache: expect.any(Boolean), monorepo: false, nodejsVersion, + flags: [], opts: expect.any(Object), - args: expect.any(Array), + argCount: expect.any(Number), // TODO: this should be NPM however some CI tests are using pnpm which changes the value packageManager: expect.stringMatching(/npm|pnpm/), }) @@ -129,8 +130,9 @@ await withMockApi(routes, () => { didEnableCompileCache: expect.any(Boolean), monorepo: false, nodejsVersion, + flags: [], opts: expect.any(Object), - args: expect.any(Array), + argCount: expect.any(Number), // TODO: this should be NPM however some CI tests are using pnpm which changes the value packageManager: expect.stringMatching(/npm|pnpm/), }) @@ -157,7 +159,7 @@ await withMockApi(routes, () => { }) }) - test('should attach user options as `opts` and positional args as `args`', async ({ + test('should attach user-set flag names as `flags` and the positional arg count as `argCount`', async ({ apiUrl, requests, }) => { @@ -172,31 +174,33 @@ await withMockApi(routes, () => { 'properties', expect.objectContaining({ command: 'blobs:get', - opts: { - filter: { source: 'cli', value: 'web' }, - output: { source: 'cli', value: './output_dir' }, - }, - args: ['my-store', 'my-key'], + flags: ['filter', 'output'], + opts: {}, + argCount: 2, }), ) }) - test('should scrub values of sensitive `opts`', async ({ apiUrl, requests }) => { - await callCli(['api', 'listSites', '--auth', 'my-sensitive-token'], getCLIOptions(apiUrl)) + test('should not send option values or positional args', async ({ apiUrl, requests }) => { + await callCli( + ['api', 'listSites', '--auth', 'my-sensitive-token', '--data', '{"filter":"my-filter-value"}'], + getCLIOptions(apiUrl), + ) const request = requests.find(({ path }) => path === '/api/v1/track') expect(request).toBeDefined() - expect(request!.body).toHaveProperty('event', 'cli:command') expect(request!.body).toHaveProperty( 'properties', expect.objectContaining({ command: 'api', - opts: { - auth: { source: 'cli', value: '********' }, - list: { source: 'default', value: false }, - }, - args: ['listSites'], + flags: ['auth', 'data'], + opts: {}, + argCount: 1, }), ) + const sentPayload = JSON.stringify(request!.body) + expect(sentPayload).not.toContain('my-sensitive-token') + expect(sentPayload).not.toContain('my-filter-value') + expect(sentPayload).not.toContain('listSites') }) })