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
+32
View File
@@ -5,8 +5,16 @@ import (
"testing"
"github.com/Tencent/WeKnora/internal/im"
secutils "github.com/Tencent/WeKnora/internal/utils"
)
func withQQBotSSRFWhitelist(t *testing.T, whitelist string) {
t.Helper()
t.Setenv("SSRF_WHITELIST", whitelist)
secutils.ResetSSRFWhitelistForTest()
t.Cleanup(secutils.ResetSSRFWhitelistForTest)
}
func TestParseGatewayPayloadC2CMessage(t *testing.T) {
event := messageEvent{
ID: "msg-1",
@@ -76,3 +84,27 @@ func TestParseExpiresIn(t *testing.T) {
})
}
}
func TestNewClientRejectsPrivateAPIBaseURL(t *testing.T) {
withQQBotSSRFWhitelist(t, "")
if _, err := NewClient("app", "secret", "http://127.0.0.1:8080", ""); err == nil {
t.Fatal("expected private qqbot api_base_url to be rejected")
}
}
func TestNewClientRejectsInsecureGatewayURL(t *testing.T) {
withQQBotSSRFWhitelist(t, "127.0.0.1")
if _, err := NewClient("app", "secret", "http://127.0.0.1:8080", "ws://127.0.0.1/gateway"); err == nil {
t.Fatal("expected non-wss qqbot gateway_url to be rejected")
}
}
func TestNewClientAllowsWhitelistedPrivateDeployment(t *testing.T) {
withQQBotSSRFWhitelist(t, "127.0.0.1")
if _, err := NewClient("app", "secret", "http://127.0.0.1:8080", "wss://127.0.0.1/gateway"); err != nil {
t.Fatalf("expected whitelisted private qqbot endpoints to pass: %v", err)
}
}