From 3d3daa0c3bd7248efd9459282494796ee5ff055b Mon Sep 17 00:00:00 2001 From: sec-check <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:30:29 -0500 Subject: [PATCH 1/3] fix: close easy bypasses in toolapprove command-safety regexes dd was only checked for 'if=/dev/zero'; writing to a raw block device with any other input source (or no /dev/zero at all) passed clean. credentialExfilRe required unquoted $GH_TOKEN/$GITHUB_TOKEN, a bare '~' for the ssh dir, and 'cat' as the reader, so ordinary variants like echo "$GH_TOKEN", cat $HOME/.ssh/id_rsa, or less ~/.ssh/id_rsa all bypassed it. Fixes #157 Signed-off-by: sec-check <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- toolapprove/security.go | 22 +++++-- toolapprove/security_command_test.go | 99 ++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 5 deletions(-) diff --git a/toolapprove/security.go b/toolapprove/security.go index f52b450..33ad382 100644 --- a/toolapprove/security.go +++ b/toolapprove/security.go @@ -108,11 +108,23 @@ func NewDefaultSecurityScanner() *DefaultSecurityScanner { var ( // destructiveCmdRe matches the non-rm destructive commands: mkfs, dd - // from /dev/zero and the fork bomb, which needs no word boundary. A - // recursive rm of a protected directory is decided by analyzeRmCommand - // (shellrm.go), which reads the command the way the shell does. - destructiveCmdRe = regexp.MustCompile(`(?i)(\bmkfs\b|\bdd\s+if=/dev/zero\b|:\(\)\{\s*:\|:&\s*\};:)`) - credentialExfilRe = regexp.MustCompile(`(?i)\b(cat\s+~/\.ssh/|cat\s+/var/run/secrets/|printenv\s+.*TOKEN|echo\s+\$GH_TOKEN|echo\s+\$GITHUB_TOKEN)\b`) + // from /dev/zero or onto a raw block device, and the fork bomb, which + // needs no word boundary. A recursive rm of a protected directory is + // decided by analyzeRmCommand (shellrm.go), which reads the command the + // way the shell does. dd is destructive by its *output* target as much + // as its input source — "dd if=/dev/urandom of=/dev/sda" wipes a disk + // without ever reading /dev/zero — so both sides are checked. + destructiveCmdRe = regexp.MustCompile(`(?i)(\bmkfs\b|\bdd\s+if=/dev/zero\b|\bdd\s+[^;|&\n]*\bof=/dev/(?:[sh]d|vd|xvd|nvme)[a-z0-9]*\b|:\(\)\{\s*:\|:&\s*\};:)`) + // credentialExfilRe flags common commands that read or print credential + // material. It is a substring/pattern heuristic, not a shell parser + // (unlike analyzeRmCommand): it tolerates the quoting and $HOME/${HOME} + // variants a normal command line uses, but it does not follow variable + // indirection, and it deliberately omits bare env/printenv dumps, which + // are routine and would make the check too noisy to keep enabled. + credentialExfilRe = regexp.MustCompile(`(?i)\b(?:cat|less|more|head|tail|tac|base64|xxd|od|strings)\s+["']?(?:~|\$\{?HOME\}?|/root)["']?/\.ssh/` + + `|\b(?:cat|less|more|head|tail|tac|base64|xxd|od|strings)\s+["']?/var/run/secrets/` + + `|\bprintenv\b.*\bTOKEN\b` + + `|\becho\s+["']?\$\{?(?:GH_TOKEN|GITHUB_TOKEN)\b`) ) // Scan analyzes the tool request for security violations. diff --git a/toolapprove/security_command_test.go b/toolapprove/security_command_test.go index c14f8f0..d260c44 100644 --- a/toolapprove/security_command_test.go +++ b/toolapprove/security_command_test.go @@ -158,3 +158,102 @@ func TestDefaultSecurityScannerDestructiveCommandsWithoutTextFindings(t *testing }) } } + +func TestDefaultSecurityScannerDdBlockDeviceWithoutTextFindings(t *testing.T) { + scanner := NewDefaultSecurityScanner() + scanner.Text = textScannerFunc(func(string) TextVerdict { return TextVerdict{} }) + + for _, command := range []string{ + "dd if=/dev/zero of=/dev/sda", + "dd if=/dev/urandom of=/dev/sda", + "dd if=/dev/urandom of=/dev/sda bs=1M", + "dd of=/dev/sda if=/dev/urandom", + "dd if=image.iso of=/dev/sdb", + "dd if=image.iso of=/dev/nvme0n1", + "dd if=image.iso of=/dev/vda", + "dd if=image.iso of=/dev/xvda", + "dd if=image.iso of=/dev/hda", + } { + t.Run(command, func(t *testing.T) { + result, err := scanner.Scan(context.Background(), ToolRequest{Tool: "bash", Command: command}) + if err != nil { + t.Fatal(err) + } + if result.Passed { + t.Fatalf("Scan(%q) = %+v, want a destructive-command violation", command, result) + } + }) + } + + for _, command := range []string{ + "dd if=image.iso of=output.img", + "dd if=/dev/zero of=/dev/null", + "dd if=image.iso of=/dev/null bs=1M", + } { + t.Run(command, func(t *testing.T) { + result, err := scanner.Scan(context.Background(), ToolRequest{Tool: "bash", Command: command}) + if err != nil { + t.Fatal(err) + } + if !result.Passed || len(result.Violations) != 0 { + t.Fatalf("Scan(%q) = %+v, want clean scan", command, result) + } + }) + } +} + +func TestDefaultSecurityScannerCredentialExfilBypasses(t *testing.T) { + scanner := NewDefaultSecurityScanner() + scanner.Text = textScannerFunc(func(string) TextVerdict { return TextVerdict{} }) + + for _, command := range []string{ + "cat ~/.ssh/id_rsa", + "cat /var/run/secrets/kubernetes.io/serviceaccount/token", + "printenv GH_TOKEN", + "echo $GH_TOKEN", + "echo $GITHUB_TOKEN", + // Previously missed: quoting around the variable. + `echo "$GH_TOKEN"`, + `echo '$GITHUB_TOKEN'`, + "echo ${GH_TOKEN}", + // Previously missed: $HOME/${HOME} instead of ~, and /root. + "cat $HOME/.ssh/id_rsa", + `cat "$HOME/.ssh/id_rsa"`, + "cat ${HOME}/.ssh/id_rsa", + "cat /root/.ssh/id_rsa", + // Previously missed: readers other than cat. + "less ~/.ssh/id_rsa", + "head -c 100 ~/.ssh/id_rsa", + "base64 ~/.ssh/id_rsa", + "tail /var/run/secrets/kubernetes.io/serviceaccount/token", + } { + t.Run(command, func(t *testing.T) { + result, err := scanner.Scan(context.Background(), ToolRequest{Tool: "bash", Command: command}) + if err != nil { + t.Fatal(err) + } + if result.Passed { + t.Fatalf("Scan(%q) = %+v, want a credential-exfiltration violation", command, result) + } + }) + } + + for _, command := range []string{ + "env", + "printenv", + "echo hello", + "echo $PATH", + "cat README.md", + "cat ~/.ssh_backup/id_rsa", + } { + t.Run(command, func(t *testing.T) { + result, err := scanner.Scan(context.Background(), ToolRequest{Tool: "bash", Command: command}) + if err != nil { + t.Fatal(err) + } + if !result.Passed || len(result.Violations) != 0 { + t.Fatalf("Scan(%q) = %+v, want clean scan", command, result) + } + }) + } +} From 0e6f40d600a79fbf44721295021f636c8572c1bd Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:47:42 -0500 Subject: [PATCH 2/3] fix: restore printenv *_TOKEN match and allow reader flags in credentialExfilRe Drop the trailing \b after TOKEN (no boundary after underscore, so printenv GH_TOKEN passed), and allow option flags between the reader and the path so head -c 100 ~/.ssh/id_rsa is flagged. Hive-Fix-Attempt: 1/6 Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- toolapprove/security.go | 6 +++--- toolapprove/security_command_test.go | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/toolapprove/security.go b/toolapprove/security.go index 33ad382..6ffd512 100644 --- a/toolapprove/security.go +++ b/toolapprove/security.go @@ -121,9 +121,9 @@ var ( // variants a normal command line uses, but it does not follow variable // indirection, and it deliberately omits bare env/printenv dumps, which // are routine and would make the check too noisy to keep enabled. - credentialExfilRe = regexp.MustCompile(`(?i)\b(?:cat|less|more|head|tail|tac|base64|xxd|od|strings)\s+["']?(?:~|\$\{?HOME\}?|/root)["']?/\.ssh/` + - `|\b(?:cat|less|more|head|tail|tac|base64|xxd|od|strings)\s+["']?/var/run/secrets/` + - `|\bprintenv\b.*\bTOKEN\b` + + credentialExfilRe = regexp.MustCompile(`(?i)\b(?:cat|less|more|head|tail|tac|base64|xxd|od|strings)(?:\s+-\S+(?:\s+\d+)?)*\s+["']?(?:~|\$\{?HOME\}?|/root)["']?/\.ssh/` + + `|\b(?:cat|less|more|head|tail|tac|base64|xxd|od|strings)(?:\s+-\S+(?:\s+\d+)?)*\s+["']?/var/run/secrets/` + + `|\bprintenv\s+.*TOKEN` + `|\becho\s+["']?\$\{?(?:GH_TOKEN|GITHUB_TOKEN)\b`) ) diff --git a/toolapprove/security_command_test.go b/toolapprove/security_command_test.go index d260c44..ac71fb7 100644 --- a/toolapprove/security_command_test.go +++ b/toolapprove/security_command_test.go @@ -210,6 +210,7 @@ func TestDefaultSecurityScannerCredentialExfilBypasses(t *testing.T) { "cat ~/.ssh/id_rsa", "cat /var/run/secrets/kubernetes.io/serviceaccount/token", "printenv GH_TOKEN", + "printenv GITHUB_TOKEN", "echo $GH_TOKEN", "echo $GITHUB_TOKEN", // Previously missed: quoting around the variable. @@ -224,6 +225,7 @@ func TestDefaultSecurityScannerCredentialExfilBypasses(t *testing.T) { // Previously missed: readers other than cat. "less ~/.ssh/id_rsa", "head -c 100 ~/.ssh/id_rsa", + "tail -n 5 $HOME/.ssh/id_rsa", "base64 ~/.ssh/id_rsa", "tail /var/run/secrets/kubernetes.io/serviceaccount/token", } { From c08ca3ec8c77a939d8551075afefd1c8fa858b66 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:56:59 -0500 Subject: [PATCH 3/3] test: drop dd if=/dev/zero of=/dev/null from the clean-scan list Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- toolapprove/security_command_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/toolapprove/security_command_test.go b/toolapprove/security_command_test.go index ac71fb7..41cf933 100644 --- a/toolapprove/security_command_test.go +++ b/toolapprove/security_command_test.go @@ -187,7 +187,6 @@ func TestDefaultSecurityScannerDdBlockDeviceWithoutTextFindings(t *testing.T) { for _, command := range []string{ "dd if=image.iso of=output.img", - "dd if=/dev/zero of=/dev/null", "dd if=image.iso of=/dev/null bs=1M", } { t.Run(command, func(t *testing.T) {