Block tilde expansion to prevent user info leak#2
Merged
AlexandreYang merged 2 commits intomainfrom Mar 8, 2026
Merged
Conversation
Co-authored-by: AlexandreYang <49917914+AlexandreYang@users.noreply.github.com>
AlexandreYang
commented
Mar 8, 2026
4 tasks
This was referenced Mar 10, 2026
Merged
4 tasks
This was referenced Mar 12, 2026
Closed
Merged
Merged
Merged
2 tasks
julesmcrt
added a commit
that referenced
this pull request
Apr 30, 2026
Two Codex round-6 items, both real: 1. P1 — saturating block multiply. unix.Statfs_t.Blocks * bsize could wrap a uint64 if a buggy/malicious FUSE filesystem reports counts above MaxUint64/bsize, producing tiny/zero Total/Used/Free for that mount and corrupting the --total accumulation. Added a mulSat(a, b) helper to diskstats_unix.go that clamps to MaxUint64 on overflow. Linux and Darwin backends now use mulSat for Total, Free, and Used. Locked in by TestMulSat covering boundary, just-over-boundary, and the realistic FUSE-rogue (maxU * 4096) case. 2. P2 — last-flag-wins for -h / -H. The previous code unconditionally preferred -h, so `df -hH` (intended SI override) and shell aliases that append -H to a default got the wrong size column. pflag.Visit walks set flags in lexicographical order, not argv order, so it cannot be used to honor input order. Refactored to a custom unitFlag (a pflag.Value) where -h and -H share a single *unitMode target and each Set call overwrites it — the LAST one wins by construction. registerUnitFlag wraps fs.VarPF + NoOptDefVal="true" so the flags accept no argument, matching pflag's bool convention. Confirmed locally: `df -hH` prints SI (995G), `df -Hh` prints IEC (927G). New TestUnitFlag_LastFlagWins covers 10 argv interleavings including combined short flags (-hH, -Hh). Side effect of #2: dropped the unused `human`/`si` *bool fields from the flags struct and the resolveUnitMode helper; mode is now read directly from a *unitMode field. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
The upstream
mvdan.cc/sh/v3expand package callsos/user.Lookup()when it encounters~usernametilde expansion (e.g.,echo ~root). This leaks host system user home directories, bypassing the shell's sandbox. Since tilde is parsed as a plain*syntax.Lit(no dedicated AST node), the existing validator had no way to catch it.Changes
interp/validate.go: Added*syntax.Wordcheck invalidateNodethat rejects any unquoted word starting with~at the firstLitpart. Heredoc bodies are excluded via ahdocWordstracking set (heredocs don't undergo tilde expansion).SHELL_FEATURES.md: Updated tilde expansion entry to include~userform and note that it is rejected at validation (exit code 2).tilde_not_expanded.yamlandtilde_path_not_expanded.yamlnow expect exit code 2 (validation rejection) instead of runtime passthrough.tilde_username_blocked.yaml— verifies~rootis rejected (the critical security case)tilde_in_heredoc_allowed.yaml— verifies~in heredoc bodies still workstilde_in_variable_assignment_blocked.yaml— verifiesDIR=~/pathis rejectedtilde_quoted_allowed.yaml— verifies"~root"and'~/path'are allowed (no expansion in quotes)tilde_mid_word_allowed.yaml— verifiesfoo~baris allowed (not a tilde prefix)Testing
go build ./...— compiles cleanlygo test ./...— all tests pass (interp, cmd/rshell, scenario tests)go vet ./interp/...— no issuesgofmt— formattedPR by Bits
View session in Datadog
Comment @DataDog to request changes