diff --git a/src/bin.ts b/src/bin.ts index c5e95920..e5666ff5 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -3361,10 +3361,15 @@ async function runCli(): Promise { yargs, 'env', 'Show WORKOS_* environment variables and their effects', - (y) => y, - async () => { + (y) => + y.option('show-secrets', { + type: 'boolean', + default: false, + describe: 'Show unredacted API keys', + }), + async (argv) => { const { runDebugEnv } = await import('./commands/debug.js'); - await runDebugEnv(); + await runDebugEnv({ showSecrets: argv.showSecrets as boolean }); }, ); registerSubcommand( diff --git a/src/commands/debug.spec.ts b/src/commands/debug.spec.ts index 25e472c0..6777b664 100644 --- a/src/commands/debug.spec.ts +++ b/src/commands/debug.spec.ts @@ -572,7 +572,7 @@ describe('debug commands', () => { it('shows set env vars with values', async () => { process.env.WORKOS_FORCE_TTY = '1'; - await runDebugEnv(); + await runDebugEnv({ showSecrets: false }); const output = consoleOutput.join('\n'); expect(output).toContain('WORKOS_FORCE_TTY'); @@ -584,7 +584,7 @@ describe('debug commands', () => { it('shows unset env vars with descriptions', async () => { delete process.env.WORKOS_API_KEY; - await runDebugEnv(); + await runDebugEnv({ showSecrets: false }); const output = consoleOutput.join('\n'); expect(output).toContain('WORKOS_API_KEY'); @@ -595,7 +595,7 @@ describe('debug commands', () => { jsonMode = true; process.env.WORKOS_DEBUG = '1'; - await runDebugEnv(); + await runDebugEnv({ showSecrets: false }); const parsed = JSON.parse(consoleOutput[0]); expect(parsed.variables.WORKOS_DEBUG.value).toBe('1'); @@ -605,10 +605,36 @@ describe('debug commands', () => { delete process.env.WORKOS_DEBUG; }); + it('masks WORKOS_API_KEY in human and JSON output', async () => { + process.env.WORKOS_API_KEY = 'sk_test_abcdefghijklmnop'; + + await runDebugEnv({ showSecrets: false }); + jsonMode = true; + await runDebugEnv({ showSecrets: false }); + + const output = consoleOutput.join('\n'); + expect(output).not.toContain('sk_test_abcdefghijklmnop'); + expect(output).toContain('sk_t****mnop'); + expect(JSON.parse(consoleOutput[consoleOutput.length - 1]).variables.WORKOS_API_KEY.value).toBe('sk_t****mnop'); + + delete process.env.WORKOS_API_KEY; + }); + + it('shows WORKOS_API_KEY unmasked with --show-secrets', async () => { + jsonMode = true; + process.env.WORKOS_API_KEY = 'sk_test_abcdefghijklmnop'; + + await runDebugEnv({ showSecrets: true }); + + expect(JSON.parse(consoleOutput[0]).variables.WORKOS_API_KEY.value).toBe('sk_test_abcdefghijklmnop'); + + delete process.env.WORKOS_API_KEY; + }); + it('lists all known env vars', async () => { jsonMode = true; - await runDebugEnv(); + await runDebugEnv({ showSecrets: false }); const parsed = JSON.parse(consoleOutput[0]); expect(Object.keys(parsed.variables)).toContain('WORKOS_API_KEY'); diff --git a/src/commands/debug.ts b/src/commands/debug.ts index 91b77872..8e279aec 100644 --- a/src/commands/debug.ts +++ b/src/commands/debug.ts @@ -378,9 +378,9 @@ interface EnvVarInfo { * (env-var-catalog.spec.ts) scans the source for `process.env.WORKOS_*` reads * and fails if any are missing here, so this list can't silently drift. */ -export const ENV_VAR_CATALOG: { name: string; effect: string }[] = [ +export const ENV_VAR_CATALOG: { name: string; effect: string; secret?: boolean }[] = [ // Credentials - { name: 'WORKOS_API_KEY', effect: 'Bypasses credential resolution — used directly for API calls' }, + { name: 'WORKOS_API_KEY', effect: 'Bypasses credential resolution — used directly for API calls', secret: true }, { name: 'WORKOS_CLIENT_ID', effect: 'WorkOS client ID used during credential resolution' }, // Interaction & output { name: 'WORKOS_MODE', effect: 'Controls interaction behavior: human, agent, or CI' }, @@ -413,10 +413,10 @@ export const ENV_VAR_CATALOG: { name: string; effect: string }[] = [ }, ]; -export async function runDebugEnv(): Promise { - const vars: EnvVarInfo[] = ENV_VAR_CATALOG.map(({ name, effect }) => ({ +export async function runDebugEnv({ showSecrets }: { showSecrets: boolean }): Promise { + const vars: EnvVarInfo[] = ENV_VAR_CATALOG.map(({ name, effect, secret }) => ({ name, - value: process.env[name], + value: secret && !showSecrets ? maskSecret(process.env[name]) : process.env[name], effect, }));