Skip to content

Commit 052960e

Browse files
committed
fix(config): mask all secret fields in config show
1 parent a8f45e9 commit 052960e

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

packages/commands/src/commands/config/show.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineCommand, detectOutputFormat, maskToken } from "bailian-cli-core";
22
import { emitResult } from "bailian-cli-runtime";
3+
import { SECRET_KEYS } from "./shared.ts";
34

45
export default defineCommand({
56
description: "Display current configuration",
@@ -20,13 +21,9 @@ export default defineCommand({
2021
config_file: store.path,
2122
};
2223

23-
if (typeof result.api_key === "string") result.api_key = maskToken(result.api_key);
24-
if (typeof result.access_token === "string")
25-
result.access_token = maskToken(result.access_token);
26-
if (typeof result.access_key_id === "string")
27-
result.access_key_id = maskToken(result.access_key_id);
28-
if (typeof result.access_key_secret === "string")
29-
result.access_key_secret = maskToken(result.access_key_secret);
24+
for (const key of SECRET_KEYS) {
25+
if (typeof result[key] === "string") result[key] = maskToken(result[key]);
26+
}
3027

3128
emitResult(result, format);
3229
},

packages/commands/tests/e2e/config.e2e.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,29 @@ describe("e2e: config", () => {
8181
expect(stdout).toMatch(/config_file|timeout|base_url/i);
8282
});
8383

84+
test("config show 脱敏 security_token", async () => {
85+
const configDir = mkdtempSync(join(tmpdir(), "bl-config-show-secret-"));
86+
try {
87+
const securityToken = "sts-sensitive-token";
88+
writeFileSync(
89+
join(configDir, "config.json"),
90+
JSON.stringify({ security_token: securityToken }, null, 2) + "\n",
91+
);
92+
93+
const { stdout, stderr, exitCode } = await runCommandE2e(
94+
CONFIG_ROUTES,
95+
["config", "show", "--output", "json"],
96+
{ BAILIAN_CONFIG_DIR: configDir },
97+
);
98+
expect(exitCode, stderr).toBe(0);
99+
const data = parseStdoutJson<{ security_token?: string }>(stdout);
100+
expect(data.security_token).toBe("sts-...oken");
101+
expect(stdout).not.toContain(securityToken);
102+
} finally {
103+
rmSync(configDir, { recursive: true, force: true });
104+
}
105+
});
106+
84107
test("config set 缺少 --key / --value 时报用法错误并退出 (2)", async () => {
85108
const { stderr, exitCode } = await runCommandE2e(CONFIG_ROUTES, ["config", "set", "--quiet"]);
86109
expect(exitCode, stderr).toBe(2);

0 commit comments

Comments
 (0)