Skip to content

Commit 6f5f712

Browse files
committed
return an error if Dir is invoked with a normal file
1 parent 8e2f667 commit 6f5f712

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/net/http/fs.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,16 @@ func (d Dir) Open(name string) (File, error) {
7878
if dir == "" {
7979
dir = "."
8080
}
81+
fi, err := os.Stat(dir)
82+
if err != nil || !fi.IsDir() {
83+
return nil, errors.New("http: invalid directory path")
84+
}
8185
fullName := filepath.Join(dir, path)
8286
f, err := os.Open(fullName)
8387
if err != nil {
8488
return nil, mapOpenError(err, fullName, filepath.Separator, os.Stat)
8589
}
90+
8691
return f, nil
8792
}
8893

@@ -661,7 +666,7 @@ func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirec
661666
return
662667
}
663668
} else {
664-
if url != "/" && url[len(url)-1] == '/' {
669+
if url[len(url)-1] == '/' {
665670
localRedirect(w, r, "../"+path.Base(url))
666671
return
667672
}

src/net/http/fs_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,6 @@ func TestDirJoin(t *testing.T) {
475475
test(Dir("/etc"), "/hosts")
476476
test(Dir("/etc"), "hosts")
477477
test(Dir("/etc"), "../../../../hosts")
478-
479-
// Not really directories, but since we use this trick in
480-
// ServeFile, test it:
481-
test(Dir("/etc/hosts"), "")
482-
test(Dir("/etc/hosts"), "/")
483-
test(Dir("/etc/hosts"), "../")
484478
}
485479

486480
func TestEmptyDirOpenCWD(t *testing.T) {
@@ -497,6 +491,22 @@ func TestEmptyDirOpenCWD(t *testing.T) {
497491
test(Dir("./"))
498492
}
499493

494+
// issue 63769
495+
func TestDirNormalFile(t *testing.T) {
496+
test := func(d Dir, filename string) {
497+
f, err := d.Open(filename)
498+
if err == nil {
499+
f.Close()
500+
t.Fatalf("got nil, want error")
501+
}
502+
}
503+
504+
test(Dir("testdata/index.html"), "")
505+
test(Dir("testdata/index.html"), "/")
506+
test(Dir("testdata/index.html"), "..")
507+
test(Dir("testdata/index.html"), "../")
508+
}
509+
500510
func TestServeFileContentType(t *testing.T) { run(t, testServeFileContentType) }
501511
func testServeFileContentType(t *testing.T, mode testMode) {
502512
const ctype = "icecream/chocolate"
@@ -1672,24 +1682,15 @@ func (grw gzipResponseWriter) Flush() {
16721682
// Issue 63769
16731683
func TestFileServerDirWithRootFile(t *testing.T) { run(t, testFileServerDirWithRootFile) }
16741684
func testFileServerDirWithRootFile(t *testing.T, mode testMode) {
1675-
filename := "index.html"
1676-
contents, err := os.ReadFile("testdata/index.html")
1677-
if err != nil {
1678-
t.Fatal(err)
1679-
}
16801685
ts := newClientServerTest(t, mode, FileServer(Dir("testdata/index.html"))).ts
16811686
defer ts.Close()
16821687

16831688
res, err := ts.Client().Get(ts.URL)
16841689
if err != nil {
16851690
t.Fatal(err)
16861691
}
1687-
b, err := io.ReadAll(res.Body)
1688-
if err != nil {
1689-
t.Fatal("reading Body:", err)
1690-
}
1691-
if s := string(b); s != string(contents) {
1692-
t.Errorf("for path %q got %q, want %q", filename, s, contents)
1692+
if s := res.StatusCode; s != StatusInternalServerError {
1693+
t.Errorf("got %q, want 500", s)
16931694
}
16941695
res.Body.Close()
16951696
}

0 commit comments

Comments
 (0)