Repository navigation
fix(cookbooks): keep downloads inside destDir through an os.Root - #113
Merged
Merged
Conversation
DownloadFiles checked manifest paths lexically, so a path could not climb out of destDir, but the filesystem it then wrote into was trusted: a symlink already under destDir that pointed elsewhere carried the write with it. With recipes -> /elsewhere, recipes/default.rb landed in /elsewhere. A file that was itself a symlink out of destDir was read through for the up-to-date check and, when its target had the right checksum, left in place. Writes now go through os.OpenRoot(destDir): the checksum read, MkdirAll, temp file and rename are all held under destDir. A symlink pointing out fails the download (a directory) or is replaced by the downloaded file (a file); one that stays inside destDir is followed as before. Manifest paths are checked with filepath.IsLocal up front, so a bad manifest still fails before anything is fetched. The temp file's mode is set on the open file rather than by name. Signed-off-by: Tim Smith <tsmith84@proton.me>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Cookbooks.DownloadFiles(andDownload) checked each manifest path lexically, so../xwas refused. But it then trusted the filesystem it wrote into. A symlink already underdestDirthat pointed elsewhere carried the write with it: withrecipes -> /elsewhere, the manifest's cleanrecipes/default.rblanded in/elsewhere/default.rb. A file that was itself a symlink out ofdestDirwas read through for the "already up to date" check; when its target happened to match the checksum, the link was left in place instead of a real file.A manifest cannot create symlinks, so this needs a pre-existing link in the destination (a shared or reused checkout, say). It's defense in depth rather than a remote exploit.
os.Root(Go 1.24, withMkdirAll/Renamesince 1.25) is the standard-library tool for exactly this.Change
os.OpenRoot(destDir): the checksum read,MkdirAll, temp file and final rename are all held underdestDirby the OS, not by string checks.destDirfails the download, and nothing is written outside.destDiris followed as before.filepath.IsLocal(equivalent to the oldfilepath.Relcheck, and it also refuses Windows reserved names). A bad manifest still fails before anything is fetched.File.Chmodon the open handle.Root.Chmodby name is documented as racy on Unix.destDiris created up front if missing (it was created implicitly before), and a destination that can't be created or opened fails before any fetch.DownloadFilesdoc comment states the new guarantee. No signature change.Test plan
TestDownload_RefusesSymlinkedDirectoryOutOfDest(the file was written outside),TestDownload_ReplacesSymlinkedFileOutOfDest(an outside link was kept as a cached copy)TestDownload_FollowsSymlinkWithinDest,TestDownload_DestinationUnusable(destination is a file; destination can't be opened)go test ./... -race -count=2,cd integration && go test ./...(HTTP and TLS cookbook round-trips),golangci-lint run ./...in both modules