fix(security): harden outbound requests and file previews

This commit is contained in:
wizardchen
2026-07-02 18:17:51 +08:00
committed by lyingbug
parent 7099a7e181
commit 663d13eb58
16 changed files with 546 additions and 110 deletions
@@ -1,6 +1,13 @@
package service
import "testing"
import (
stderrors "errors"
"net/http"
"net/http/httptest"
"testing"
secutils "github.com/Tencent/WeKnora/internal/utils"
)
func TestValidateEmbedWebhookURL(t *testing.T) {
withSSRFWhitelist(t, "*.example.com,127.0.0.1")
@@ -33,3 +40,27 @@ func TestSignEmbedWebhookBody(t *testing.T) {
t.Fatal("signature not deterministic")
}
}
func TestEmbedWebhookHTTPClientBlocksRedirectToInternalURL(t *testing.T) {
withSSRFWhitelist(t, "127.0.0.1")
origin := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "http://169.254.169.254/latest/meta-data/", http.StatusFound)
}))
defer origin.Close()
req, err := http.NewRequest(http.MethodPost, origin.URL, nil)
if err != nil {
t.Fatalf("NewRequest: %v", err)
}
resp, err := newEmbedWebhookHTTPClient().Do(req)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
if err == nil {
t.Fatal("expected redirect to link-local metadata endpoint to be blocked")
}
if !stderrors.Is(err, secutils.ErrSSRFRedirectBlocked) {
t.Fatalf("error = %v, want ErrSSRFRedirectBlocked", err)
}
}