Repository navigation
feat(fsroot): add CreateTemp and MkdirTemp to the Root interface - #413
Merged
Merged
Conversation
Phase 2b, first half of PR 2b.2 of the native-Windows-permissions plan (#405). Split out from the accessor change so a sealed-interface addition is reviewed on its own rather than inside an 82-site diff. The design is unchanged from the ruling: both methods land, with the accessors following. os.Root has neither method, and os.CreateTemp cannot be confined to a root, so a shared per-session scratch directory had no way to hand concurrent callers unique names -- and gather dispatches bodies concurrently by design. Dir.Create does not solve it; two concurrent spools would pick the same name. Both delegate to createTempIn/mkdirTempIn, which create through the root's OWN OpenFile and Mkdir rather than the inner os.Root. Confinement and the phase-2 Windows enforcement pass therefore apply for free: a temp file is 0600 and a temp dir 0700, both private, so both receive a protected DACL on Windows without the caller asking for one. CreateTemp takes a directory argument rather than always creating at the root, because a stage-then-rename is only atomic within the target's own directory -- a tree can span mounts. It returns the created Path beside the file so callers do not rebuild one from f.Name(). The root itself is NewPath("."). Names come from crypto/rand.Text rather than a pseudo-random generator: a predictable temporary name in a directory an attacker can also write to invites a symlink race, and the strong source costs nothing at this frequency. Collisions are still handled by O_EXCL with bounded retries, exactly as os.CreateTemp does. A pattern containing a path separator is refused with fs.ErrInvalid -- the one way a name could otherwise act as a path and escape the directory argument. Tests: unique names honoring the pattern and staying under the given directory, across both writable implementations; the private modes asserted on Unix, where mode bits are the subject; separator patterns refused; a scratch root's temp files dying with its tree; and both methods added to the read-only refusal set. Verified: make test zero failures; make lint-all zero issues on linux, darwin, and windows. lint-all also caught a weak-RNG finding (fixed by crypto/rand rather than a suppression) and an importShadow where the new path import collided with a local variable in an existing fixture helper, renamed to absolute -- which it always was. Assisted-by: Claude Signed-off-by: David Noble <[email protected]>
David-Noble-at-work
added a commit
that referenced
this pull request
Aug 14, 2026
Phase 2b, PR 2b.2b of the native-Windows-permissions plan (#405), completing 2b.2 after #413 put CreateTemp/MkdirTemp on the interface. RuntimeEnvironment.Root is no longer a field anyone can set or read directly: the session owns filesystem access and hands it out. HasRoot() answers from the spec's anchor rather than from whether a handle has been minted, so its answer cannot change mid-session. Root() panics when HasRoot() is false -- a rootless session is legal and advertised (cmd/lore builds one in production), so the panic reports a caller that never asked rather than an impossible state. Returning a bare nil was rejected during design: fsroot.Root is an interface, so nil does not avoid the panic, it relocates it into fsroot with a stack that never names the mistake. Modeled on reflect.Value.IsValid, whose doc states the rule once and whose getters panic. Scratch() returns a per-session private directory INSIDE the OS temp directory -- never a root over the temp directory itself, which is shared with every process, cannot be mode-controlled, and could not be removed on Close. It is created 0700, so phase 2 gives it a protected DACL on Windows, which matters because scratch holds the most sensitive transient data in the system. There is no HasScratch: nothing about scratch is configurable, so the predicate could only ever return true. Both anchors are PREFLIGHTED at construction and released again -- probeRootAnchor opens and closes the root, probeScratchAnchor creates and removes a temporary directory. That is what licenses both accessors to assert: validation has already passed, so a later failure is an invariant violation, matching the repository's checksum trust boundary. Allocation stays lazy behind a sync.Once each, because gather dispatches bodies concurrently and two goroutines can reach an accessor at once. A planning-only session holds no handle and closes nothing. 105 sites across 25 files moved, and the seven that nil-checked the field became HasRoot() guards. The 82-site estimate counted production only and was accurate there; test files added the rest. The mechanical rewrite over-matched once and the compiler caught it: triad_test.go has a local triadEnv fixture with its own Root field, so env.Root there is not the session. Twenty rewrites reverted. It failed loudly only because a field is not callable -- an interface with a Root() method would have compiled and silently changed meaning, which is the risk phase 6's rename runs over a much larger surface. One existing test specified the old contract (Root() != nil on a rootless session) and now specifies the new one, with recover() proving the panic. A new test pins scratch: private tree inside os.TempDir, same instance on every call, 0700, removed on Close. Verified: make test zero failures; make lint-all zero issues on linux, darwin, and windows. Assisted-by: Claude Signed-off-by: David Noble <[email protected]> Co-authored-by: David Noble <[email protected]>
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
Phase 2b, the first half of PR 2b.2 of
windows-native-permissions.md(#405), split out so a sealed-interface
addition is reviewed on its own rather than inside the accessor change's 82-site diff. The design
is exactly as ruled; only the delivery splits.
Why these methods exist
os.Roothas neither, andos.CreateTempcannot be confined to a root. A shared per-sessionscratch directory therefore had no way to give concurrent callers unique names — and
gatherdispatches bodies concurrently by design, so two spools would pick the same name and silently
corrupt each other.
How they are built
Both delegate to
createTempIn/mkdirTempIn, which create through the root's ownOpenFileand
Mkdirrather than the inner*os.Root. Confinement and the phase-2 Windows enforcement passapply for free: 0600 files and 0700 directories are private, so each gets a protected DACL
on Windows without the caller asking.
CreateTemp(dir Path, pattern string) (*os.File, Path, error)takes a directory because astage-then-rename is only atomic within the target's own directory — a tree can span mounts — and
returns the created
Pathso callers need not rebuild one fromf.Name(). The root itself isNewPath(".").Names come from
crypto/rand.Text: a predictable temporary name in a directory an attacker canalso write to invites a symlink race, and the strong source costs nothing here. Collisions are still
handled by
O_EXCLwith bounded retries, asos.CreateTempdoes. A pattern containing a pathseparator is refused with
fs.ErrInvalid— the one way a name could act as a path.Verification
make testzero failures;make lint-allzero issues on linux, darwin, and windows.lint-allalso caught a weak-RNG finding — fixed by
crypto/rand, not a suppression — and animportShadowwhere the new
pathimport collided with a local variable in an existing fixture helper.test (windows-latest)is expected to hold at its known 28 failures.Assisted-by: Claude