fix(auth): require resource mode value

This commit is contained in:
Tam Nhu Tran committed 2026-05-08 23:36:12 -04:00
1 parent bfe32257b6
commit 3b550205dd
3 files changed
+59 -7

No files matched your search

+15 -7
View File
@@ -46,6 +46,13 @@ export async function handleResources(ctx: CommandContext, args: string[]): Prom
const currentPolicy = resolveSharedResourcePolicy(currentProfile); const currentPolicy = resolveSharedResourcePolicy(currentProfile);
if (mode === '') {
exitWithError(
'Missing value for --mode: expected shared|profile-local',
ExitCode.PROFILE_ERROR
);
}
if (mode !== undefined && !isSharedResourceMode(mode)) { if (mode !== undefined && !isSharedResourceMode(mode)) {
exitWithError( exitWithError(
'Invalid shared resource mode: expected shared|profile-local', 'Invalid shared resource mode: expected shared|profile-local',
@@ -53,7 +60,7 @@ export async function handleResources(ctx: CommandContext, args: string[]): Prom
); );
} }
if (!mode) { if (mode === undefined) {
if (json) { if (json) {
console.log( console.log(
JSON.stringify( JSON.stringify(
@@ -92,7 +99,8 @@ export async function handleResources(ctx: CommandContext, args: string[]): Prom
: undefined; : undefined;
const previousLegacy = existsLegacy ? ctx.registry.getProfile(profileName) : undefined; const previousLegacy = existsLegacy ? ctx.registry.getProfile(profileName) : undefined;
const contextPolicy = resolveAccountContextPolicy(currentProfile); const contextPolicy = resolveAccountContextPolicy(currentProfile);
const metadata = sharedResourceModeToMetadata(mode); const selectedMode: SharedResourceMode = mode;
const metadata = sharedResourceModeToMetadata(selectedMode);
try { try {
if (existsUnified) { if (existsUnified) {
@@ -103,7 +111,7 @@ export async function handleResources(ctx: CommandContext, args: string[]): Prom
} }
await ctx.instanceMgr.ensureInstance(profileName, contextPolicy, { await ctx.instanceMgr.ensureInstance(profileName, contextPolicy, {
bare: mode === 'profile-local', bare: selectedMode === 'profile-local',
}); });
} catch (error) { } catch (error) {
if (existsUnified && previousUnified) { if (existsUnified && previousUnified) {
@@ -128,9 +136,9 @@ export async function handleResources(ctx: CommandContext, args: string[]): Prom
JSON.stringify( JSON.stringify(
{ {
name: profileName, name: profileName,
shared_resource_mode: mode, shared_resource_mode: selectedMode,
shared_resource_inferred: false, shared_resource_inferred: false,
bare: mode === 'profile-local' ? true : undefined, bare: selectedMode === 'profile-local' ? true : undefined,
}, },
null, null,
2 2
@@ -139,8 +147,8 @@ export async function handleResources(ctx: CommandContext, args: string[]): Prom
return; return;
} }
console.log(ok(`Shared resources for "${profileName}" set to ${formatMode(mode)}`)); console.log(ok(`Shared resources for "${profileName}" set to ${formatMode(selectedMode)}`));
console.log(''); console.log('');
console.log(modeDescription(mode)); console.log(modeDescription(selectedMode));
console.log(''); console.log('');
} }
+7
View File
@@ -77,6 +77,13 @@ describe('auth command args parsing', () => {
expect(parsed.mode).toBe('shared'); expect(parsed.mode).toBe('shared');
}); });
it('flags missing shared resource mode value as empty string', () => {
const parsed = parseArgs(['work', '--mode'], { allowMode: true });
expect(parsed.profileName).toBe('work');
expect(parsed.mode).toBe('');
});
it('treats shared resource mode as unknown unless the command opts in', () => { it('treats shared resource mode as unknown unless the command opts in', () => {
const parsed = parseArgs(['work', '--mode', 'shared']); const parsed = parseArgs(['work', '--mode', 'shared']);
+37
View File
@@ -172,6 +172,43 @@ describe('auth resources command', () => {
expect(account.bare).toBeUndefined(); expect(account.bare).toBeUndefined();
}); });
it('rejects resources --mode without a value instead of showing current mode', async () => {
const ccsDir = path.join(tempRoot, '.ccs');
fs.mkdirSync(ccsDir, { recursive: true });
fs.writeFileSync(
path.join(ccsDir, 'config.yaml'),
[
'version: 8',
'accounts:',
' work:',
' created: "2026-02-01T00:00:00.000Z"',
' last_used: null',
'profiles: {}',
'cliproxy:',
' oauth_accounts: {}',
' providers: {}',
' variants: {}',
].join('\n'),
'utf8'
);
const registry = new ProfileRegistry();
const instanceMgr = new InstanceManager();
const output = await expectProfileExit(() =>
handleResources(
{
registry,
instanceMgr,
version: 'test',
},
['work', '--mode']
)
);
expect(output).toContain('Missing value for --mode: expected shared|profile-local');
});
it('rejects --mode on auth create instead of silently ignoring it', async () => { it('rejects --mode on auth create instead of silently ignoring it', async () => {
const registry = new ProfileRegistry(); const registry = new ProfileRegistry();
const instanceMgr = new InstanceManager(); const instanceMgr = new InstanceManager();