refactor(hm-util): Replace bare (file_mode: u32, dir_mode: u32) pair with distinct mode newtypes to prevent swapped-argument bugs#122
Merged
markovejnovic merged 2 commits intoJun 10, 2026
Conversation
…with distinct mode newtypes to prevent swapped-argument bugs
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.
The smell
write_atomic_restricted(path, contents, file_mode: u32, dir_mode: u32)(and itsblockingwrapper) took two adjacent, same-typedu32octal mode parameters. Call sites passed them positionally as bare literals —creds.rspasses0o600, 0o700,lib.rspasses0o644, 0o700. Nothing prevented transposing the two arguments. Swapping them would silently set a secrets file to0o700(or the parent dir to0o644) while still compiling and type-checking — a security-relevant invariant (secrets must be0o600in a0o700dir) caught today only by a unit test.The change
Introduced two distinct one-field newtypes in
crates/hm-util/src/os/fs.rs:The signature is now
write_atomic_restricted(path, contents, file: FileMode, dir: DirMode)for both the async function and its blocking wrapper. Callers writeFileMode(0o600), DirMode(0o700), so the file/dir slots can no longer be transposed — an incorrect ordering is now a compile error and the intent is documented at every call site.Behavior-preserving: the wrappers just unwrap
.0into the existingu32-based internals (create_dir_with_mode,write_file_with_mode). The two real call sites inhm-config(creds.rs,lib.rs) and the in-module tests were updated to match.Pattern applied
This mirrors the "Distinct id newtypes per domain (no shared usize)" pattern — two incompatible wrappers over the same underlying integer, so the type system rejects mixing them. The reference is the same doctrine used elsewhere for domain id newtypes; here it guards a security-sensitive mode invariant rather than entity ids.
CI note
Only
cargo check -p hm-util(and, since the call sites changed,cargo check -p hm-config) was run locally — both pass. Full CI runs on this PR. Opened as a draft for review.