fix(cli): recognize --flag=value form in startup arg pre-scan#144
Open
Episkey-G wants to merge 1 commit into
Open
fix(cli): recognize --flag=value form in startup arg pre-scan#144Episkey-G wants to merge 1 commit into
Episkey-G wants to merge 1 commit into
Conversation
root.go init() 的启动期 os.Args 预扫描用精确比较 arg=="--base-url",只匹配 空格形式,漏识别 --base-url=X / --profile=X / -p X 等形式。连接类参数因此在 InitClientRuntime 构造 sdk.Config 前未生效:--base-url=X 被无视、照打旧地址。 --profile=X / -p X 尤其是安全问题:ConfigIns 指向错误 profile,OAuth token 刷新可能把别人的 Bearer 重放到当前请求(platform/client.go 已载明)。讽刺的是 config 命令的官方 Example 就写着 --profile=test,工具用了自己处理不了的形式。 这是 issue #119 为 --wait-timeout-sec 修过的同一个 bug;parseWaitTimeoutSec 用 strings.CutPrefix 已是双形式模板,其余 flag 当年没跟着改。本次抽通用 helper scanFlagValue(args, names...)(支持 --profile/-p 多别名),替换 init() 7 处精确 比较扫描。附着/组合短选项(-pfoo/-dpfoo)有意不处理(手工解析组合短选项易错, 残留风险与现状持平),TestScanFlagValue 用 -pfoo→("",false) 守住该边界。 不含 -h/resetHelpFunc(用户裁定不改)。--profile=X 行为变更(静默失效→真正 生效)需入发版说明。CI 不跑 ./cmd/,用例须本地跑。
|
🔴 平台 PR 默认硬拦,需管理员 Approve 放行(或由管理员提交)。判定:改动触及平台/受保护路径(cmd/root.go, cmd/root_test.go) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
cmd/root.go的init()在 cobra 解析之前手写扫描 os.Args,把连接类参数落到global.*(时序约束:这些参数须在InitClientRuntime构造sdk.Config前定型)。但扫描用精确比较arg == "--base-url",只匹配空格形式:--base-url X(两个 argv)→ 匹配 ✅--base-url=X(一个 argv)→ 不匹配,被静默无视 ❌受影响:
--base-url、--profile(含短选项-p)、--public-key、--private-key、--channel-key、--timeout-sec、--max-retry-times。这不是新 bug — issue #119 已为一个 flag 修过
parseWaitTimeoutSec(root.go)用strings.CutPrefix(arg, flag+"=")已是双形式模板,并有表驱动测试TestParseWaitTimeoutSec。其余 flag 当年没跟着改。本 PR 把该修复推广开。--profile=X/-p X是安全问题platform/client.go注释已载明:扫描漏识别-p X/--profile=X→ ConfigIns 指向另一个 profile → OAuth token 刷新可能把别人的 Bearer 重放到当前请求。讽刺的是config命令的官方 Example 就写着--profile=test。实测:今天这些形式操作的是错误的 profile
两个 profile(default 活动 / prod 非活动,base_url 各不同),命令意图操作 prod:
--profile)--profile prod(空格)--profile=prod(等号)-p prod(短选项空格)-p=prod(短等号)注意
-p prod:文档明载的短选项,今天操作的却是 default 而非 prod。用户跑ucloud uhost list -p prod拿到的是 default 的列表。这不是「打断正常使用」,是纠正一个一直在悄悄用错 profile 的写法。方案
抽通用 helper
scanFlagValue(args, names...)(模仿parseWaitTimeoutSec,支持--profile/-p多别名),识别name value与name=value两形式,空值视为未命中。init()的 7 处精确比较扫描全部改为调它。附着/组合短选项(
-pfoo、-dpfoo)有意不处理:手工解析组合短选项易错,且其残留风险与现状持平(现状-p任何形式都不早识别)。TestScanFlagValue用-pfoo → ("", false)守住该边界。parseWaitTimeoutSec及其 #119 测试保留不动。测试
TestScanFlagValue(10 用例):长/短 × 空格/等号、空值、末尾无值、附着不识别、多别名、靠前优先。TestParseWaitTimeoutSec仍绿。go test ./...全绿,go vet干净。--base-url=X亦实测:master 无视、照打旧地址,本 PR 生效。=识别,等号用例立刻红 → 证明测试守护该修复。行为变更(需入发版说明)
--flag=value等号形式此前静默失效、现生效。尤其:空格形式(
--flag value)与不带 flag 的行为零变化。属安全修复(消除 profile/Bearer 错配窗口)+ 能力恢复(等号形式生效)。不在本 PR 范围
-h/resetHelpFunc(与本问题同源,但已裁定不改)。-pfoo/-dpfoo)。root.go+root_test.go,不碰 configure.go,与 fix(config): reject config writes when remote validation fails #142/feat(config): validate remotely only when validation-relevant fields change #143 无冲突,可独立合入。