diff --git a/internal/gopher/gopher.go b/internal/gopher/gopher.go index 1ddcdc0..6843637 100644 --- a/internal/gopher/gopher.go +++ b/internal/gopher/gopher.go @@ -292,12 +292,20 @@ func (s *Server) fileAreas(c io.Writer) { fas, _ := s.B.Store.FileAreas() items := []item{{'i', "File areas of " + s.B.Conf().Name, ""}, {'i', "", ""}} for _, fa := range fas { - items = append(items, item{'1', fmt.Sprintf("%s (%d) %s", fa.Name, fa.Files, fa.Description), - fmt.Sprintf("/files/%d", fa.ID)}) + if openFiles(fa) { + items = append(items, item{'1', fmt.Sprintf("%s (%d) %s", fa.Name, fa.Files, fa.Description), + fmt.Sprintf("/files/%d", fa.ID)}) + } } s.menu(c, items) } +// openFiles reports whether a file area is gopher's: gopher has no login, +// so only an area anyone may download from (no download requirement). +func openFiles(fa *store.FileArea) bool { + return bbs.MustRequire(fa.DownloadReq).Allows(nil) +} + // kind is a file's gopher type, by its name. func kind(name string) byte { n := strings.ToLower(name) @@ -317,7 +325,7 @@ func (s *Server) files(c io.Writer, id int64) { return } fa, err := s.B.Store.FileArea(id) - if err != nil { + if err != nil || !openFiles(fa) { notFound(s, c) return } @@ -333,8 +341,8 @@ func (s *Server) files(c io.Writer, id int64) { s.menu(c, items) } -// download sends a file's bytes as they are, counting a download if all of -// it went. +// download sends a file's bytes as they are, if its area is gopher's, +// counting a download if all of it went. func (s *Server) download(c io.Writer, id int64) { if s.B.Files == nil || id < 1 { notFound(s, c) @@ -345,6 +353,10 @@ func (s *Server) download(c io.Writer, id int64) { notFound(s, c) return } + if fa, err := s.B.Store.FileArea(f.AreaID); err != nil || !openFiles(fa) { + notFound(s, c) + return + } r, err := s.B.Files.OpenFile(f) if err != nil { notFound(s, c) diff --git a/internal/gopher/gopher_test.go b/internal/gopher/gopher_test.go index 7e3b913..eeb3ba0 100644 --- a/internal/gopher/gopher_test.go +++ b/internal/gopher/gopher_test.go @@ -3,6 +3,7 @@ package gopher import ( "bytes" "context" + "fmt" "io" "log" "net" @@ -159,3 +160,24 @@ func TestCurl(t *testing.T) { t.Errorf("curl binary: %v %q", err, out) } } + +// TestGopherRestrictedFiles: a file area with a download requirement is not +// gopher's (gopher has no login): not listed, its files neither listed nor +// sent nor counted. +func TestGopherRestrictedFiles(t *testing.T) { + s := setup(t, "general", true) + if err := s.b.Store.SetFileAreaRequire(s.file.AreaID, "20", ""); err != nil { + t.Fatal(err) + } + if got := s.get(t, "/files"); strings.Contains(got, "Uploads") { + t.Errorf("a restricted area listed:\n%s", got) + } + for _, sel := range []string{fmt.Sprintf("/files/%d", s.file.AreaID), fmt.Sprintf("/file/%d", s.file.ID)} { + if got := s.get(t, sel); !strings.HasPrefix(got, "3Not here.") { + t.Errorf("%s: %q", sel, got) + } + } + if f, _ := s.b.Store.FileByID(s.file.ID); f.Downloads != 0 { + t.Errorf("downloads %d", f.Downloads) + } +}