Skip to content

fix(SC2114): recognize non-canonical paths to system directories - #3614

Open
santhiprakash wants to merge 2 commits into
koalaman:masterfrom
santhiprakash:fix/3584-sc2114-canonicalize-path
Open

santhiprakash wants to merge 2 commits into
koalaman:masterfrom
santhiprakash:fix/3584-sc2114-canonicalize-path

Conversation

@santhiprakash

Copy link
Copy Markdown
Contributor

Fixes #3584

Problem

rm -rf /etc triggers SC2114 ("Warning: deletes a system directory"), but the same deletion spelled non-canonically does not:

rm -rf /./etc
rm -rf /etc/../etc

Both delete exactly /etc, yet ShellCheck reports nothing for them.

Triage / Root cause

checkCatastrophicRm compares the argument text — after fixPath collapses repeated / and * and strips a trailing / — against importantPaths. fixPath never resolves . or .. components, so non-canonical spellings never reach the comparison.

Fix

Extend fixPath to lexically resolve . and .. components before the importantPaths lookup:

  • . segments are dropped; .. pops the preceding segment.
  • Resolution is purely lexical — no symlink resolution, no filesystem access.
  • A .. never pops a leading .. of a relative path (rm -rf ../etc stays ../etc), matching normpath-style semantics.
  • Resolution stops at the first segment containing a glob metacharacter (*, ?, [): rm -rf /etc/*/../foo does not necessarily delete /foo (the glob may match non-directories), so it correctly stays silent.

Since fixPath also feeds the SC2115 path (the ${var:?} suggestion for paths that could expand catastrophically), the same canonicalization applies there: rm -rf "$x/../etc" now warns SC2115 because an empty x really does expand to /etc. Flagging explicitly since it changes SC2115 behavior too — it can only produce warnings where an empty expansion lexically resolves to a system directory.

Verification

  • New prop_checkCatastrophicRm14–23 fail on the pre-fix fixPath (checked via cabal repl: Rm14/15/17/18/20/23 = False before the change) and all pass with it; the four verifyNot props document the conservative limits above.

  • cabal test (ghc 9.6.7, aarch64, haskell:9.6 image): all tests pass.

  • e2e via cabal run shellcheck -- -s sh:

    rm -rf /etc              -> SC2114 (unchanged)
    rm -rf /./etc            -> SC2114 (new)
    rm -rf /etc/../etc       -> SC2114 (new)
    rm -rf /etc/*            -> SC2114 (unchanged)
    rm -rf /etc/..           -> SC2114 (resolves to /)
    rm -rf "$x/../etc"       -> SC2115 (new)
    rm -rf /usr/local/../etc -> silent (/usr/etc is not a system dir)
    rm -rf /etc/*/../foo     -> silent (glob blocks resolution)
    rm -rf ../etc            -> silent (relative path)
    rm -rf foo/../etc        -> silent (relative path)
    rm -rf "$x"              -> silent (empty path stays empty)
    

Notes / Risks

  • Pure false-negative fix for SC2114: warnings are added only when the lexical canonical form lands in importantPaths; no existing warning is removed or reworded (check ids and messages unchanged).
  • rm -rf /.. and rm -rf /x/../.. now warn — they resolve to /, which is in importantPaths.
  • /proc/self/../etc resolves to /proc/etc (no warning): correct under lexical-only rules — /proc/self is a symlink and we deliberately do not resolve symlinks.

Assistance

AI-assisted. AI tools helped draft this change and this description. The verification steps were actually run.

- Problem: `rm -rf /./etc` and `rm -rf /etc/../etc` delete a system
  directory but drew no SC2114, because fixPath compared the literal
  argument text against importantPaths.
- Fix: extend fixPath to lexically resolve "." and ".." components.
  Resolution stops at the first segment containing a glob character
  ("/etc/*/../x" is not necessarily "/x"), never pops a leading ".."
  of a relative path, and performs no symlink resolution. The same
  canonicalization applies to the potential paths checked for SC2115.
- Verification: new prop_checkCatastrophicRm14-23 fail on the pre-fix
  fixPath and pass after; `cabal test` passes all properties; e2e,
  `rm -rf /./etc` and `rm -rf /etc/../etc` now warn SC2114 while
  `rm -rf /usr/local/../etc`, `rm -rf ../etc` and `rm -rf /etc/*/../foo`
  stay silent.
@santhiprakash

Copy link
Copy Markdown
Contributor Author

Related: #3616 (open) also modifies checkCatastrophicRm — it removes the recursive-flag gate, while this PR only extends fixPath normalization. Semantically complementary (that PR explicitly leaves path normalization to this one). Textually both add properties after prop_checkCatastrophicRmC, so whichever lands second will need a trivial rebase — happy to rebase this one if it goes second.

Comment thread src/ShellCheck/Checks/Commands.hs Resolved
- Problem: reviewer e-kwsm reported rm -rf /*/../etc/ still not warned; the
  canonicalizer bailed at the first glob segment and kept the remainder
  verbatim (*../etc), which never matches a system directory although the
  shell resolves the path to /etc.
- Fix: let a .. pop the nearest previous segment, glob or concrete: /* expands
  to top-level entries /name, so /name/../etc is exactly /etc. Mid-path globs
  keep correct semantics (/etc/*/../etc resolves to /etc/etc, not warned).
- Verification: docker haskell:9.6 cabal test; new props
  checkCatastrophicRm24/25/26 (Rm24 Falsified pre-fix), full test suite green
  post-fix (1 of 1 test suites passed).

This branch has not been deployed

No deployments
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.

SC2114 should canonicalize path

2 participants