fix(storage): keep the SSRF redirect policy on the KS3 client (#3440)
ks3s3.New merges its default config into the one it is given and then installs the SDK's own redirect policy on the http.Client it was handed: up to ten hops, no check of the target, and the previous hop's Authorization header copied onto every redirect, cross-host included. That silently replaced the SSRF-safe policy the client was built with, so a redirect from the KS3 endpoint was followed without the target check every other request gets, and the request signature travelled to whatever host the redirect named. Go's own client drops Authorization on a cross-domain redirect precisely to avoid that. Save the policy before New and put it back on the client afterwards. The per-hop transport guard was never affected; this restores the redirect-time check and keeps the signature with the endpoint.main
parent
562b131e6c
commit
bcfb6dfc12
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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}))
|
||||
}
|
||||
Loading…
Reference in New Issue