fix(security): block SSRF via redirects in knowledge URL import

Use the SSRF-safe HTTP client for file_url downloads and add redirect-aware
guards to the docreader Playwright path, closing GHSA-44fp, GHSA-rv6v, and
GHSA-c7m6.
This commit is contained in:
wizardchen
2026-07-02 12:11:40 +08:00
committed by lyingbug
parent a17a2250ed
commit 1d322c525a
6 changed files with 387 additions and 1 deletions
@@ -0,0 +1,39 @@
package service
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
secutils "github.com/Tencent/WeKnora/internal/utils"
)
func TestDownloadFileFromURLBlocksRedirectToLoopback(t *testing.T) {
internal := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
_, _ = w.Write([]byte("INTERNAL_SECRET=metadata-token-AKIAEXAMPLE"))
}))
defer internal.Close()
if err := secutils.ValidateURLForSSRF(internal.URL); err == nil {
t.Fatalf("precondition: direct internal URL should be blocked")
}
attacker := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, internal.URL, http.StatusFound)
}))
defer attacker.Close()
attackerURL := attacker.URL + "/malicious.pdf"
fileName := ""
fileType := "pdf"
_, err := downloadFileFromURL(context.Background(), attackerURL, &fileName, &fileType)
if err == nil {
t.Fatal("expected redirect to loopback to be blocked")
}
if !strings.Contains(err.Error(), "redirect blocked") && !strings.Contains(err.Error(), "connection blocked") {
t.Fatalf("unexpected error: %v", err)
}
}