SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.
Go blob-storagecloud-drivedistributed-file-systemdistributed-storageerasure-codingfusehadoop-hdfshdfskuberneteslancelancedbobject-storageposixs3s3-storageseaweedfstiered-file-systemvector-search
filer, s3: reuse the volume server's guarded remote-storage client builder (#10990)
Chris Lu0b5fff2ccd97
diff --git a/weed/s3api/s3api_object_handlers.go b/weed/s3api/s3api_object_handlers.goindex f45f4166cc..2d55425155 100644--- a/weed/s3api/s3api_object_handlers.go+++ b/weed/s3api/s3api_object_handlers.go@@ -24,6 +24,7 @@ import ("github.com/seaweedfs/seaweedfs/weed/pb/remote_pb""github.com/seaweedfs/seaweedfs/weed/remote_storage""github.com/seaweedfs/seaweedfs/weed/security"+ weed_server "github.com/seaweedfs/seaweedfs/weed/server""github.com/seaweedfs/seaweedfs/weed/util""github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"@@ -3337,7 +3338,7 @@ func (s3a *S3ApiServer) openRemoteStream(ctx context.Context, bucket, object strreturn nil, err}- client, err := remote_storage.GetRemoteStorage(storageConf)+ client, err := weed_server.BuildGuardedRemoteStorageClient(ctx, storageConf, false)if err != nil {return nil, err}diff --git a/weed/server/filer_server_handlers_read.go b/weed/server/filer_server_handlers_read.goindex e12bf14456..2992f31324 100644--- a/weed/server/filer_server_handlers_read.go+++ b/weed/server/filer_server_handlers_read.go@@ -286,7 +286,7 @@ func (fs *FilerServer) streamFromRemote(ctx context.Context, dir, name string, oif err != nil {return nil, err}- client, err := remote_storage.GetRemoteStorage(storageConf)+ client, err := BuildGuardedRemoteStorageClient(ctx, storageConf, false)if err != nil {return nil, err}diff --git a/weed/server/volume_grpc_remote.go b/weed/server/volume_grpc_remote.goindex f4d775469a..4eedef0973 100644--- a/weed/server/volume_grpc_remote.go+++ b/weed/server/volume_grpc_remote.go@@ -350,6 +350,42 @@ func checkGcsCredentials(creds string) error {return nil}+// BuildGuardedRemoteStorageClient builds a remote storage client whose dial+// path is validated against the SSRF deny-list and pinned against DNS+// rebinding, unless allowUntrusted is set. It is the single builder for every+// caller that dials a caller-influenced RemoteConf endpoint: the volume+// FetchAndWriteNeedle write path and the filer and S3 gateway remote-mount read+// paths, which otherwise reach the same s3manager sink unguarded.+func BuildGuardedRemoteStorageClient(ctx context.Context, remoteConf *remote_pb.RemoteConf, allowUntrusted bool) (remote_storage.RemoteStorageClient, error) {+ if !allowUntrusted {+ if remoteConf.GetType() == "gcs" {+ if credsErr := checkGcsCredentials(remoteConf.GetGcsGoogleApplicationCredentials()); credsErr != nil {+ return nil, fmt.Errorf("reject remote credentials: %w", credsErr)+ }+ }+ if endpoint, makeClient, ok := guardedRemoteClient(remoteConf); ok {+ if validateErr := validateRemoteEndpoint(ctx, endpoint); validateErr != nil {+ return nil, fmt.Errorf("reject remote endpoint: %w", validateErr)+ }+ // Build a one-shot client whose dial path re-validates the resolved+ // IP every time. This pins the validated endpoint against DNS+ // rebinding (a hostname that resolves to a public IP for+ // validateRemoteEndpoint and then flips to 127.0.0.1 / 169.254.x.x+ // when the SDK dials).+ client, err := makeClient(newGuardedHTTPClient(endpoint))+ if err != nil {+ return nil, fmt.Errorf("get remote client: %w", err)+ }+ return client, nil+ }+ }+ client, err := remote_storage.GetRemoteStorage(remoteConf)+ if err != nil {+ return nil, fmt.Errorf("get remote client: %w", err)+ }+ return client, nil+}+func (vs *VolumeServer) FetchAndWriteNeedle(ctx context.Context, req *volume_server_pb.FetchAndWriteNeedleRequest) (resp *volume_server_pb.FetchAndWriteNeedleResponse, err error) {if err := vs.checkGrpcAdminAuth(ctx); err != nil {return nil, err@@ -366,29 +402,9 @@ func (vs *VolumeServer) FetchAndWriteNeedle(ctx context.Context, req *volume_serremoteConf := req.RemoteConf- if !vs.AllowUntrustedRemoteEndpoints && remoteConf.GetType() == "gcs" {- if credsErr := checkGcsCredentials(remoteConf.GetGcsGoogleApplicationCredentials()); credsErr != nil {- return nil, fmt.Errorf("reject remote credentials: %w", credsErr)- }- }-- var client remote_storage.RemoteStorageClient- var getClientErr error- if endpoint, makeClient, ok := guardedRemoteClient(remoteConf); ok && !vs.AllowUntrustedRemoteEndpoints {- if validateErr := validateRemoteEndpoint(ctx, endpoint); validateErr != nil {- return nil, fmt.Errorf("reject remote endpoint: %w", validateErr)- }- // Build a one-shot client whose dial path re-validates the resolved- // IP every time. This pins the validated endpoint against DNS- // rebinding (a hostname that resolves to a public IP for- // validateRemoteEndpoint and then flips to 127.0.0.1 / 169.254.x.x- // when the SDK dials).- client, getClientErr = makeClient(newGuardedHTTPClient(endpoint))- } else {- client, getClientErr = remote_storage.GetRemoteStorage(remoteConf)- }+ client, getClientErr := BuildGuardedRemoteStorageClient(ctx, remoteConf, vs.AllowUntrustedRemoteEndpoints)if getClientErr != nil {- return nil, fmt.Errorf("get remote client: %w", getClientErr)+ return nil, getClientErr}remoteStorageLocation := req.RemoteLocationdiff --git a/weed/server/volume_grpc_remote_test.go b/weed/server/volume_grpc_remote_test.goindex ddfa4862cd..0d3b2bf08c 100644--- a/weed/server/volume_grpc_remote_test.go+++ b/weed/server/volume_grpc_remote_test.go@@ -638,3 +638,36 @@ func TestGuardedReplicaDialerRebind(t *testing.T) {t.Fatalf("private peer must be allowed by the replica dialer, got %v", perr)}}++// TestBuildGuardedRemoteStorageClient confirms the shared builder refuses a+// caller-influenced endpoint that resolves to a blocked address, and a gcs+// credentials path, while allowUntrusted falls back to the plain builder.+func TestBuildGuardedRemoteStorageClient(t *testing.T) {+ loopbackS3 := &remote_pb.RemoteConf{+ Name: "poc",+ Type: "s3",+ S3Endpoint: "http://127.0.0.1:8000",+ S3AccessKey: "k",+ S3SecretKey: "s",+ S3Region: "us-east-1",+ }+ if _, err := BuildGuardedRemoteStorageClient(context.Background(), loopbackS3, false); err == nil {+ t.Error("expected a loopback s3 endpoint to be rejected")+ } else if !strings.Contains(err.Error(), "reject remote endpoint") {+ t.Errorf("error = %v, want reject remote endpoint", err)+ }+ if _, err := BuildGuardedRemoteStorageClient(context.Background(), loopbackS3, true); err != nil {+ t.Errorf("allowUntrusted should build the client: %v", err)+ }++ gcsPathCreds := &remote_pb.RemoteConf{+ Name: "poc",+ Type: "gcs",+ GcsGoogleApplicationCredentials: "/etc/hostname",+ }+ if _, err := BuildGuardedRemoteStorageClient(context.Background(), gcsPathCreds, false); err == nil {+ t.Error("expected a gcs credentials path to be rejected")+ } else if !strings.Contains(err.Error(), "reject remote credentials") {+ t.Errorf("error = %v, want reject remote credentials", err)+ }+}