Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(storage): keep the SSRF redirect policy on the KS3 client (#3440)
Captainbcfb6dfc1253
diff --git a/internal/application/service/file/ks3.go b/internal/application/service/file/ks3.goindex 2fbe4cd8c..e9f3e6470 100644--- a/internal/application/service/file/ks3.go+++ b/internal/application/service/file/ks3.go@@ -60,6 +60,8 @@ func newKS3Client(endpoint, region, accessKey, secretKey string) (*ks3s3.S3, errreturn nil, fmt.Errorf("unsafe KS3 endpoint: %w", err)}creds := credentials.NewStaticCredentials(accessKey, secretKey, "")+ httpClient := objectStorageHTTPClient()+ checkRedirect := httpClient.CheckRedirectclient := ks3s3.New(&ks3aws.Config{Credentials: creds,Region: region,@@ -68,8 +70,14 @@ func newKS3Client(endpoint, region, accessKey, secretKey string) (*ks3s3.S3, errS3ForcePathStyle: false, // KS3 uses virtual-hosted styleSignerVersion: "V2", // KS3 recommends V2 signingMaxRetries: 3,- HTTPClient: objectStorageHTTPClient(),+ HTTPClient: httpClient,})+ // ks3s3.New installs the SDK's own redirect policy on the client it was+ // handed: ten hops, no check of the target, and the previous hop's+ // Authorization copied onto every redirect, cross-host included. Put the+ // SSRF-safe policy back, so a redirect target is validated like any other+ // request and the signature stays with the endpoint.+ client.Config.HTTPClient.CheckRedirect = checkRedirectreturn client, nil}diff --git a/internal/application/service/file/ks3_redirect_test.go b/internal/application/service/file/ks3_redirect_test.gonew file mode 100644index 000000000..bde0671be--- /dev/null+++ b/internal/application/service/file/ks3_redirect_test.go@@ -0,0 +1,38 @@+package file++import (+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/Tencent/WeKnora/internal/utils"+)++// The KS3 SDK replaces the redirect policy on the client it is given with+// one that follows any target and carries the signature along; the client+// must come out of the constructor with the SSRF-safe policy it was built+// with.+func TestKS3ClientKeepsTheSSRFRedirectPolicy(t *testing.T) {+ t.Setenv("SSRF_WHITELIST", "ks3-cn-beijing.ksyuncs.com")+ utils.ResetSSRFWhitelistForTest()+ t.Cleanup(utils.ResetSSRFWhitelistForTest)+ client, err := newKS3Client("https://ks3-cn-beijing.ksyuncs.com", "cn-beijing", "ak", "secret")+ require.NoError(t, err)+ policy := client.Config.HTTPClient.CheckRedirect+ require.NotNil(t, policy)++ signed := httptest.NewRequest(http.MethodGet, "https://bucket.ks3-cn-beijing.ksyuncs.com/object", nil)+ signed.Header.Set("Authorization", "KSS ak:signature")++ // A redirect to another host must not take the signature with it.+ elsewhere := httptest.NewRequest(http.MethodGet, "https://93.184.216.34/object", nil)+ _ = policy(elsewhere, []*http.Request{signed})+ require.Empty(t, elsewhere.Header.Get("Authorization"))++ // A redirect into the private network is refused at redirect time, as+ // for every other client built on the SSRF-safe policy.+ inside := httptest.NewRequest(http.MethodGet, "http://127.0.0.1:8080/admin", nil)+ require.Error(t, policy(inside, []*http.Request{signed}))+}