From c3515cb3cabcb23ce94106e6f117214a16209b69 Mon Sep 17 00:00:00 2001 From: mazkev Date: Mon, 16 Nov 2026 07:42:28 +0700 Subject: [PATCH] fix: prevent nil pointer panic on ProcessState and improve CGI environment compliance --- shell2http.go | 18 +++++++-- shell2http_test.go | 93 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 96 insertions(+), 15 deletions(-) diff --git a/shell2http.go b/shell2http.go index c2b01ad..ab57ea3 100644 --- a/shell2http.go +++ b/shell2http.go @@ -265,7 +265,12 @@ func execShellCommand(appConfig Config, shell string, params []string, req *http } } - exitCode := osExecCommand.ProcessState.ExitCode() + exitCode := 0 + if osExecCommand.ProcessState != nil { + exitCode = osExecCommand.ProcessState.ExitCode() + } else if err != nil { + exitCode = 1 + } return shellOut, exitCode, err } @@ -365,12 +370,12 @@ func setCGIEnv(cmd *exec.Cmd, req *http.Request, appConfig Config) { if envName == "PROXY" { continue } - cmd.Env = append(cmd.Env, fmt.Sprintf("HTTP_%s=%s", envName, headerValue[0])) + cmd.Env = append(cmd.Env, fmt.Sprintf("HTTP_%s=%s", envName, strings.Join(headerValue, ", "))) } remoteHost, remotePort, err := net.SplitHostPort(req.RemoteAddr) if err != nil { - log.Printf("failed to parse remote address %s: %s", req.RemoteAddr, err) + remoteHost = req.RemoteAddr } CGIVars := [...]struct { cgiName, value string @@ -391,6 +396,13 @@ func setCGIEnv(cmd *exec.Cmd, req *http.Request, appConfig Config) { for _, row := range CGIVars { cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", row.cgiName, row.value)) } + + if req.ContentLength > 0 { + cmd.Env = append(cmd.Env, fmt.Sprintf("CONTENT_LENGTH=%d", req.ContentLength)) + } + if contentType := req.Header.Get("Content-Type"); contentType != "" { + cmd.Env = append(cmd.Env, fmt.Sprintf("CONTENT_TYPE=%s", contentType)) + } } /* diff --git a/shell2http_test.go b/shell2http_test.go index 149c213..9ab806f 100644 --- a/shell2http_test.go +++ b/shell2http_test.go @@ -7,10 +7,14 @@ import ( "net" "net/http" "os" + "os/exec" "reflect" + "runtime" "strings" "testing" "time" + + "github.com/msoap/raphanus" ) func Test_parseCGIHeaders(t *testing.T) { @@ -171,6 +175,10 @@ func testHTTP(t *testing.T, method, url, postData string, fn func(body string) b } func Test_main(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("skipping on windows: requires unix echo and cat executables") + } + port := getFreePort(t) os.Args = []string{"shell2http", "-add-exit", @@ -180,7 +188,7 @@ func Test_main(t *testing.T) { "-export-vars=HOME", "-one-thread", "-shell=", - "-log=/dev/null", + "-log=" + os.DevNull, "-port=" + port, "GET:/echo", "echo 123", "POST:/form", "echo var=$v_var", @@ -193,18 +201,19 @@ func Test_main(t *testing.T) { // hide stderr oldStderr := os.Stderr // keep backup of the real stderr - newStderr, err := os.Open("/dev/null") + newStderr, err := os.Open(os.DevNull) if err != nil { - t.Errorf("open /dev/null: %s", err) - } - os.Stderr = newStderr - defer func() { - os.Stderr = oldStderr - err := newStderr.Close() - if err != nil { - t.Errorf("Stderr Close failed: %s", err) - } - }() + t.Errorf("open %s: %s", os.DevNull, err) + } else { + os.Stderr = newStderr + defer func() { + os.Stderr = oldStderr + err := newStderr.Close() + if err != nil { + t.Errorf("Stderr Close failed: %s", err) + } + }() + } testHTTP(t, "GET", "http://localhost:"+port+"/", "", func(res string) bool { return len(res) > 0 && strings.HasPrefix(res, "") }, @@ -409,3 +418,63 @@ func Test_parsePathAndCommands(t *testing.T) { }) } } + +func Test_execShellCommand_ProcessStateNil(t *testing.T) { + appConfig := Config{} + req, err := http.NewRequest(http.MethodGet, "http://localhost/test", nil) + if err != nil { + t.Fatal(err) + } + + // Executing a nonexistent executable with shell="" should not panic with nil pointer dereference + shellOut, exitCode, err := execShellCommand(appConfig, "nonexistent_command_12345", nil, req, raphanus.DB{}) + if err == nil { + t.Errorf("expected error for nonexistent command, got nil") + } + if exitCode == 0 { + t.Errorf("expected non-zero exit code on failure, got %d", exitCode) + } + if len(shellOut) != 0 { + t.Errorf("expected empty shellOut, got %s", string(shellOut)) + } +} + +func Test_setCGIEnv_MultiHeadersAndContent(t *testing.T) { + appConfig := Config{ + host: "example.com", + port: 8080, + } + req, err := http.NewRequest(http.MethodPost, "http://example.com/cgi?q=1", strings.NewReader("hello world")) + if err != nil { + t.Fatal(err) + } + req.Header.Add("Accept-Encoding", "gzip") + req.Header.Add("Accept-Encoding", "deflate") + req.Header.Set("Content-Type", "text/plain") + req.ContentLength = 11 + req.RemoteAddr = "192.0.2.1" // without port to test fallback + + cmd := &exec.Cmd{} + setCGIEnv(cmd, req, appConfig) + + envMap := make(map[string]string) + for _, env := range cmd.Env { + parts := strings.SplitN(env, "=", 2) + if len(parts) == 2 { + envMap[parts[0]] = parts[1] + } + } + + if envMap["HTTP_ACCEPT_ENCODING"] != "gzip, deflate" { + t.Errorf("expected 'gzip, deflate', got %q", envMap["HTTP_ACCEPT_ENCODING"]) + } + if envMap["CONTENT_LENGTH"] != "11" { + t.Errorf("expected CONTENT_LENGTH=11, got %q", envMap["CONTENT_LENGTH"]) + } + if envMap["CONTENT_TYPE"] != "text/plain" { + t.Errorf("expected CONTENT_TYPE='text/plain', got %q", envMap["CONTENT_TYPE"]) + } + if envMap["REMOTE_ADDR"] != "192.0.2.1" { + t.Errorf("expected REMOTE_ADDR='192.0.2.1', got %q", envMap["REMOTE_ADDR"]) + } +}