Skip to content

Commit

Permalink
net/http/cgi: PATH_INFO should be empty or start with a slash
Browse files Browse the repository at this point in the history
Fixed PATH_INFO not starting with a slash as described in RFC 3875 for PATH_INFO

Fixes golang#63925
  • Loading branch information
aimuz committed Nov 3, 2023
1 parent 450f469 commit 2fccfd4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
11 changes: 2 additions & 9 deletions src/net/http/cgi/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,14 @@ func removeLeadingDuplicates(env []string) (ret []string) {
}

func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
root := h.Root
if root == "" {
root = "/"
}

if len(req.TransferEncoding) > 0 && req.TransferEncoding[0] == "chunked" {
rw.WriteHeader(http.StatusBadRequest)
rw.Write([]byte("Chunked request bodies are not supported by CGI."))
return
}

pathInfo := req.URL.Path
if root != "/" && strings.HasPrefix(pathInfo, root) {
pathInfo = pathInfo[len(root):]
}
root := strings.TrimSuffix(h.Root, "/")
pathInfo := strings.TrimPrefix(req.URL.Path, root)

port := "80"
if req.TLS != nil {
Expand Down
6 changes: 3 additions & 3 deletions src/net/http/cgi/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ func TestPathInfoDirRoot(t *testing.T) {
Root: "/myscript/",
}
expectedMap := map[string]string{
"env-PATH_INFO": "bar",
"env-PATH_INFO": "/bar",
"env-QUERY_STRING": "a=b",
"env-REQUEST_URI": "/myscript/bar?a=b",
"env-SCRIPT_FILENAME": "testdata/test.cgi",
"env-SCRIPT_NAME": "/myscript/",
"env-SCRIPT_NAME": "/myscript",
}
runCgiTest(t, h, "GET /myscript/bar?a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
}
Expand Down Expand Up @@ -278,7 +278,7 @@ func TestPathInfoNoRoot(t *testing.T) {
"env-QUERY_STRING": "a=b",
"env-REQUEST_URI": "/bar?a=b",
"env-SCRIPT_FILENAME": "testdata/test.cgi",
"env-SCRIPT_NAME": "/",
"env-SCRIPT_NAME": "",
}
runCgiTest(t, h, "GET /bar?a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
}
Expand Down

0 comments on commit 2fccfd4

Please sign in to comment.