Files
ECC/scripts
Nguyen Thanh Dat eaf23b3fdc fix(gateguard): look past sudo/doas/env when classifying destructive commands
Every token-based detector keys on tokens[0], so a privilege or environment
wrapper hid the command being run. Measured against main with a primed
session, through the published hook runner:

  rm -rf /important/data          DENY-destructive
  sudo rm -rf /important/data     allowed
  sudo -u root rm -rf /data       allowed
  doas rm -rf /data               allowed
  env rm -rf /data                allowed
  command rm -rf /data            allowed
  nohup rm -rf /data              allowed
  sudo git reset --hard           allowed
  sudo git clean -fd              allowed
  sudo git push --force           allowed
  sudo find . -exec rm {} ;       allowed
  echo a && sudo rm -rf /data     allowed

The gate therefore missed exactly the spellings it matters most for: the
privileged ones. `sudo truncate table t` was already denied, because the
SQL arm is a text match and never looked at a command word at all.

stripCommandWrappers() removes leading wrapper commands, their flags, and
VAR=value assignment prefixes before the detectors see the tokens. It is
applied in the per-segment loop (rm, git) and in isDestructiveFindExec.

Two bounds keep it from over-reaching:
  - flags are skipped only after a wrapper has been seen, so it can never
    walk into an unwrapped command's own arguments;
  - the flags that consume a following token (-u/-g/-p/...) are listed, so
    `sudo -u root rm` resolves to rm rather than to root.

Ordinary privileged work is unaffected — verified allowed: sudo apt-get
update, sudo systemctl restart nginx, sudo ls -la /etc, command ls, and
env NODE_ENV=production npm run build.

Tests: 13 fail on main, 162 pass with this change. The 5 must-allow cases
pass either way by design; they are the over-blocking guard.
2026-08-21 10:17:52 +07:00
..