fix(bat-api): 完成 issue #19 同机 live 联调
bat-rust / Build and test Go API (push) Canceled after 0s
bat-rust / Build and test Rust (push) Canceled after 0s

This commit is contained in:
2026-08-29 22:58:01 +08:00
parent 90083302a2
commit 7f465523e1
22 changed files with 819 additions and 115 deletions
+27
View File
@@ -4,6 +4,7 @@ import (
"crypto/subtle"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
@@ -15,9 +16,35 @@ func (s *Server) wrapHandler(next http.Handler) http.Handler {
handler = s.rateLimitMiddleware(handler)
handler = s.securityHeadersMiddleware(handler)
handler = s.accessLogMiddleware(handler)
// Check before ServeMux can clean dot segments and route an escaped path
// to a different host-shaped endpoint.
handler = rejectDotSegmentsMiddleware(handler)
return handler
}
func rejectDotSegmentsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if hasDotPathSegment(r.URL.Path) || (r.URL.RawPath != "" && hasDotPathSegment(r.URL.RawPath)) {
http.NotFound(w, r)
return
}
next.ServeHTTP(w, r)
})
}
func hasDotPathSegment(rawPath string) bool {
decoded, err := url.PathUnescape(rawPath)
if err != nil {
return true
}
for _, segment := range strings.Split(decoded, "/") {
if segment == "." || segment == ".." {
return true
}
}
return false
}
func (s *Server) securityHeadersMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Content-Type-Options", "nosniff")