Skip to content

Commit 6458c8e

Browse files
aimuzgopherbot
authored andcommitted
net/http/cgi: the PATH_INFO should be empty or start with a slash
fixed PATH_INFO not starting with a slash as described in RFC 3875 for PATH_INFO. Fixes golang#63925 Change-Id: I1ead98dff190c53eb7a50546569ef6ded3199a0a GitHub-Last-Rev: 1c532e3 GitHub-Pull-Request: golang#63926 Reviewed-on: https://go-review.googlesource.com/c/go/+/539615 Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Damien Neil <[email protected]> Auto-Submit: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 42b2029 commit 6458c8e

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

src/net/http/cgi/host.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,14 @@ func removeLeadingDuplicates(env []string) (ret []string) {
115115
}
116116

117117
func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
118-
root := h.Root
119-
if root == "" {
120-
root = "/"
121-
}
122-
123118
if len(req.TransferEncoding) > 0 && req.TransferEncoding[0] == "chunked" {
124119
rw.WriteHeader(http.StatusBadRequest)
125120
rw.Write([]byte("Chunked request bodies are not supported by CGI."))
126121
return
127122
}
128123

129-
pathInfo := req.URL.Path
130-
if root != "/" && strings.HasPrefix(pathInfo, root) {
131-
pathInfo = pathInfo[len(root):]
132-
}
124+
root := strings.TrimRight(h.Root, "/")
125+
pathInfo := strings.TrimPrefix(req.URL.Path, root)
133126

134127
port := "80"
135128
if req.TLS != nil {

src/net/http/cgi/host_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,14 @@ func TestPathInfoDirRoot(t *testing.T) {
210210
check(t)
211211
h := &Handler{
212212
Path: "testdata/test.cgi",
213-
Root: "/myscript/",
213+
Root: "/myscript//",
214214
}
215215
expectedMap := map[string]string{
216-
"env-PATH_INFO": "bar",
216+
"env-PATH_INFO": "/bar",
217217
"env-QUERY_STRING": "a=b",
218218
"env-REQUEST_URI": "/myscript/bar?a=b",
219219
"env-SCRIPT_FILENAME": "testdata/test.cgi",
220-
"env-SCRIPT_NAME": "/myscript/",
220+
"env-SCRIPT_NAME": "/myscript",
221221
}
222222
runCgiTest(t, h, "GET /myscript/bar?a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
223223
}
@@ -278,7 +278,7 @@ func TestPathInfoNoRoot(t *testing.T) {
278278
"env-QUERY_STRING": "a=b",
279279
"env-REQUEST_URI": "/bar?a=b",
280280
"env-SCRIPT_FILENAME": "testdata/test.cgi",
281-
"env-SCRIPT_NAME": "/",
281+
"env-SCRIPT_NAME": "",
282282
}
283283
runCgiTest(t, h, "GET /bar?a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
284284
}

0 commit comments

Comments
 (0)