mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-18 23:25:10 +08:00
fix security advisory issues
This commit is contained in:
@@ -95,11 +95,12 @@ func setStaticContentType(w http.ResponseWriter, fullPath string) {
|
||||
}
|
||||
|
||||
func (h *Handler) serveFromDisk(w http.ResponseWriter, r *http.Request, staticDir string) {
|
||||
root := filepath.Clean(staticDir)
|
||||
path := strings.TrimPrefix(r.URL.Path, "/admin")
|
||||
path = strings.TrimPrefix(path, "/")
|
||||
if path != "" && strings.Contains(path, ".") {
|
||||
full := filepath.Join(staticDir, filepath.Clean(path))
|
||||
if !strings.HasPrefix(full, staticDir) {
|
||||
full := filepath.Join(root, filepath.Clean(path))
|
||||
if full != root && !strings.HasPrefix(full, root+string(os.PathSeparator)) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
@@ -116,7 +117,7 @@ func (h *Handler) serveFromDisk(w http.ResponseWriter, r *http.Request, staticDi
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
index := filepath.Join(staticDir, "index.html")
|
||||
index := filepath.Join(root, "index.html")
|
||||
if _, err := os.Stat(index); err != nil {
|
||||
http.Error(w, "index.html not found", http.StatusNotFound)
|
||||
return
|
||||
|
||||
@@ -78,6 +78,33 @@ func TestServeFromDiskPinsContentType(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeFromDiskRejectsSiblingDirectoryWithSharedPrefix(t *testing.T) {
|
||||
parent := t.TempDir()
|
||||
staticDir := filepath.Join(parent, "admin")
|
||||
siblingDir := filepath.Join(parent, "admin-leak")
|
||||
if err := os.MkdirAll(staticDir, 0o755); err != nil {
|
||||
t.Fatalf("mkdir static dir: %v", err)
|
||||
}
|
||||
if err := os.MkdirAll(siblingDir, 0o755); err != nil {
|
||||
t.Fatalf("mkdir sibling dir: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(siblingDir, "secret.txt"), []byte("secret"), 0o644); err != nil {
|
||||
t.Fatalf("write sibling secret: %v", err)
|
||||
}
|
||||
|
||||
h := &Handler{StaticDir: staticDir}
|
||||
req := httptest.NewRequest(http.MethodGet, "/admin/../admin-leak/secret.txt", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.serveFromDisk(rec, req, staticDir)
|
||||
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want 404", rec.Code)
|
||||
}
|
||||
if body := rec.Body.String(); strings.Contains(body, "secret") {
|
||||
t.Fatal("served content from sibling directory")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetStaticContentTypeUnknownExtensionFallsThrough verifies that unknown
|
||||
// extensions leave the Content-Type header unset, so http.ServeFile can apply
|
||||
// its own detection (sniffing or mime.TypeByExtension) for cases the pinned
|
||||
|
||||
Reference in New Issue
Block a user