diff --git a/src/commands/base-command.ts b/src/commands/base-command.ts index bf8a9f7ed4b..4271dd37feb 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 93f39fa5e65..e2c202b45ee 100644 --- a/tests/integration/telemetry.test.ts +++ b/tests/integration/telemetry.test.ts @@ -117,8 +117,9 @@ test('should send invoked command on success', async () => { 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/), }) @@ -143,8 +144,9 @@ test('should send invoked command on failure', async () => { 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/), }) @@ -174,8 +176,10 @@ test('should add frameworks, buildSystem, and packageManager', async (t) => { }) }) -test('should attach user options as `opts` and positional args as `args`', async () => { - await withMockApi(routes, async ({ apiUrl, requests }) => { + test('should attach user-set flag names as `flags` and the positional arg count as `argCount`', async ({ + apiUrl, + requests, + }) => { await expect( callCli(['blobs:get', '--filter', 'web', '-O', './output_dir', 'my-store', 'my-key'], getCLIOptions(apiUrl)), ).rejects.toThrow(/You don't appear to be in a folder that is linked to a project/) @@ -187,33 +191,34 @@ test('should attach user options as `opts` and positional args as `args`', async '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 () => { - await withMockApi(routes, 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') }) })