diff --git a/internal/application/service/file/s3.go b/internal/application/service/file/s3.go index 02da6a02..f43bb3b5 100644 --- a/internal/application/service/file/s3.go +++ b/internal/application/service/file/s3.go @@ -65,6 +65,12 @@ func newS3Client(endpoint, accessKey, secretKey, bucketName, region, pathPrefix client = s3.NewFromConfig(cfg, func(o *s3.Options) { o.BaseEndpoint = aws.String(endpoint) o.UsePathStyle = usePathStyle + if !strings.Contains(endpoint, "amazonaws.com") { + // S3-compatible services commonly reject the SDK's default + // trailing checksum negotiation. Only relax this for explicit + // non-AWS endpoints; standard AWS S3 keeps its default behavior. + o.RequestChecksumCalculation = aws.RequestChecksumCalculationWhenRequired + } o.HTTPClient = httpClient }) } else { diff --git a/internal/application/service/file/s3_test.go b/internal/application/service/file/s3_test.go index 8fff6945..0291e8b3 100644 --- a/internal/application/service/file/s3_test.go +++ b/internal/application/service/file/s3_test.go @@ -4,6 +4,9 @@ import ( "context" "strings" "testing" + + "github.com/Tencent/WeKnora/internal/utils" + "github.com/aws/aws-sdk-go-v2/aws" ) func TestNewS3Client_Credentials(t *testing.T) { @@ -92,6 +95,61 @@ func TestNewS3Client_EmptyEndpoint(t *testing.T) { } } +func TestNewS3Client_RequestChecksumCalculation(t *testing.T) { + const ( + compatibleEndpoint = "https://s3-compatible.example.com" + awsEndpoint = "https://s3.us-east-1.amazonaws.com" + ) + + // Keep the test independent of the developer's AWS profile while checking + // that a standard AWS endpoint still honors the SDK-resolved default. + t.Setenv("AWS_REQUEST_CHECKSUM_CALCULATION", "when_supported") + t.Setenv("SSRF_WHITELIST", "s3-compatible.example.com,s3.us-east-1.amazonaws.com") + utils.ResetSSRFWhitelistForTest() + t.Cleanup(utils.ResetSSRFWhitelistForTest) + + standard, err := newS3Client("", "ak", "sk", "bucket", "us-east-1", "", false) + if err != nil { + t.Fatalf("newS3Client() with empty endpoint error = %v", err) + } + wantStandard := standard.client.Options().RequestChecksumCalculation + + tests := []struct { + name string + endpoint string + want aws.RequestChecksumCalculation + }{ + { + name: "empty endpoint preserves standard AWS setting", + endpoint: "", + want: wantStandard, + }, + { + name: "AWS endpoint preserves standard AWS setting", + endpoint: awsEndpoint, + want: wantStandard, + }, + { + name: "S3-compatible endpoint calculates checksum only when required", + endpoint: compatibleEndpoint, + want: aws.RequestChecksumCalculationWhenRequired, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + svc, err := newS3Client(tt.endpoint, "ak", "sk", "bucket", "us-east-1", "", false) + if err != nil { + t.Fatalf("newS3Client() error = %v", err) + } + got := svc.client.Options().RequestChecksumCalculation + if got != tt.want { + t.Errorf("RequestChecksumCalculation = %v, want %v", got, tt.want) + } + }) + } +} + func TestParseS3FilePath(t *testing.T) { svc := &s3FileService{bucketName: "test-bucket"}