Skip to content

fix(cli/slack): incomplete .env parsing undetected + failed download leaves corrupt file #535

@hrygo

Description

@hrygo

Background

internal/cli/slack 是 Slack CLI 子命令模块。loadEnvFile 未检查 scanner 错误可能导致 .env 不完整解析无提示,DownloadFile 在下载失败时留下损坏文件。

Scope: error-handling — cycle 192 (模块分析通过 2)
Key files: client.go, download.go


Finding Summary

Category Critical High Medium Low
Error-handling 0 0 2 0
合计 0 0 2 0

Findings

loadEnvFile-scanner-error-unchecked

Severity: Medium | Confidence: High | ROI: Medium
Location: client.go:73-91

Problem: loadEnvFile 的 for/s.Scan() 循环后未检查 s.Err()。如果 .env 文件在读取中途遇到 I/O 错误,部分环境变量被设置而其余被静默丢弃。调用者无法知道解析不完整。

Current Pattern:

// client.go:73-91
func loadEnvFile(path string) {
    // ...
    s := bufio.NewScanner(f)
    for s.Scan() {
        // process lines
    }
    // s.Err() never checked — silent partial parse
}

Proposed Fix: 循环后添加 if err := s.Err(); err != nil { return fmt.Errorf("scan .env: %w", err) },将 loadEnvFile 改为返回 error。


download-file-corrupt-on-failure

Severity: Medium | Confidence: High | ROI: Medium
Location: download.go:28-30

Problem: DownloadFileclient.GetFileContext 失败后,已写入的部分数据仍留在磁盘上。defer f.Close() 运行但文件未删除。用户可能将不完整文件当作有效下载使用。

Proposed Fix: 在 GetFileContext 错误路径中显式关闭并删除文件:

if _, err := io.Copy(f, resp.Body); err != nil {
    f.Close()
    os.Remove(outputPath)
    return fmt.Errorf("download file: %w", err)
}

Acceptance Criteria:

  • loadEnvFile 检查 s.Err() 并返回错误
  • DownloadFile 在失败时删除部分文件
  • go test ./internal/cli/slack/... 通过

Implementation Priority

Finding Priority Effort Risk Impact
loadEnvFile scanner error P2 Small Low 检测不完整 .env 加载
download corrupt file P2 Small Low 防止用户使用损坏文件

Verification

  • go test ./internal/cli/slack/... 通过
  • make lint 无新警告

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Medium: tech debt, refactoring, improvementsarchitectureDomain: design patterns, coupling, separation of concerns

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions