minio/minio-dotnet

SignatureDoesNotMatch when CultureInfo is thai

KeNJiKunG opened this issue · 1 comments

The requests always return SignatureDoesNotMatch when CultureInfo.CurrentCulture is 'th-TH' but works when CultureInfo.CurrentCulture is 'en-GB'.

I found these codes will generated Authorization token with non-gregorian year which is incorrect.

private string GetScope(string region, DateTime signingDate, bool isSts = false)
{
return $"{signingDate:yyyyMMdd}/{region}/{GetService(isSts)}/aws4_request";
}

private string GetStringToSign(string region, DateTime signingDate,
string canonicalRequestHash, bool isSts = false)
{
var scope = GetScope(region, signingDate, isSts);
return $"AWS4-HMAC-SHA256\n{signingDate:yyyyMMddTHHmmssZ}\n{scope}\n{canonicalRequestHash}";
}

Here is my reproducible code.

using System.Globalization;
using Minio;

public class Program
{
    public static async Task Main(string[] args)
    {
        CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("th-TH");
        // CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-GB");

        var http = new HttpClient(new MyHttpClientHandler());

        var endpoint = "play.min.io";
        var accessKey = "USERNAME"; // <--- Replace with a valid access key.
        var secretKey = "PASSWORD"; // <--- Replace with a valid secret key.

        var factory = new MinioClientFactory(config =>
        {
            config.WithEndpoint(endpoint)
                    .WithCredentials(accessKey, secretKey)
                    .WithHttpClient(http, true)
                    .WithSSL();

            // config.SetTraceOn(new ReqeustLogger());
        });

        var client = factory.CreateClient().Build();

        // client.SetTraceOn(new ReqeustLogger());

        var isExists = await client.BucketExistsAsync(new Minio.DataModel.Args.BucketExistsArgs()
            .WithBucket("my-every-not-existing-random-bucket-2")
        );

        Console.WriteLine(isExists);

        var download = await client.GetObjectAsync(new Minio.DataModel.Args.GetObjectArgs()
            .WithBucket("my-every-not-existing-random-bucket-2")
            .WithObject("Not exists")
            .WithCallbackStream((stream) => { })
        );

        Console.WriteLine(download);
    }
}

public class MyHttpClientHandler : HttpClientHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var respons = await base.SendAsync(request, cancellationToken);

        Console.WriteLine( request.Headers );

        Console.WriteLine(respons.Headers);

        return respons;
    }
}

I also have the same problem. add this code before executing any minio actions for reset current culture

CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

worked for me.