Skip to content

fix: stop blocking harmless /dev/null redirections in CommandValidator - #19

Merged
chouzz merged 1 commit into
mainfrom
fix/redirect-dev-null-false-positive
Sep 14, 2026
Merged

chouzz merged 1 commit into
mainfrom
fix/redirect-dev-null-false-positive

Conversation

@chouzz

@chouzz chouzz commented Sep 14, 2026

Copy link
Copy Markdown
Owner

Problem

The CommandValidator rule (r'>\s*/dev/', 'Redirect to device file') rejects any command containing a > redirect to any /dev/... path, including the extremely common, harmless idiom:

some_cmd 2>/dev/null

The rule's intent is to block catastrophic overwrites of real storage devices (e.g. writing raw output to a disk), but its granularity is too coarse: it doesn't distinguish dangerous block devices from harmless character devices like /dev/null (a data sink by definition — writing to it can never be destructive).

Fix

Replace the blanket rule with a whitelist of real storage device prefixes, instead of a denylist-style (?!null) lookahead — this also auto-allows future harmless /dev/* character devices:

(r'>\s*/dev/(?:sd[a-z]|hd[a-z]|vd[a-z]|xvd[a-z]|nvme|mmcblk|loop|mapper|sr\d|fd\d)',
 'Overwrite block device'),

Covered prefixes: sd (SATA/SCSI), hd (IDE), vd (virtio), xvd (Xen), nvme (incl. nvme0n1), mmcblk (SD/eMMC), loop, mapper (LVM/dm-crypt), sr (CD/DVD), fd (floppy).

Still blocked:

  • echo x > /dev/sda
  • echo x >/dev/nvme0n1
  • echo x > /dev/mapper/vg-root

Now allowed:

  • echo hello 2>/dev/null
  • cmd > /dev/null 2>&1
  • cat /etc/hostname > /dev/full
  • echo x 1>/dev/stdout 2>/dev/stderr

Note (not changed in this PR)

The validator is still regex-over-string, not shell-syntax-aware, so any text content that happens to contain a redirect-to-block-device pattern inside a quoted string would still false-positive — a known structural limitation of this class of validator, out of scope here.

Tests

Added 7 test cases (3 blocked block-device overwrites + 4 allowed harmless redirections). Full suite: 12 passed.

The pattern '>/dev/' blocked any redirect to any /dev/ file, including
/dev/null, /dev/stdout, /dev/stderr and /dev/full — breaking the standard
'2>/dev/null' stderr-discard idiom.

Replace with a whitelist of real storage device prefixes (sd, hd, vd,
xvd, nvme, mmcblk, loop, mapper, sr, fd) so only overwrites of actual
block devices are rejected. Future harmless /dev/* character devices are
allowed by default.
@chouzz
chouzz merged commit 91373a7 into main Sep 14, 2026
2 checks passed
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.

1 participant