minio-dotnet icon indicating copy to clipboard operation
minio-dotnet copied to clipboard

Error message in name length checks does not match logic

Open pbolduc opened this issue 3 years ago • 3 comments

There are a number of parameter checks where the length check looks for > 512 but the error in the exception says 1024. For example

https://github.com/minio/minio-dotnet/blob/bb2d0943552114d37e59bf8194b384aad129d765/Minio/Helper/utils.cs#L78-L79

https://github.com/minio/minio-dotnet/blob/bb2d0943552114d37e59bf8194b384aad129d765/Minio/Helper/utils.cs#L84-L85

pbolduc avatar May 27 '22 19:05 pbolduc

Hi pbolduc,

Isn't this just due to the use of UTF-16 encoding? There's a test here which appears to show the correct behaviour. https://github.com/minio/minio-dotnet/blob/bad67b668de1105de8949283d28d85ea8e2efdb8/Minio.Tests/UtilsTest.cs#L88-L99

AdamHodgson avatar Jul 26 '22 17:07 AdamHodgson

If you are encoding at UTF-16, then each character is two bytes. But the error message is saying it cant be more then 1024 characters. It would be confusing to a caller when they log the error message any it says more than 1024 characters but they have ensured it is less than or equal to 1024 characters. The exception should say Object name cannot be greater than 512 characters.

For example the following test fails but shouldn't based on the exception message.

    [TestMethod]
    public void TestVeryLongObjectName513()
    {
        var objName = TestHelper.GetRandomName(513);
        Assert.IsTrue(objName.Length <= 1024); // precondition, object name cannot be greater than 1024 characters.
        utils.ValidateObjectName(objName);
    }

Edit: remove the try catch, this should not fail cause we are not more than 1024 characters

pbolduc avatar Jul 26 '22 18:07 pbolduc

Not sure if Minio is following AWS specifications, but based on Creating object key names, it says: The object key name is a sequence of Unicode characters with UTF-8 encoding of up to 1,024 bytes long.. Just because strings in the runtime are represent as UTF-16, when you serialize the request for the network, it will need to be encoded as UTF-8. So perhaps a better check would be:

    internal static void ValidateObjectName(string objectName)
    {
        if (string.IsNullOrEmpty(objectName) || objectName.Trim() == string.Empty)
            throw new InvalidObjectNameException(objectName, "Object name cannot be empty.");
        
        if (Encoding.UTF8.GetByteCount(objectName) > 1024)
            throw new InvalidObjectNameException(objectName, "Object name cannot be greater than 1024 characters.");
    }

pbolduc avatar Jul 26 '22 19:07 pbolduc