Prechádzať zdrojové kódy

Hide directory browsing on the static content (#85)

* Hide directory browsing on the static content

* update changelog
Henri Vasserman 2 mesiacov pred
rodič
commit
38d62eeb56
3 zmenil súbory, kde vykonal 14 pridanie a 1 odobranie
  1. 1 0
      docs/docs/CHANGELOG.md
  2. 12 0
      internal/headers.go
  3. 1 1
      lib/anubis.go

+ 1 - 0
docs/docs/CHANGELOG.md

@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 
 ## [Unreleased]
 ## [Unreleased]
 
 
+- Hide the directory listings for Anubis' internal static content
 - Changed `--debug-x-real-ip-default` to `--use-remote-address`, getting the IP address from the request's socket address instead.
 - Changed `--debug-x-real-ip-default` to `--use-remote-address`, getting the IP address from the request's socket address instead.
 - DroneBL lookups have been disabled by default
 - DroneBL lookups have been disabled by default
 
 

+ 12 - 0
internal/headers.go

@@ -4,6 +4,7 @@ import (
 	"log/slog"
 	"log/slog"
 	"net"
 	"net"
 	"net/http"
 	"net/http"
+	"strings"
 
 
 	"github.com/TecharoHQ/anubis"
 	"github.com/TecharoHQ/anubis"
 	"github.com/sebest/xff"
 	"github.com/sebest/xff"
@@ -62,3 +63,14 @@ func XForwardedForToXRealIP(next http.Handler) http.Handler {
 		next.ServeHTTP(w, r)
 		next.ServeHTTP(w, r)
 	})
 	})
 }
 }
+
+// Do not allow browsing directory listings in paths that end with /
+func NoBrowsing(next http.Handler) http.Handler {
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if strings.HasSuffix(r.URL.Path, "/") {
+			http.NotFound(w, r)
+			return
+		}
+		next.ServeHTTP(w, r)
+	})
+}

+ 1 - 1
lib/anubis.go

@@ -119,7 +119,7 @@ func New(opts Options) (*Server, error) {
 	mux := http.NewServeMux()
 	mux := http.NewServeMux()
 	xess.Mount(mux)
 	xess.Mount(mux)
 
 
-	mux.Handle(anubis.StaticPath, internal.UnchangingCache(http.StripPrefix(anubis.StaticPath, http.FileServerFS(web.Static))))
+	mux.Handle(anubis.StaticPath, internal.UnchangingCache(internal.NoBrowsing(http.StripPrefix(anubis.StaticPath, http.FileServerFS(web.Static)))))
 
 
 	if opts.ServeRobotsTXT {
 	if opts.ServeRobotsTXT {
 		mux.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
 		mux.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {