Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve check for invalid ICC profiles #647

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/ImageSharp/MetaData/Profiles/ICC/IccProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,22 @@ public void Extend(byte[] bytes)
/// <returns>True if the profile is valid; False otherwise</returns>
public bool CheckIsValid()
{
return Enum.IsDefined(typeof(IccColorSpaceType), this.Header.DataColorSpace) &&
const int minSize = 128;
const int maxSize = 50_000_000; // it's unlikely there is a profile bigger than 50MB

bool arrayValid = true;
if (this.data != null)
{
arrayValid = this.data.Length >= minSize &&
this.data.Length >= this.Header.Size;
}

return arrayValid &&
Enum.IsDefined(typeof(IccColorSpaceType), this.Header.DataColorSpace) &&
Enum.IsDefined(typeof(IccColorSpaceType), this.Header.ProfileConnectionSpace) &&
Enum.IsDefined(typeof(IccRenderingIntent), this.Header.RenderingIntent) &&
this.Header.Size >= 128 &&
this.Header.Size < 50_000_000; // it's unlikely there is a profile bigger than 50MB
this.Header.Size >= minSize &&
this.Header.Size < maxSize;
}

/// <summary>
Expand Down
119 changes: 94 additions & 25 deletions tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ public static byte[] CreateHeaderRandomArray(uint size, uint nrOfEntries, byte[]
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
// Nr of tag table entries (0)
// Nr of tag table entries
(byte)(nrOfEntries >> 24), (byte)(nrOfEntries >> 16), (byte)(nrOfEntries >> 8), (byte)nrOfEntries
});
}

