Skip to content

CLI 安全: 避免 --api-key 明文传参 #10

@KenyonY

Description

@KenyonY

问题

当前所有命令都接受 `--api-key sk-xxx`,会进入:

  1. Shell 历史(`/.bash_history` / `/.zsh_history`)
  2. `ps aux` 输出(同机用户可见)
  3. 脚本日志/CI 日志

agent-cli-guide P2 明确要求 "密码禁止通过标志传递"。

目标

  1. 新增 `--api-key-file `:从文件读取 key,优先级高于环境变量
  2. `--api-key -` 表示从 stdin 读取(一行)
  3. 保留 `--api-key ` 但:
    • 在 help 中加 `(not recommended: shell history leak)`
    • 非 TTY 且值看起来像真实 key 时(正则 `^sk-|^AIza|^ant-`),向 stderr 打印一次 warning
  4. 更新 README / init 生成的 config.yaml 注释,推荐顺序:
    • 环境变量 > 配置文件 > `--api-key-file` > `--api-key`(不推荐)

实现要点

  • 在 `flexllm/cli/_common.py` 抽一个 `resolve_api_key(key, key_file)` helper 统一处理
  • ask/chat/batch/serve/mock/test/models/credits 都切过来

验收

  • `echo sk-xxx | flexllm ask "hi" --api-key -` 可用
  • `flexllm ask "hi" --api-key-file ~/.secret` 可用
  • `flexllm ask "hi" --api-key sk-xxx` 仍可用但 stderr warning
  • `--api-key` 与 `--api-key-file` 同时给 → 退出码 2 + 明确错误

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions