Skip to content

Commit ac64e3e

Browse files
committed
fix: filter --use-keyring and --global-config from user global flags
These flags are managed by the extension and conflict with the auth mode it chooses. Passing --use-keyring=false with --url breaks keyring auth, and --global-config with --url causes the CLI to ignore the keyring entirely.
1 parent c2ee595 commit ac64e3e

File tree

3 files changed

+87
-16
lines changed

3 files changed

+87
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
]
151151
},
152152
"coder.globalFlags": {
153-
"markdownDescription": "Global flags to pass to every Coder CLI invocation. Enter each flag as a separate array item; values are passed verbatim and in order. Do **not** include the `coder` command itself. See the [CLI reference](https://coder.com/docs/reference/cli) for available global flags.\n\nNote that for `--header-command`, precedence is: `#coder.headerCommand#` setting, then `CODER_HEADER_COMMAND` environment variable, then the value specified here. The `--global-config` flag is explicitly ignored.",
153+
"markdownDescription": "Global flags to pass to every Coder CLI invocation. Enter each flag as a separate array item; values are passed verbatim and in order. Do **not** include the `coder` command itself. See the [CLI reference](https://coder.com/docs/reference/cli) for available global flags.\n\nNote that for `--header-command`, precedence is: `#coder.headerCommand#` setting, then `CODER_HEADER_COMMAND` environment variable, then the value specified here. The `--global-config` and `--use-keyring` flags are silently ignored as the extension manages them via `#coder.useKeyring#`.",
154154
"type": "array",
155155
"items": {
156156
"type": "string"

src/cliConfig.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,29 @@ export function getGlobalFlags(
3232
? ["--url", escapeCommandArg(auth.url)]
3333
: ["--global-config", escapeCommandArg(auth.configDir)];
3434

35-
// Last takes precedence/overrides previous ones
36-
return [
37-
...getGlobalFlagsRaw(configs),
38-
...authFlags,
39-
...getHeaderArgs(configs),
40-
];
35+
const raw = getGlobalFlagsRaw(configs);
36+
const filtered: string[] = [];
37+
for (let i = 0; i < raw.length; i++) {
38+
if (isFlag(raw[i], "--use-keyring")) {
39+
continue;
40+
}
41+
if (isFlag(raw[i], "--global-config")) {
42+
// Skip the next item too when the value is a separate entry.
43+
if (raw[i] === "--global-config") {
44+
i++;
45+
}
46+
continue;
47+
}
48+
filtered.push(raw[i]);
49+
}
50+
51+
return [...filtered, ...authFlags, ...getHeaderArgs(configs)];
52+
}
53+
54+
function isFlag(item: string, name: string): boolean {
55+
return (
56+
item === name || item.startsWith(`${name}=`) || item.startsWith(`${name} `)
57+
);
4158
}
4259

4360
/**

test/unit/cliConfig.test.ts

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,72 @@ describe("cliConfig", () => {
6868
]);
6969
});
7070

71-
it("should not filter duplicate global-config flags, last takes precedence", () => {
71+
it.each(["--use-keyring", "--use-keyring=false", "--use-keyring=true"])(
72+
"should filter %s from global flags",
73+
(managedFlag) => {
74+
const config = new MockConfigurationProvider();
75+
config.set("coder.globalFlags", [
76+
"--verbose",
77+
managedFlag,
78+
"--disable-direct-connections",
79+
]);
80+
81+
expect(getGlobalFlags(config, globalConfigAuth)).toStrictEqual([
82+
"--verbose",
83+
"--disable-direct-connections",
84+
"--global-config",
85+
'"/config/dir"',
86+
]);
87+
},
88+
);
89+
90+
interface GlobalConfigCase {
91+
scenario: string;
92+
flags: string[];
93+
}
94+
it.each<GlobalConfigCase>([
95+
{
96+
scenario: "space-separated in one item",
97+
flags: ["-v", "--global-config /path/to/ignored"],
98+
},
99+
{
100+
scenario: "equals form",
101+
flags: ["-v", "--global-config=/path/to/ignored"],
102+
},
103+
{
104+
scenario: "separate items",
105+
flags: ["-v", "--global-config", "/path/to/ignored"],
106+
},
107+
])(
108+
"should filter --global-config ($scenario) in both auth modes",
109+
({ flags }) => {
110+
const urlAuth: CliAuth = {
111+
mode: "url",
112+
url: "https://dev.coder.com",
113+
};
114+
const config = new MockConfigurationProvider();
115+
config.set("coder.globalFlags", flags);
116+
117+
expect(getGlobalFlags(config, globalConfigAuth)).toStrictEqual([
118+
"-v",
119+
"--global-config",
120+
'"/config/dir"',
121+
]);
122+
expect(getGlobalFlags(config, urlAuth)).toStrictEqual([
123+
"-v",
124+
"--url",
125+
'"https://dev.coder.com"',
126+
]);
127+
},
128+
);
129+
130+
it("should not filter flags with similar prefixes", () => {
72131
const config = new MockConfigurationProvider();
73-
config.set("coder.globalFlags", [
74-
"-v",
75-
"--global-config /path/to/ignored",
76-
"--disable-direct-connections",
77-
]);
132+
config.set("coder.globalFlags", ["--global-configs", "--use-keyrings"]);
78133

79134
expect(getGlobalFlags(config, globalConfigAuth)).toStrictEqual([
80-
"-v",
81-
"--global-config /path/to/ignored",
82-
"--disable-direct-connections",
135+
"--global-configs",
136+
"--use-keyrings",
83137
"--global-config",
84138
'"/config/dir"',
85139
]);

0 commit comments

Comments
 (0)