-
Notifications
You must be signed in to change notification settings - Fork 0
Implement /dev/null redirection and fd duplication #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlexandreYang
wants to merge
9
commits into
main
Choose a base branch
from
alex/feature_redir_dev_null
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fb6b066
Implement /dev/null redirection and fd duplication support
AlexandreYang 42e22d0
Address PR review comments: defense-in-depth, dead code, bash compat
AlexandreYang e67935f
Fix fd 0 input redirection and remove unnecessary skip_assert_against…
AlexandreYang b5b74e9
Add GitHub PR comment posting for review-fix-loop final summary
AlexandreYang 919a643
Post self-review result as PR comment in review-fix-loop skill
AlexandreYang 35da8df
Address PR review comments: document isDevNull Windows behavior, add …
AlexandreYang c72d4d8
Merge branch 'main' into alex/feature_redir_dev_null
AlexandreYang 7f4643c
Document why orig is unused for RdrAll/AppAll redirect ops
AlexandreYang 75054e7
Reject unsupported input fds at validation time, not just runtime
AlexandreYang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed | ||
| // under the Apache License Version 2.0. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2026-present Datadog, Inc. | ||
|
|
||
| package interp_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "mvdan.cc/sh/v3/syntax" | ||
|
|
||
| "github.com/DataDog/rshell/interp" | ||
| ) | ||
|
|
||
| func pentestRedirRun(t *testing.T, script, dir string) (string, string, int) { | ||
| t.Helper() | ||
| ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
| defer cancel() | ||
| return pentestRedirRunCtx(ctx, t, script, dir) | ||
| } | ||
|
|
||
| func pentestRedirRunCtx(ctx context.Context, t *testing.T, script, dir string) (string, string, int) { | ||
| t.Helper() | ||
| parser := syntax.NewParser() | ||
| prog, err := parser.Parse(strings.NewReader(script), "") | ||
| if err != nil { | ||
| // Parse errors are expected for some pentest cases | ||
| return "", err.Error(), 2 | ||
| } | ||
|
|
||
| var outBuf, errBuf bytes.Buffer | ||
| opts := []interp.RunnerOption{ | ||
| interp.StdIO(nil, &outBuf, &errBuf), | ||
| } | ||
| if dir != "" { | ||
| opts = append(opts, interp.AllowedPaths([]string{dir})) | ||
| } | ||
|
|
||
| runner, err := interp.New(opts...) | ||
| require.NoError(t, err) | ||
| defer runner.Close() | ||
|
|
||
| if dir != "" { | ||
| runner.Dir = dir | ||
| } | ||
|
|
||
| err = runner.Run(ctx, prog) | ||
| exitCode := 0 | ||
| if err != nil { | ||
| var es interp.ExitStatus | ||
| if errors.As(err, &es) { | ||
| exitCode = int(es) | ||
| } else if ctx.Err() != nil { | ||
| return outBuf.String(), errBuf.String(), -1 // timeout | ||
| } else { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| } | ||
| return outBuf.String(), errBuf.String(), exitCode | ||
| } | ||
|
|
||
| // --- Path traversal attacks --- | ||
|
|
||
| func TestPentestRedirPathTraversal(t *testing.T) { | ||
| dir := t.TempDir() | ||
| tests := []struct { | ||
| name string | ||
| script string | ||
| }{ | ||
| {"dot-dot traversal", "echo hello > /dev/null/../../tmp/evil"}, | ||
| {"dot-dot from devnull", "echo hello > /dev/null/../passwd"}, | ||
| {"double slash", "echo hello > /dev//null"}, | ||
| {"dot in path", "echo hello > /dev/./null"}, | ||
| {"trailing slash", "echo hello > /dev/null/"}, | ||
| {"case variation", "echo hello > /Dev/Null"}, | ||
| {"relative devnull", "echo hello > dev/null"}, | ||
| {"bare null", "echo hello > null"}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| stdout, stderr, code := pentestRedirRun(t, tt.script, dir) | ||
| assert.Equal(t, "", stdout, "should produce no stdout") | ||
| assert.NotEqual(t, 0, code, "should fail with non-zero exit") | ||
| // Should either be validation error (exit 2) or runtime error | ||
| assert.True(t, code == 2 || code == 1, "exit code should be 1 or 2, got %d", code) | ||
| _ = stderr // error message varies | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // --- Variable expansion attacks --- | ||
|
|
||
| func TestPentestRedirVariableExpansion(t *testing.T) { | ||
| dir := t.TempDir() | ||
| tests := []struct { | ||
| name string | ||
| script string | ||
| }{ | ||
| {"variable target", "TARGET=/dev/null; echo hello > $TARGET"}, | ||
| {"variable partial", "DEV=/dev; echo hello > $DEV/null"}, | ||
| {"variable with braces", "TARGET=/dev/null; echo hello > ${TARGET}"}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| _, _, code := pentestRedirRun(t, tt.script, dir) | ||
| assert.Equal(t, 2, code, "variable expansion in redirect target should be blocked at validation") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // --- Quoting attacks --- | ||
|
|
||
| func TestPentestRedirQuotedDevNull(t *testing.T) { | ||
| dir := t.TempDir() | ||
| tests := []struct { | ||
| name string | ||
| script string | ||
| }{ | ||
| {"single quoted", "echo hello > '/dev/null'"}, | ||
| {"double quoted", `echo hello > "/dev/null"`}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| _, _, code := pentestRedirRun(t, tt.script, dir) | ||
| // Quoted paths have different AST structure (SglQuoted/DblQuoted vs Lit) | ||
| // Our check requires a single Lit part, so quoted paths should be rejected | ||
| assert.Equal(t, 2, code, "quoted /dev/null in redirect should be blocked at validation") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // --- Glob/wildcard attacks --- | ||
|
|
||
| func TestPentestRedirGlobInTarget(t *testing.T) { | ||
| dir := t.TempDir() | ||
| // Glob characters in redirect targets | ||
| _, _, code := pentestRedirRun(t, "echo hello > /dev/nul?", dir) | ||
| assert.Equal(t, 2, code, "glob in redirect target should be rejected") | ||
| } | ||
|
|
||
| // --- fd duplication attacks --- | ||
|
|
||
| func TestPentestRedirFdDupAttacks(t *testing.T) { | ||
| dir := t.TempDir() | ||
| tests := []struct { | ||
| name string | ||
| script string | ||
| }{ | ||
| {"fd 0 dup", "echo hello 0>&1"}, | ||
| {"fd 3 dup", "echo hello 3>&1"}, | ||
| {"fd 9 dup", "echo hello 9>&1"}, | ||
| {"fd to 0", "echo hello >&0"}, | ||
| {"fd to 3", "echo hello >&3"}, | ||
| {"close fd", "echo hello >&-"}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| _, _, code := pentestRedirRun(t, tt.script, dir) | ||
| assert.Equal(t, 2, code, "unsupported fd duplication should be blocked") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // --- Redirect-to-file attacks that must remain blocked --- | ||
|
|
||
| func TestPentestRedirToSensitiveFiles(t *testing.T) { | ||
| dir := t.TempDir() | ||
| tests := []struct { | ||
| name string | ||
| script string | ||
| }{ | ||
| {"etc passwd", "echo evil > /etc/passwd"}, | ||
| {"etc shadow", "echo evil > /etc/shadow"}, | ||
| {"tmp file", "echo evil > /tmp/evil.txt"}, | ||
| {"home file", "echo evil > ~/.bashrc"}, | ||
| {"proc self", "echo evil > /proc/self/mem"}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| _, stderr, code := pentestRedirRun(t, tt.script, dir) | ||
| assert.NotEqual(t, 0, code, "redirect to %s should be blocked", tt.name) | ||
| _ = stderr | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // --- DplIn still blocked --- | ||
|
|
||
| func TestPentestRedirDplInBlocked(t *testing.T) { | ||
| dir := t.TempDir() | ||
| _, stderr, code := pentestRedirRun(t, "echo hello <&0", dir) | ||
| assert.Equal(t, 2, code) | ||
| assert.Contains(t, stderr, "fd duplication is not supported") | ||
| } | ||
|
|
||
| // --- Read-write redirect still blocked --- | ||
|
|
||
| func TestPentestRedirReadWriteBlocked(t *testing.T) { | ||
| dir := t.TempDir() | ||
| _, stderr, code := pentestRedirRun(t, "echo hello <> /dev/null", dir) | ||
| assert.Equal(t, 2, code) | ||
| assert.Contains(t, stderr, "file redirection is not supported") | ||
| } | ||
|
|
||
| // --- Herestring still blocked --- | ||
|
|
||
| func TestPentestRedirHerestringBlocked(t *testing.T) { | ||
| dir := t.TempDir() | ||
| _, stderr, code := pentestRedirRun(t, "cat <<< 'hello'", dir) | ||
| assert.Equal(t, 2, code) | ||
| assert.Contains(t, stderr, "herestring") | ||
| } | ||
|
|
||
| // --- Multiple redirects mixing allowed and blocked --- | ||
|
|
||
| func TestPentestRedirMixedAllowedBlocked(t *testing.T) { | ||
| dir := t.TempDir() | ||
| // First redirect is allowed, second is not | ||
| _, _, code := pentestRedirRun(t, "echo hello >/dev/null > /tmp/evil", dir) | ||
| assert.Equal(t, 2, code, "mixed redirects with blocked target should fail at validation") | ||
| } | ||
|
|
||
| // --- Ensure /dev/null redirect doesn't create any files --- | ||
|
|
||
| func TestPentestRedirNoFileCreated(t *testing.T) { | ||
| dir := t.TempDir() | ||
| // Run a redirect to /dev/null | ||
| pentestRedirRun(t, "echo hello >/dev/null", dir) | ||
|
|
||
| // Simple check: the temp dir should be empty (ls with no flags on empty dir produces no output) | ||
| stdout, _, _ := redirRun(t, "ls "+dir, dir) | ||
| assert.Equal(t, "", stdout, "no files should have been created") | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.