Repository navigation
fix: verify bundled terraform against a checksum file in the Coder repo - #31600
Conversation
403de77 to
8494caf
Compare
On branches with provisioner/terraform/terraform_SHA256SUMS (#31600), replace it with the verified SHA256SUMS and regenerate install.sh with the branch's scripts/gen_install_sh_terraform.sh, instead of editing install.sh and scripts/Dockerfile.base. Branches without it keep the existing behavior. Also check that install.sh ends up on the new version.
The NOTE above TerraformVersion is rewritten by #31600, which now points at this script, so drop the line added here to avoid a conflict.
…tall.sh
scripts/Dockerfile.base and install.sh --with-terraform downloaded the
bundled Terraform zip from releases.hashicorp.com without verifying it.
Pin the SHA256 of each release zip, copied from HashiCorp's GPG-signed
terraform_1.16.7_SHA256SUMS, and fail if the download doesn't match.
install.sh also unzipped ${ARCH} instead of ${TERRAFORM_ARCH}, which broke
--with-terraform on armv7.
Add provisioner/terraform/terraform_SHA256SUMS, a verbatim copy of HashiCorp's GPG-signed terraform_1.16.7_SHA256SUMS, as the only place the bundled Terraform checksums live. - scripts/Dockerfile.base copies it in and reads both the version and the checksum from it, so it no longer pins either. build_docker.sh adds it to the build context; docker-base.yaml and release.yaml need the same change. - install.sh is served standalone, so make gen regenerates its TERRAFORM_VERSION and terraform_sha256sums block from the file via scripts/gen_install_sh_terraform.sh. The gen job catches drift. - The generator fails if TerraformVersion in install.go doesn't match.
f7a0ae4 to
b61d4f7
Compare
install.sh pinned TERRAFORM_VERSION and embedded a generated copy of the checksums, while building the download URL from that version. Drop both. with_terraform now downloads provisioner/terraform/terraform_SHA256SUMS from the Coder repository (at the tag of the release being installed, falling back to main for releases from before the file existed), takes the Terraform version and checksum for the platform from it, and verifies the HashiCorp download against it. The only thing trusted is the Coder repository; anything from elsewhere is checksum-verified. This removes scripts/gen_install_sh_terraform.sh and its make gen target. A Go test now checks that terraform_SHA256SUMS matches TerraformVersion and covers the platforms Dockerfile.base builds for.
install.sh now reads the Terraform version and checksums from terraform_SHA256SUMS at install time (#31600), so on branches with that file the script only replaces it. The install.sh version check now only applies to branches that still pin TERRAFORM_VERSION.
Lisa-Fiander
left a comment
There was a problem hiding this comment.
see changes required on linked assessment
Addresses security review feedback on the checksum-file approach: - Fetch terraform_SHA256SUMS with fully qualified refs (refs/tags/v<version>, refs/heads/main) and only fall back to main when the release tag doesn't have the file (404), not on other errors. - Only accept well-formed checksum lines (two fields, 64 lowercase hex characters, terraform_X.Y.Z_<os>_<arch>.zip), validate the Terraform version and rebuild the zip name from validated parts instead of using the file name from the checksum file verbatim. - sh_c runs its arguments directly instead of re-parsing them with sh -c, and sudo_sh_c passes argv to sudo/doas as-is. The su fallback single-quotes every argument with quote_args. - Validate the resolved Coder version (X.Y.Z or X.Y.Z-rc.N) before it's used in URLs, file names and commands.
main now bundles Terraform 1.17.1 (#31438). This is HashiCorp's terraform_1.17.1_SHA256SUMS, verified against its GPG signature (key C874011F0AB405110D02105534365D9472D7468F).
adb5c87 dropped it by accident (100755 -> 100644).
f0ssel
left a comment
There was a problem hiding this comment.
Tested locally on f2f1f64 and it works as described.
Checksum file: exact copy of HashiCorp's terraform_1.17.1_SHA256SUMS. The signature checks out against C874 011F … 72D7 468F.
CI checks: the Go test passes, and gofmt, shellcheck, shfmt and actionlint are clean.
Dockerfile.base (amd64): builds and installs Terraform 1.17.1. With one hash changed, the build fails.
install.sh --with-terraform (dash, fake curl serving this file):
Installs Terraform 1.17.1 and Coder 2.29.0 into a folder with a space in its name, and into a root-owned folder through sudo.
A changed hash fails the install and deletes the cached zip.
A 404 on the tag falls back to main. A 500 or a network error fails without trying main.
Injected checksum lines, junk responses, unsupported architectures and bad --version values are refused, and nothing runs.
--stable, the default mainline, RC versions and --dry-run still work.
Shell quoting: every command install.sh runs as root passes plain arguments, so removing the sh -c step is safe. The su quoting round-trips spaces, quotes, ;, $(...) and backticks unchanged.
Not tested: arm64/armv7 images (CI covers them), macOS shasum, real doas/su, and the deb/rpm/apk install methods (I only read their code).
Note: tags cut from release branches only get their own checksums if this is backported. Otherwise they use main's file, which is also how it works today.
|
I have read the CLA Document and I hereby sign the CLA 2 out of 3 committers have signed the CLA. |
|
CLA check fails because Superblink commits are not signed (?) @kylecarbs what can be done here? I can squash PR under my name with signature as a workaround but generally speaking it limits Superblink use if it's PRs can't be accepted. |
cjfit
left a comment
There was a problem hiding this comment.
Security review (quick-pr-review): no findings at cb4b969.
- Hardening: today
install.sh --with-terraformdoesn't check the HashiCorp zip, andsh_c'ssh -c "$*"lets shell syntax in the version or a path run as a command. After this PR, the zip is checked against the hash from the Coder repo, the version must beX.Y.Z[-rc.N],sh_cruns its arguments as-is, andsugets every argument quoted. - Call sites: every
sh_c,sudo_sh_cand"$sh_c"caller passes separate arguments, so none relied on the old re-parsing. - Checksum file: it's fetched at
refs/tags/v<validated>and falls back torefs/heads/mainonly on a 404. Lines are strictly validated and the zip name is built from validated parts. A tampered file could at worst pick a different genuine HashiCorp release, and only someone who can already write tags ormaincould tamper with it. - Docker builds:
Dockerfile.basereads the checksum file from the build's own ref.docker-base.yamlstill never publishes (pull_request,push: false).
🤖 Generated with Claude Code
scripts/Dockerfile.baseandinstall.sh --with-terraformdownload the bundled Terraform zip fromreleases.hashicorp.comand use it without checking it. This verifies that zip against a checksum file kept in the Coder repo. The only thing trusted is the Coder repo, which is where theDockerfileandinstall.shthemselves come from. Everything fetched from HashiCorp is checked against it.The other download paths are already covered:
provisioner/terraform/install.gouseshc-install, which checks the GPG-signed SHA256SUMS, andmise.lock/flake.nixalready carry per-platform hashes.Source of truth
provisioner/terraform/terraform_SHA256SUMSis a verbatim copy of HashiCorp'sterraform_1.17.2_SHA256SUMS, the versionmainships, verified against their signing key (C874 011F 0AB4 0511 0D02 1055 3436 5D94 72D7 468F). Being verbatim, you can diff it against the published file or check it with the published.sig. Its hashes match the Terraform entries inmise.lockonmain. Both the Terraform version and the checksums are read from it, so neither consumer pins a version.Consumers
scripts/Dockerfile.basecopies the file in and reads the version and its architecture's checksum from it. It checks the zip withsha256sum -cbefore unzipping. The build fails if there's no entry for the architecture or the download doesn't match. All three build contexts include the file:docker-base.yaml,release.yamlandscripts/build_docker.sh.docker-base.yamlalso runs when the file changes.install.shno longer has aTERRAFORM_VERSIONor any hashes.--with-terraformdownloadsterraform_SHA256SUMSfrom the Coder repo atrefs/tags/v${VERSION}, the tag of the release being installed. Only if that tag doesn't have the file (a 404, for releases from before it existed) does it fall back torefs/heads/main, with a note in the output. Any other error fails. Fully qualified refs mean a branch named like the tag, or a tag namedmain, can't be read instead. It then:terraform_X.Y.Z_<os>_<arch>.zip. It picks the line for${OS}_${TERRAFORM_ARCH}, validates the Terraform version and builds the zip name from validated parts rather than using the one in the file. It refuses to install if there's no such line;sha256sum,shasum -a 256orsha256, including a zip reused from the cache. On a mismatch it deletes the zip so a rerun downloads it again;--dry-run, since nothing is downloaded. The checksum file is still fetched so the version can be printed.install.shcommand handling (security review):sh_cruns its arguments directly instead of re-parsing them withsh -c, andsudo_sh_cpasses them tosudoordoasas-is. Thesufallback single-quotes every argument (quote_args). The resolved Coder version must beX.Y.ZorX.Y.Z-rc.Nbefore it's used in URLs, file names or commands. As a side effect, install prefixes and cache paths with spaces now work.provisioner/terraform/checksums_test.gofails if the file isn't in SHA256SUMS format, is for a different version thanTerraformVersion, or lacks thelinux_{amd64,arm64,arm}entries thatDockerfile.baseneeds.Also fixes
install.shunzipping${ARCH}instead of${TERRAFORM_ARCH}, which broke--with-terraformon armv7 (armv7vsarm).Bumping after this
Replace
terraform_SHA256SUMSwith the new verified file and updateTerraformVersion(plusmise.toml,mise.lock,flake.nixand the airgap doc, as today).Dockerfile.baseandinstall.shneed no edits.scripts/update_terraform_version.sh(#31463) does all of this, and theTerraformVersionnote ininstall.gopoints at it.mainis merged into this branch, including #31438 (Terraform 1.17.1) and #32110 (Terraform 1.17.2), so the checksum file andTerraformVersionare at 1.17.2.Testing
After merging
main(Terraform 1.17.2), I re-ran the Go test,go vet,gofmt,shellcheck,shfmtandactionlint, and checked that theDockerfile.baseextraction resolves 1.17.2 for amd64, arm64 and arm. Thedocker-basebuild and theinstall.shandDockerfile.baseruns below were done before that merge, against 1.16.7 and 1.17.1, and I haven't repeated them at 1.17.2. Thedocker-baseworkflow rebuilds the image from the 1.17.2 file in CI.docker-baseworkflow built all three platforms from the checksum file:terraform_1.16.7_linux_{amd64,arm64,arm}.zip: OK, then Terraform v1.16.7 on each.Dockerfile.baseTerraform step under BusyBox 1.35sh. It fails with a changed hash or no entry for the architecture. With the verified 1.17.1 file it installs 1.17.1 with no Dockerfile edit.install.shwith_terraformunderdashon linux/amd64:raw.githubusercontent.comfrom this branch (standing in formain), then verified and installed 1.16.7;--dry-runprints the commands.mainhave the file, so--with-terraformfails with "Could not download the Terraform checksums" until this merges. After that, older releases usemain's file.dashandbash --posix:$(...), backticks,;,../, an extra field, an uppercase or 65-character hash, or a malformed Terraform version are refused before any command runs. The previous version of the script ran the$(...)one. A file with an injected line followed by the real ones installs only the real zip;curlstub, a tag hit readsrefs/tags/v2.39.0, a 404 falls back torefs/heads/main, and a 500 or a network error fails without tryingmain;sh_c, andsudo_sh_cthroughsudo,doasandsustubs, pass arguments with spaces, quotes,;and$(...)through unchanged and run nothing else.--dry-runruns nothing;--version v2.28.0,2.28.0;idand../../xare refused.--version 2.39.0,--version 2.33.0-rc.1,--stableand the default mainline still work;sudo), and of Coder 2.39.0 with--method standaloneinto a prefix with a space.go test ./provisioner/terraform -run TestTerraformChecksumFileMatchesVersionpasses with the 1.17.2 file, and fails with the 1.17.1 file in place (expected: "1.17.2", actual: "1.17.1").go vetandgofmtare clean.shellcheck --external-sources(v0.10.0) andshfmt -d(v3.12.0) are clean oninstall.shandscripts/build_docker.sh, andactionlint(v1.7.7) is clean on the workflows. Local git hooks were skipped.How #31600, #31463 and #31461 fit together
provisioner/terraform/terraform_SHA256SUMS(a verbatim copy of HashiCorp's signed file) the one place the bundled Terraform checksums live.Dockerfile.basereads it, andinstall.shdownloads it from the Coder repo at install time.scripts/update_terraform_version.sh. On branches with the checksum file it just replaces that file. On older branches it editsinstall.shandDockerfile.basedirectly.mainand the supported release branches. It needed no changes for the checksum file.None of the three touch the same files, and merging all three onto
mainis conflict-free. On that combined tree, the bump script (run beforemainmoved to 1.17.2) took 1.16.7 to 1.17.1 by replacing the checksum file, the new Go test passes, andactionlintandshellcheckpass. They can merge in any order. Merge #31461 after #31463, retargeted tomain.Opened by Superblink on behalf of @droopy4096.