diff --git a/internal/application/service/file/ks3.go b/internal/application/service/file/ks3.go index 2fbe4cd8..e9f3e647 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, err return nil, fmt.Errorf("unsafe KS3 endpoint: %w", err) } creds := credentials.NewStaticCredentials(accessKey, secretKey, "") + httpClient := objectStorageHTTPClient() + checkRedirect := httpClient.CheckRedirect client := ks3s3.New(&ks3aws.Config{ Credentials: creds, Region: region, @@ -68,8 +70,14 @@ func newKS3Client(endpoint, region, accessKey, secretKey string) (*ks3s3.S3, err S3ForcePathStyle: false, // KS3 uses virtual-hosted style SignerVersion: "V2", // KS3 recommends V2 signing MaxRetries: 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 = checkRedirect return client, nil } diff --git a/internal/application/service/file/ks3_redirect_test.go b/internal/application/service/file/ks3_redirect_test.go new file mode 100644 index 00000000..bde0671b --- /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})) +}