Skip to content

feat(fsroot): add CreateTemp and MkdirTemp to the Root interface - #413

Merged
David-Noble-at-work merged 1 commit into
developfrom
fsroot-temp-methods
Aug 14, 2026
Merged

David-Noble-at-work merged 1 commit into
developfrom
fsroot-temp-methods

Conversation

@David-Noble-at-work

Copy link
Copy Markdown
Contributor

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.Root has neither, and os.CreateTemp cannot be confined to a root. A shared per-session
scratch directory therefore had no way to give concurrent callers unique names — and gather
dispatches 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 own OpenFile
and Mkdir rather than the inner *os.Root. Confinement and the phase-2 Windows enforcement pass
apply 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 a
stage-then-rename is only atomic within the target's own directory — a tree can span mounts — and
returns the created Path so callers need not rebuild one from f.Name(). The root itself is
NewPath(".").

Names come from crypto/rand.Text: a predictable temporary name in a directory an attacker can
also write to invites a symlink race, and the strong source costs nothing here. Collisions are still
handled by O_EXCL with bounded retries, as os.CreateTemp does. A pattern containing a path
separator is refused with fs.ErrInvalid — the one way a name could act as a path.

Verification

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, not a suppression — and an importShadow
where the new path import 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

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 David-Noble-at-work self-assigned this Aug 14, 2026
@David-Noble-at-work
David-Noble-at-work merged commit fe8f2ea into develop Aug 14, 2026
7 of 8 checks passed
@David-Noble-at-work
David-Noble-at-work deleted the fsroot-temp-methods branch August 14, 2026 22:38
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]>
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