Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions shell2http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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))
}
}

/*
Expand Down
93 changes: 81 additions & 12 deletions shell2http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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, "<!DOCTYPE html>") },
Expand Down Expand Up @@ -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"])
}
}