Skip to content

Commit 480bda8

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

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-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: 11 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,15 @@ func TestEmptyDirOpenCWD(t *testing.T) {
497491
test(Dir("./"))
498492
}
499493

494+
// issue 63769
495+
func TestDirNormalFile(t *testing.T) {
496+
f, err := Dir("testdata/index.html").Open("")
497+
if err == nil {
498+
f.Close()
499+
t.Fatalf("got nil, want error")
500+
}
501+
}
502+
500503
func TestServeFileContentType(t *testing.T) { run(t, testServeFileContentType) }
501504
func testServeFileContentType(t *testing.T, mode testMode) {
502505
const ctype = "icecream/chocolate"
@@ -1672,24 +1675,15 @@ func (grw gzipResponseWriter) Flush() {
16721675
// Issue 63769
16731676
func TestFileServerDirWithRootFile(t *testing.T) { run(t, testFileServerDirWithRootFile) }
16741677
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-
}
16801678
ts := newClientServerTest(t, mode, FileServer(Dir("testdata/index.html"))).ts
16811679
defer ts.Close()
16821680

16831681
res, err := ts.Client().Get(ts.URL)
16841682
if err != nil {
16851683
t.Fatal(err)
16861684
}
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)
1685+
if s := res.StatusCode; s != StatusInternalServerError {
1686+
t.Errorf("got %q, want 500", s)
16931687
}
16941688
res.Body.Close()
16951689
}

0 commit comments

Comments
 (0)