Skip to content
Open
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
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="alhuda, Gary Gregory">Fix int overflow in PCX image buffer sizing, now throws AllocationRequestException (#552).</action>
<action type="fix" dev="ggregory" due-to="alhuda, Gary Gregory">Fix int overflow in Allocator.array/arrayList size checks (#551).</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Add messages when throwing NullPointerException.</action>
<action issue="IMAGING-399" type="fix" dev="pkarwasz" due-to="Piotr P. Karwasz, Gary Gregory">Inflate the PNG iCCP chunk only up to the profile size declared in its header, now throws ImagingException if the profile is shorter (#582).</action>
<!-- ADD -->
<action type="fix" dev="ggregory" due-to="Sean R, Gary Gregory">Add unit tests (#553).</action>
<!-- UPDATE -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
package org.apache.commons.imaging.formats.png.chunks;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.logging.Level;
Expand All @@ -27,6 +31,7 @@
import org.apache.commons.imaging.ImagingException;
import org.apache.commons.imaging.common.Allocator;
import org.apache.commons.imaging.common.BinaryFunctions;
import org.apache.commons.imaging.common.ByteConversions;
import org.apache.commons.io.IOUtils;

/**
Expand All @@ -42,6 +47,35 @@ public final class PngChunkIccp extends PngChunk {
*/
private static final Logger LOGGER = Logger.getLogger(PngChunkIccp.class.getName());

/**
* Size of the ICC profile header and tag count: the smallest valid ICC profile.
*/
private static final int ICC_PROFILE_MIN_SIZE = 132;

/**
* Inflates an ICC profile, reading exactly the profile size declared in its header.
*
* @param compressedProfile the deflated profile.
* @return the profile, of the size declared in its header.
* @throws ImagingException if the declared size is smaller than an ICC profile header, or the profile data is shorter than its declared size.
* @throws IOException if the profile data cannot be inflated.
*/
private static byte[] inflateProfile(final byte[] compressedProfile) throws ImagingException, IOException {
try (InputStream in = new InflaterInputStream(new ByteArrayInputStream(compressedProfile))) {
// The first 4 bytes of an ICC profile give the size of the whole profile.
final byte[] header = IOUtils.toByteArray(in, ICC_PROFILE_MIN_SIZE);
final int profileSize = ByteConversions.toInt(header, ByteOrder.BIG_ENDIAN);
if (profileSize < ICC_PROFILE_MIN_SIZE) {
throw new ImagingException("PngChunkIccp: Invalid ICC profile size: " + profileSize);
}
Allocator.checkByteArray(profileSize);
// Reads incrementally, so memory use follows the data actually inflated rather than the declared size.
return IOUtils.toByteArray(new SequenceInputStream(new ByteArrayInputStream(header), in), profileSize, IOUtils.DEFAULT_BUFFER_SIZE);
} catch (final EOFException e) {
throw new ImagingException("PngChunkIccp: ICC profile is shorter than its declared size", e);
}
}

/**
* ICC profile name.
*/
Expand Down Expand Up @@ -93,7 +127,7 @@ public PngChunkIccp(final int length, final int chunkType, final int crc, final
LOGGER.finest("bytes.length: " + bytes.length);
}

uncompressedProfile = IOUtils.toByteArray(new InflaterInputStream(new ByteArrayInputStream(compressedProfile)));
uncompressedProfile = inflateProfile(compressedProfile);

if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("UncompressedProfile: " + bytes.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@
package org.apache.commons.imaging.formats.png.chunks;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.awt.color.ColorSpace;
import java.awt.color.ICC_Profile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
import java.util.zip.Deflater;
import java.util.Arrays;
import java.util.zip.DeflaterOutputStream;

import org.apache.commons.imaging.ImagingConstants;
import org.apache.commons.imaging.ImagingException;
import org.apache.commons.imaging.common.AllocationRequestException;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -39,6 +41,32 @@ class PngChunkIccpTest {

private static final int chunkType = 1766015824;

/**
* Builds the data of an iCCP chunk: a profile name, a null separator, the compression method and the deflated profile.
*/
private static byte[] chunkData(final byte[] profile) throws IOException {
final ByteArrayOutputStream data = new ByteArrayOutputStream();
data.write("my-profile-01".getBytes(StandardCharsets.ISO_8859_1));
data.write(0); // null separator
data.write(0); // 0=deflate compression method
try (DeflaterOutputStream deflater = new DeflaterOutputStream(data)) {
deflater.write(profile);
}
return data.toByteArray();
}

/**
* Builds dummy profile data of the given length, whose first 4 bytes declare the given size.
*/
private static byte[] profile(final int declaredSize, final int length) {
final byte[] profile = new byte[length];
for (int i = 4; i < length; i++) {
profile[i] = (byte) i;
}
ByteBuffer.wrap(profile).putInt(declaredSize);
return profile;
}

@Test
void testErrorOnNoProfileName() {
final byte[] data = ImagingConstants.EMPTY_BYTE_ARRAY;
Expand All @@ -47,37 +75,53 @@ void testErrorOnNoProfileName() {

@Test
void testParsingIccpChunk() throws ImagingException, IOException {
final List<Byte> bytes = new ArrayList<>();
final String profileName = "my-profile-01";
for (final byte b : profileName.getBytes(StandardCharsets.ISO_8859_1)) {
bytes.add(b);
}
bytes.add((byte) 0); // null
bytes.add((byte) 0); // 0=deflate compression method
final byte[] profile = profile(200, 200);
final byte[] data = chunkData(profile);
final PngChunkIccp chunk = new PngChunkIccp(data.length, chunkType, 0, data);
assertArrayEquals(profile, chunk.getUncompressedProfile());
}

// generate some 100 bytes of dummy data
final byte[] uncompressedData = new byte[100];
IntStream.range(0, 100).forEach(i -> {
uncompressedData[i] = (byte) (i + 1); // dummy data
});
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(100)) {
// compress the dummy data with deflate
final Deflater def = new Deflater();
try (DeflaterOutputStream ios = new DeflaterOutputStream(baos, def)) {
ios.write(uncompressedData);
}
baos.flush();
final byte[] compressedData = baos.toByteArray();
final byte[] data = new byte[bytes.size() + compressedData.length];
// gather everything, except for the compressed data
for (int i = 0; i < bytes.size(); ++i) {
data[i] = bytes.get(i).byteValue();
}
// gather the compressed data
IntStream.range(0, compressedData.length).forEach(i -> data[bytes.size() + i] = compressedData[i]);
// create the chunk
final PngChunkIccp chunk = new PngChunkIccp(data.length, chunkType, 0, data);
assertArrayEquals(uncompressedData, chunk.getUncompressedProfile());
}
/**
* Tests that only the declared profile size is inflated, even if the compressed data continues.
*/
@Test
void testProfileLongerThanDeclared() throws ImagingException, IOException {
final byte[] profile = profile(200, 10_000_000);
final byte[] data = chunkData(profile);
final PngChunkIccp chunk = new PngChunkIccp(data.length, chunkType, 0, data);
assertArrayEquals(Arrays.copyOf(profile, 200), chunk.getUncompressedProfile());
}

@Test
void testProfileShorterThanDeclared() throws IOException {
final byte[] data = chunkData(profile(1_000_000, 200));
assertThrows(ImagingException.class, () -> new PngChunkIccp(data.length, chunkType, 0, data));
}

@Test
void testProfileShorterThanHeader() throws IOException {
final byte[] data = chunkData(profile(100, 100));
assertThrows(ImagingException.class, () -> new PngChunkIccp(data.length, chunkType, 0, data));
}

@Test
void testProfileSizeExceedsAllocationLimit() throws IOException {
final byte[] data = chunkData(profile(Integer.MAX_VALUE, 200));
assertThrows(AllocationRequestException.class, () -> new PngChunkIccp(data.length, chunkType, 0, data));
}

@Test
void testProfileSizeSmallerThanHeader() throws IOException {
final byte[] data = chunkData(profile(100, 200));
assertThrows(ImagingException.class, () -> new PngChunkIccp(data.length, chunkType, 0, data));
}

@Test
void testRealProfile() throws ImagingException, IOException {
final byte[] profile = ICC_Profile.getInstance(ColorSpace.CS_sRGB).getData();
final byte[] data = chunkData(profile);
final PngChunkIccp chunk = new PngChunkIccp(data.length, chunkType, 0, data);
assertArrayEquals(profile, chunk.getUncompressedProfile());
assertEquals(ColorSpace.TYPE_RGB, ICC_Profile.getInstance(chunk.getUncompressedProfile()).getColorSpaceType());
}
}
Loading