refactor(utils): simplify EnsureDestinationRoot and consolidate securefile copy
Follow-up to !3650 (merged) — addresses @hacks4oats's post-merge review comments.
Summary
- Simplifies
utils.EnsureDestinationRoot: drops the intermediateos.OpenRoot(".")+MkdirAlldance and itsensureDirectoryExistshelper. The relative-path escape check is now an explicitfilepath.IsLocalguard, andos.MkdirAllcreates the destination directory directly. - Removes the local
ensureDestinationRoot/ensureDirectoryExistscopies ininternal/commands/securefile/download/download.go; both call sites now use the sharedutils.EnsureDestinationRoot(matching the packages/download command). - Migrates the still-useful "absolute path whose parent is a file" test case into
internal/utils/root_test.goand drops the obsolete tests that referenced the deleted helpers.
Note on scope
The original suggestion was to drop os.OpenRoot entirely and let callers use os.Create(path). I kept the returned *os.Root because it's what gives the download commands their write-step symlink protection (server-supplied filenames are joined through root.Create / root.Rename). The anti-traversal guarantee for relative paths is now enforced up-front via filepath.IsLocal instead of via wrapping in os.OpenRoot("."), which lets the helper collapse to a single MkdirAll + OpenRoot.
Happy to trim further if you'd rather see the write path move to plain os.Create too — that's a slightly larger change in securefile/download and packages/download.
Test plan
-
go test ./internal/utils/... ./internal/commands/securefile/download/... ./internal/commands/packages/download/... -
make lint -
make build
cc @hacks4oats