Skip to content

Commit

Permalink
session: fix ineffectual code
Browse files Browse the repository at this point in the history
* Rewrite directory index suggestion to prevent ineffectual code.
  • Loading branch information
yookoala committed Feb 5, 2021
1 parent acc804b commit e380833
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
10 changes: 5 additions & 5 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ func (fs *FileSystemRouter) Router() Middleware {
fastcgiScriptName, fastcgiPathInfo = matches[1], matches[2]
}

// If accessing a directory, try accessing document index file
if strings.HasSuffix(fastcgiScriptName, "/") {
fastcgiScriptName = path.Join(fastcgiScriptName, "index.php")
}

req.Params["PATH_INFO"] = fastcgiPathInfo
req.Params["PATH_TRANSLATED"] = filepath.Join(docroot, fastcgiPathInfo)
req.Params["SCRIPT_NAME"] = fastcgiScriptName
Expand All @@ -228,11 +233,6 @@ func (fs *FileSystemRouter) Router() Middleware {
}

// handle directory index
urlPath := r.URL.Path
if strings.HasSuffix(urlPath, "/") {
urlPath = path.Join(urlPath, "index.php")
}
req.Params["SCRIPT_FILENAME"] = path.Join(fs.DocRoot, urlPath)

return inner(client, req)
}
Expand Down
34 changes: 33 additions & 1 deletion session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestMapFilterRequest(t *testing.T) {
}
}

func TestFileSystemRouter(t *testing.T) {
func TestFileSystemRouter_PathTraversal(t *testing.T) {
fs := &gofast.FileSystemRouter{
DocRoot: "/non-exists/folder/structure",
Exts: []string{"php"},
Expand Down Expand Up @@ -211,3 +211,35 @@ func TestFileSystemRouter(t *testing.T) {
t.Errorf("expected \"%s\", got \"%s\"", want, have)
}
}

func TestFileSystemRouter_DirectoryPath(t *testing.T) {
fs := &gofast.FileSystemRouter{
DocRoot: "/non-exists/folder/structure",
Exts: []string{"php"},
DirIndex: []string{"index.php"},
}

h := gofast.Chain(
gofast.BasicParamsMap,
fs.Router(),
)(func(client gofast.Client, req *gofast.Request) (resp *gofast.ResponsePipe, err error) {
err = fmt.Errorf("SCRIPT_FILENAME=%s", req.Params["SCRIPT_FILENAME"])
return
})

r, err := http.NewRequest("GET", "http://foobar.com/", nil)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
r.URL.Path = "/hello/"

_, err = h(nil, gofast.NewRequest(r))
if err == nil {
t.Errorf("expected error, got nil")
return
}

if want, have := "SCRIPT_FILENAME=/non-exists/folder/structure/hello/index.php", err.Error(); want != have {
t.Errorf("expected \"%s\", got \"%s\"", want, have)
}
}

0 comments on commit e380833

Please sign in to comment.