public static byte[] Profile_Random_Array = ArrayHelper.Concat(CreateHeaderRandomArray(168, 2, Profile_Random_Id_Array),
public static readonly byte[] Profile_Random_Array = ArrayHelper.Concat(CreateHeaderRandomArray(168, 2, Profile_Random_Id_Array),
new byte[]
{
0x00, 0x00, 0x00, 0x00, // tag signature (Unknown)
Expand All @@ -119,7 +119,7 @@ public static byte[] CreateHeaderRandomArray(uint size, uint nrOfEntries, byte[]
IccTestDataTagDataEntry.Unknown_Arr
);

public static IccProfile Profile_Random_Val = new IccProfile(CreateHeaderRandomValue(168,
public static readonly IccProfile Profile_Random_Val = new IccProfile(CreateHeaderRandomValue(168,
#if !NETSTANDARD1_1
Profile_Random_Id_Value,
#else
Expand All @@ -132,41 +132,110 @@ public static byte[] CreateHeaderRandomArray(uint size, uint nrOfEntries, byte[]
IccTestDataTagDataEntry.Unknown_Val
});

public static byte[] Header_Corrupt1_Array =
public static readonly byte[] Header_CorruptDataColorSpace_Array =
{
0x81, 0xB1, 0x81, 0xE4, 0x82, 0x16, 0x82, 0x49, 0x82, 0x7B, 0x82, 0xAD, 0x82, 0xDF, 0x83, 0x11,
0x83, 0x43, 0x83, 0x75, 0x83, 0xA7, 0x83, 0xD8, 0x84, 0x0A, 0x84, 0x3B, 0x84, 0x6C, 0x84, 0x9E,
0x84, 0xCF, 0x85, 0x00, 0x85, 0x31, 0x85, 0x62, 0x85, 0x93, 0x85, 0xC3, 0x85, 0xF4, 0x86, 0x24,
0x86, 0x55, 0x86, 0x85, 0x86, 0xB5, 0x86, 0xE6, 0x87, 0x16, 0x87, 0x46, 0x87, 0x76, 0x87, 0xA5,
0x87, 0xD5, 0x88, 0x05, 0x88, 0x34, 0x88, 0x64, 0x88, 0x93, 0x88, 0xC3, 0x88, 0xF2, 0x89, 0x21,
0x89, 0x50, 0x89, 0x7F, 0x89, 0xAE, 0x89, 0xDD, 0x8A, 0x0C, 0x8A, 0x3B, 0x8A, 0x69, 0x8A, 0x98,
0x8A, 0xC6, 0x8A, 0xF5, 0x8B, 0x23, 0x8B, 0x51, 0x8B, 0x7F, 0x8B, 0xAE, 0x8B, 0xDC, 0x8C, 0x09,
0x8C, 0x37, 0x8C, 0x65, 0x8C, 0x93, 0x8C, 0xC1, 0x8C, 0xEE, 0x8D, 0x1C, 0x8D, 0x49, 0x8D, 0x76,
0x00, 0x00, 0x00, 0x80, // Size
0x61, 0x62, 0x63, 0x64, // CmmType
0x04, 0x30, 0x00, 0x00, // Version
0x6D, 0x6E, 0x74, 0x72, // Class
0x68, 0x45, 0x8D, 0x6A, // DataColorSpace
0x58, 0x59, 0x5A, 0x20, // ProfileConnectionSpace
0x07, 0xC6, 0x00, 0x0B, 0x00, 0x1A, 0x00, 0x07, 0x00, 0x15, 0x00, 0x2A, // CreationDate
0x61, 0x63, 0x73, 0x70, // FileSignature
0x4D, 0x53, 0x46, 0x54, // PrimaryPlatformSignature
0x00, 0x00, 0x00, 0x01, // Flags
0x07, 0x5B, 0xCD, 0x15, // DeviceManufacturer
0x3A, 0xDE, 0x68, 0xB1, // DeviceModel
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, // DeviceAttributes
0x00, 0x00, 0x00, 0x03, // RenderingIntent
0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, // PcsIlluminant
0x64, 0x63, 0x62, 0x61, // CreatorSignature
// Profile ID
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// Padding
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};

public static byte[] Header_Corrupt2_Array =
public static readonly byte[] Header_CorruptProfileConnectionSpace_Array =
{
0x23, 0x74, 0x6D, 0x6D, 0xB1, 0xBC, 0x28, 0xB2, 0x6D, 0x0B, 0xA3, 0x9C, 0x2D, 0x60, 0x6C, 0xB4,
0x96, 0xF2, 0x31, 0x88, 0x6C, 0x67, 0x8B, 0xA9, 0x35, 0x31, 0x6C, 0x24, 0x81, 0xAE, 0x38, 0x64,
0x6B, 0xE9, 0x78, 0xEC, 0x3B, 0x28, 0x6B, 0xB7, 0x71, 0x4F, 0x3D, 0x87, 0x6B, 0x8C, 0x6A, 0xC3,
0x3F, 0x87, 0x6B, 0x68, 0x65, 0x33, 0x41, 0x30, 0x6B, 0x4A, 0x60, 0x8C, 0x42, 0x8C, 0x6B, 0x32,
0x5C, 0xB8, 0x43, 0xA2, 0x6B, 0x1F, 0x59, 0xA4, 0x44, 0x79, 0x6B, 0x10, 0x57, 0x3B, 0x45, 0x1A,
0x6B, 0x05, 0x55, 0x68, 0x45, 0x8D, 0x6A, 0xFE, 0x54, 0x15, 0x45, 0xDA, 0x6A, 0xF9, 0x53, 0x2A,
0x46, 0x16, 0x6A, 0xF5, 0x52, 0x74, 0x46, 0x27, 0x6A, 0xF4, 0x52, 0x43, 0x46, 0x27, 0x6A, 0xF4,
0x52, 0x43, 0x46, 0x27, 0x6A, 0xF4, 0x52, 0x43, 0x46, 0x27, 0x6A, 0xF4, 0x52, 0x43, 0x46, 0x27,
0x00, 0x00, 0x00, 0x80, // Size
0x62, 0x63, 0x64, 0x65, // CmmType
0x04, 0x30, 0x00, 0x00, // Version
0x6D, 0x6E, 0x74, 0x72, // Class
0x52, 0x47, 0x42, 0x20, // DataColorSpace
0x68, 0x45, 0x8D, 0x6A, // ProfileConnectionSpace
0x07, 0xC6, 0x00, 0x0B, 0x00, 0x1A, 0x00, 0x07, 0x00, 0x15, 0x00, 0x2A, // CreationDate
0x61, 0x63, 0x73, 0x70, // FileSignature
0x4D, 0x53, 0x46, 0x54, // PrimaryPlatformSignature
0x00, 0x00, 0x00, 0x01, // Flags
0x07, 0x5B, 0xCD, 0x15, // DeviceManufacturer
0x3A, 0xDE, 0x68, 0xB1, // DeviceModel
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, // DeviceAttributes
0x00, 0x00, 0x00, 0x03, // RenderingIntent
0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, // PcsIlluminant
0x64, 0x63, 0x62, 0x61, // CreatorSignature
// Profile ID
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// Padding
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};

public static readonly byte[] Header_CorruptRenderingIntent_Array =
{
0x00, 0x00, 0x00, 0x80, // Size
0x63, 0x64, 0x65, 0x66, // CmmType
0x04, 0x30, 0x00, 0x00, // Version
0x6D, 0x6E, 0x74, 0x72, // Class
0x52, 0x47, 0x42, 0x20, // DataColorSpace
0x58, 0x59, 0x5A, 0x20, // ProfileConnectionSpace
0x07, 0xC6, 0x00, 0x0B, 0x00, 0x1A, 0x00, 0x07, 0x00, 0x15, 0x00, 0x2A, // CreationDate
0x61, 0x63, 0x73, 0x70, // FileSignature
0x4D, 0x53, 0x46, 0x54, // PrimaryPlatformSignature
0x00, 0x00, 0x00, 0x01, // Flags
0x07, 0x5B, 0xCD, 0x15, // DeviceManufacturer
0x3A, 0xDE, 0x68, 0xB1, // DeviceModel
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, // DeviceAttributes
0x33, 0x41, 0x30, 0x6B, // RenderingIntent
0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, // PcsIlluminant
0x64, 0x63, 0x62, 0x61, // CreatorSignature
// Profile ID
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// Padding
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};

public static readonly byte[] Header_DataTooSmall_Array = new byte[127];

public static readonly byte[] Header_InvalidSizeSmall_Array = CreateHeaderRandomArray(127, 0, Header_Random_Id_Array);

public static readonly byte[] Header_InvalidSizeBig_Array = CreateHeaderRandomArray(50_000_000, 0, Header_Random_Id_Array);

public static readonly byte[] Header_SizeBiggerThanData_Array = CreateHeaderRandomArray(160, 0, Header_Random_Id_Array);

public static object[][] ProfileIdTestData =
public static readonly object[][] ProfileIdTestData =
{
new object[] { Header_Random_Array, Header_Random_Id_Value },
new object[] { Profile_Random_Array, Profile_Random_Id_Value },
};

public static object[][] ProfileValidityTestData =
public static readonly object[][] ProfileValidityTestData =
{
new object[] { Header_Corrupt1_Array, false },
new object[] { Header_Corrupt2_Array, false },
new object[] { Header_CorruptDataColorSpace_Array, false },
new object[] { Header_CorruptProfileConnectionSpace_Array, false },
new object[] { Header_CorruptRenderingIntent_Array, false },
new object[] { Header_DataTooSmall_Array, false },
new object[] { Header_InvalidSizeSmall_Array, false },
new object[] { Header_InvalidSizeBig_Array, false },
new object[] { Header_SizeBiggerThanData_Array, false },
new object[] { Header_Random_Array, true },
};
}
Expand Down