Skip to content

Refuse Terraform state in the local preflight - #170

Open
galion96 wants to merge 1 commit into
prooflinedev:mainfrom
galion96:refuse-terraform-state
Open

galion96 wants to merge 1 commit into
prooflinedev:mainfrom
galion96:refuse-terraform-state

Conversation

@galion96

@galion96 galion96 commented Dec 6, 2026

Copy link
Copy Markdown

Who it helps

Teams that keep Terraform in the same repository they review with proofline review or the git hooks. That is most of Proofline's audience. A terraform.tfstate stores every resource attribute in plaintext, including RDS master passwords, generated TLS private keys, IAM access keys and backend credentials. It ends up in a local patch more often than it should: a repo without a Terraform .gitignore, a terraform state pull > prod.tfstate while debugging, or --include-untracked. Today the preflight refuses .env paths and known token shapes, but it lets state through. A state file usually contains no ghp_/AKIA prefix the line scanner recognises, so the whole file goes to the review server.

What changed

  • src/preflight.rs: a new path rule beside environment_paths. It refuses any old or new endpoint that is
    • a file ending in .tfstate or .tfstate.backup (this covers terraform.tfstate.d/<workspace>/terraform.tfstate), or
    • anything under a .terraform/ directory, which caches the backend state and configuration.
  • .terraform.lock.hcl, *.tf, *.tfvars and paths such as docs/tfstate.md are not affected. The lock file is meant to be committed and reviewed.
  • The refusal has its own PreflightError::TerraformStatePath variant and message. The message says why the file is refused and points to --allow-path. The same exact-path override works as it does for .env, and a rename needs both endpoints allowed.
  • README: one sentence beside the existing "usual secret preflight" mention for review --path.

Decisions

  • Path rule, not content rule. State is JSON with arbitrary attribute names, so content heuristics would miss values or flag ordinary JSON. The path is unambiguous and matches how .env is handled.
  • A separate error variant instead of widening the .env message, so the diagnostic tells the user why a Terraform file is sensitive.
  • .tfvars stays out. It often holds secrets too, but many teams commit non-secret *.tfvars on purpose, and refusing them would break normal reviews. Happy to add it behind the same override if you'd like.

How I verified it

  • Unit tests in preflight.rs cover:
    • recognised and ignored paths, including the lock file, .terraform-version and look-alike names
    • refusal on add, delete and each rename endpoint
    • the exact-endpoint --allow-path behaviour and the message text
  • An integration test in tests/local_patch.rs runs the real binary against a temporary checkout. The checkout holds infra/terraform.tfstate (with a fake password), infra/.terraform.lock.hcl and infra/main.tf. The test checks that the review is refused before any HTTP request and that the password never appears in stderr. It then checks that --allow-path infra/terraform.tfstate uploads the original patch unchanged, lock file included.
  • cargo fmt --check, cargo clippy --locked --all-targets -- -D warnings and cargo test --locked all pass on the pinned 1.100.0 toolchain: 149 unit tests plus every integration suite.

Terraform state files store resource attributes such as passwords and private keys in plaintext, but contain none of the token prefixes the line scanner recognises. Refuse *.tfstate, *.tfstate.backup and paths under .terraform/ before upload, with the same exact-path --allow-path override as .env. The committed .terraform.lock.hcl stays reviewable.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant