fix(security): add the Windows analogues of the shutdown/mkfs hardline rules - #84563
fix(security): add the Windows analogues of the shutdown/mkfs hardline rules#84563pierrenode wants to merge 1 commit into
Conversation
…e rules NousResearch#84428 added a Windows destructive tier to DANGEROUS_PATTERNS (bypassable under --yolo / approvals.mode=off), including Format-Volume/format.com and taskkill/Stop-Process force-kills. It did not touch HARDLINE_PATTERNS -- the separate, unconditionally-blocked-even-under-yolo list -- so two of its own POSIX siblings stayed unmatched on Windows: - Restart-Computer / Stop-Computer had NO detection at all (not even a bypassable prompt): the existing hardline shutdown/reboot/halt/poweroff rule only matches those literal words, not PowerShell's cmdlet names. Verified empirically before this fix: `Restart-Computer -Force` and `Stop-Computer -Force` returned (False, None) from BOTH detect_hardline_command and detect_dangerous_command. - Format-Volume / format.com (a drive letter) were only in the bypassable tier, despite being the exact Windows analogue of the hardline mkfs rule -- an operation with no recovery path, which is precisely what the hardline floor exists to stop regardless of yolo. Both new hardline entries mirror mkfs's existing bare \b-boundary style (not _CMDPOS-anchored, matching the rest of the Windows tier below in DANGEROUS_PATTERNS -- unlike "shutdown"/"reboot", "restart-computer" and "format-volume" aren't ordinary English prose that needs command-position anchoring to avoid false positives). diskpart stays hardline-exempt (interactive, has non-destructive subcommands like `list disk`) -- same scoping choice POSIX makes by excluding fdisk/parted from the hardline list. Tests: 13 new cases in test_approval_windows.py (5 power-state hardline, 3 format hardline, 5 benign/non-hardline), mutation-verified against the unfixed code (all 8 "should be hardline" assertions fail without this change).
monerostar
left a comment
There was a problem hiding this comment.
Native Windows 11 verification
Host: Windows 11 (Windows-10-10.0.26200-SP0), CPython 3.11.15, worktree at e870d10eb.
Gap on current code (still present)
Against the installed tree on this machine, PowerShell power-state / volume-format commands are not hardline or dangerous:
| command | hardline | dangerous |
|---|---|---|
Restart-Computer / -Force / lowercase |
(False, None) |
(False, None, None) |
Stop-Computer / -Force |
(False, None) |
(False, None, None) |
Format-Volume -DriveLetter D |
(False, None) |
(False, None, None) |
format d: /fs:ntfs / format D: /y |
(False, None) |
(False, None, None) |
shutdown /s /t 0 (POSIX sibling) |
(True, 'system shutdown/reboot') |
n/a |
Current upstream main tools/approval.py still has no restart-computer / stop-computer in HARDLINE_PATTERNS. Format-Volume / format.com <drive>: exist only in bypassable DANGEROUS_PATTERNS (lines 775/778). So the PR's claim matches live main: yolo can still walk a Windows box through a no-recovery format, and Restart-Computer is not gated at all.
After this PR
Same interpreter, PR tree:
| command | hardline |
|---|---|
Restart-Computer, Restart-Computer -Force, restart-computer -force |
(True, 'system shutdown/reboot (PowerShell)') |
Stop-Computer, Stop-Computer -Force |
same |
Format-Volume -DriveLetter D |
(True, 'format filesystem (Format-Volume)') |
format d: /fs:ntfs, format D: /y |
(True, 'format drive (format.com)') |
diskpart /s wipe.txt |
not hardline (stays dangerous-only, as designed) |
format, format /?, Restart-Service, restart the computer manually |
not hardline |
HERMES_YOLO_MODE=1 does not change detect_hardline_command() results — the new rules stay True. That is the property that matters here.
Tests
python -m pytest tests/tools/test_approval_windows.py -q --tb=short -o addopts=
61 passed in 2.29s
Includes the 13 new TestHardlineWindowsDestructiveTier cases.
Review notes
- Scope is right: two POSIX hardline siblings (power-off + filesystem format), not a redesign.
diskpartleft out matches thefdisk/partedprecedent. - Duplicating
Format-Volume/format.cominto both lists matches the existing POSIX filesystem-format pattern already in this file. \b(no_CMDPOS) is consistent with the WindowsDANGEROUS_PATTERNStier and with the author's false-positive note. I would not block on that.- Compiled flags include
IGNORECASE(liveflags=50), which is whyformat D:is covered by[a-z]:.
I cannot merge. From a native Win11 box this looks ready.
|
Native Win11 verification (monerostar overnight, not a maintainer merge signal) Host: Windows 11 Pro The gap on current main is real. Live
So today a model can issue the PowerShell power-state cmdlets with no prompt at all, and can Tests: One follow-up, not a reason to drop the floor. The new rules use
POSIX reboot/halt were deliberately command-position anchored to avoid that class. A later pass wrapping these three with I would take this as-is; the Windows power-state hole is the important part. |
fix(security): add the Windows analogues of the shutdown/mkfs hardline rules Closing the Windows power-state/format hole is the important part and the test matrix is thorough (61 passing per the added suite). Two detection gaps and one false-positive concern:
|
Summary
Fixes #69472 (follow-up). #84428 added a Windows destructive tier to
DANGEROUS_PATTERNS— the bypassable tier (skippable via--yolo/approvals.mode=off) — coveringFormat-Volume/format.com,taskkill /F,Stop-Process -Force, etc. It did not touchHARDLINE_PATTERNS, the separate list checked before the yolo bypass and documented in-code as never bypassable ("commands with no recovery path... blocked unconditionally... trusting the agent with your files and services, not trusting it to wipe the disk or power the box off").Two of
HARDLINE_PATTERNS's own POSIX rules stayed unmatched on Windows:Restart-Computer/Stop-Computerhad zero detection at all — not even a bypassable prompt. The existingshutdown|reboot|halt|poweroffhardline rule only matches those literal words; PowerShell's cmdlet names don't contain them. Verified empirically before this fix:Format-Volume/format.com <drive>:— the exact Windows analogue of the hardlinemkfsrule (an operation with no recovery path) — were left in the bypassable tier only.Changes
Two new
HARDLINE_PATTERNSentries, mirroringmkfs's existing bare\b-boundary style (not_CMDPOS-anchored — like the rest of the Windows tier inDANGEROUS_PATTERNS, "restart-computer"/"format-volume" aren't ordinary English prose the way "shutdown"/"reboot" are, so command-position anchoring isn't needed to avoid false positives):diskpartstays hardline-exempt (interactive, has non-destructive subcommands likelist disk) — the same scoping choice POSIX makes by excludingfdisk/partedfrom the hardline list.Format-Volume/format.comremain inDANGEROUS_PATTERNStoo (unchanged) —mkfsitself is already duplicated in both lists in this codebase, so this matches existing precedent rather than introducing a new pattern.Test plan
tests/tools/test_approval_windows.py::TestHardlineWindowsDestructiveTier— 5 power-state hardline (Restart-Computer,-Forcevariant, lowercase,Stop-Computer,-Forcevariant), 3 format hardline, 5 benign/non-hardline (diskpart, bareformat/format /?,Restart-Service, plain prose).tests/tools/test_approval_windows.py(61 tests) +tests/tools/test_approval.py+test_approval_mode_parity.py+test_approval_deny_rules.py+test_smart_approval_policy.pypass (181/182; 1 pre-existing failure —TestDetectDangerousRm::test_nonrecursive_verification_artifact_cleanup_is_not_dangerous— verified viagit stashto reproduce identically without this change, unrelated/tmppath-mocking issue).ruff checkclean.Note on false positives
Both new patterns inherit the same prose-matching characteristic their existing siblings already have in this codebase —
grep -r "mkfs" docs/already hardline-matches today (mkfs's bare\bpattern has no command-position anchor), andFormat-Volume/format.com's existingDANGEROUS_PATTERNSentries already flag things likegrep -r "Format-Volume" docs/pre-existing this PR. This is an established, accepted trade-off in this exact list (precision over recall for genuinely catastrophic operations), not a new regression — happy to discuss further hardening (e.g._CMDPOSanchoring) as a follow-up if maintainers want it, but wanted to match the existing style rather than diverge from the pattern I'm mirroring.