Skip to content

bug(terraform): GIT_TERMINAL_PROMPT save/restore race in remote module resolver #10833

Description

@nikpivkin

Summary

remoteResolver.download() saves, sets and restores the process-global GIT_TERMINAL_PROMPT environment variable on every call:

// pkg/iac/scanners/terraform/parser/resolvers/remote.go:88-93
terminalPrompt := os.Getenv("GIT_TERMINAL_PROMPT")
os.Setenv("GIT_TERMINAL_PROMPT", "0")
defer os.Setenv("GIT_TERMINAL_PROMPT", terminalPrompt)

The variable is process-global, so concurrent download() calls create a TOCTOU window. This is not a memory data race (Go serializes environment access internally) but a logical one.

  1. Lost original value. Goroutine B reads "0" (set by A) as the original, then restores to "0" on exit. The user's original value is lost for the rest of the process, affecting later git operations including pkg/downloader.
  2. Prompt re-enabled mid-clone. Goroutine A's deferred restore turns the prompt back on while B's git clone is still running, so git blocks waiting for interactive credentials on a non-interactive server instead of failing fast (hang / soft DoS).

Reachability

  • trivy config / trivy fs: module resolution is sequential — not affected.
  • trivy image / trivy vm: per-layer PostAnalyze runs concurrently (--parallel, default 5), so two layers can invoke the Terraform scanner and download() at the same time. Reachable within a single CLI process.
  • Trivy embedded as a library with concurrent Terraform scans.

Open question

Setting GIT_TERMINAL_PROMPT=0 once makes it process-wide, visible to embedders. This is acceptable in practice — the variable only controls whether git prompts for credentials interactively, which no application embedding a scanner relies on, and pkg/downloader already runs without restoring it. Confirm this is acceptable before implementing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugCategorizes issue or PR as related to a bug.scan/misconfigurationIssues relating to misconfiguration scanning

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions