diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java index b3a80d94bf..ec86ee5f60 100644 --- a/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java @@ -261,6 +261,14 @@ protected List parseIptcBlock(final byte[] bytes) { // throw new ImageReadException( // "Unexpected record prefix in IPTC data."); + if (index + 3 > bytes.length) { + // record type and 2-byte size do not fit + if (LOGGER.isLoggable(Level.FINE)) { + LOGGER.fine("Truncated record header in IPTC data."); + } + return elements; + } + final int recordType = 0xff & bytes[index]; Debug.debug("recordType: " + recordType + " (0x" + Integer.toHexString(recordType) + ")"); index++; diff --git a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkScal.java b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkScal.java index 8f3ed7dda6..ca85579c8e 100644 --- a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkScal.java +++ b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkScal.java @@ -42,6 +42,9 @@ public final class PngChunkScal extends PngChunk { public PngChunkScal(final int length, final int chunkType, final int crc, final byte[] bytes) throws ImagingException { super(length, chunkType, crc, bytes); + if (bytes.length == 0) { + throw new ImagingException("PNG sCAL chunk is empty."); + } unitSpecifier = bytes[0]; if (getUnitSpecifier() != 1 && getUnitSpecifier() != 2) { throw new ImagingException("PNG sCAL invalid unit specifier: " + getUnitSpecifier()); diff --git a/src/main/java/org/apache/commons/imaging/formats/wbmp/WbmpImageParser.java b/src/main/java/org/apache/commons/imaging/formats/wbmp/WbmpImageParser.java index 501dca2a37..0839f4ea56 100644 --- a/src/main/java/org/apache/commons/imaging/formats/wbmp/WbmpImageParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/wbmp/WbmpImageParser.java @@ -145,7 +145,11 @@ public String getName() { private BufferedImage readImage(final WbmpHeader wbmpHeader, final InputStream is) throws IOException { final int rowLength = (wbmpHeader.width + 7) / 8; - final byte[] image = readBytes("Pixels", is, rowLength * wbmpHeader.height, "Error reading image pixels"); + final long imageLength = (long) rowLength * wbmpHeader.height; + if (imageLength > Integer.MAX_VALUE) { + throw new ImagingException("WBMP image too large: " + wbmpHeader.width + "x" + wbmpHeader.height); + } + final byte[] image = readBytes("Pixels", is, (int) imageLength, "Error reading image pixels"); final DataBufferByte dataBuffer = new DataBufferByte(image, image.length); final WritableRaster raster = Raster.createPackedRaster(dataBuffer, wbmpHeader.width, wbmpHeader.height, 1, null); final int[] palette = { 0x000000, 0xffffff }; diff --git a/src/main/java/org/apache/commons/imaging/formats/xpm/XpmImageParser.java b/src/main/java/org/apache/commons/imaging/formats/xpm/XpmImageParser.java index feb123f830..63c6390bd8 100644 --- a/src/main/java/org/apache/commons/imaging/formats/xpm/XpmImageParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/xpm/XpmImageParser.java @@ -302,11 +302,11 @@ private int parseColor(String color) throws ImagingException { private boolean parseNextString(final BasicCParser cParser, final StringBuilder stringBuilder) throws IOException, ImagingException { stringBuilder.setLength(0); String token = cParser.nextToken(); - if (token.charAt(0) != '"') { + if (token == null || token.charAt(0) != '"') { throw new ImagingException("Parsing XPM file failed, no string found where expected"); } BasicCParser.unescapeString(stringBuilder, token); - for (token = cParser.nextToken(); token.charAt(0) == '"'; token = cParser.nextToken()) { + for (token = cParser.nextToken(); token != null && token.charAt(0) == '"'; token = cParser.nextToken()) { BasicCParser.unescapeString(stringBuilder, token); } if (",".equals(token)) { diff --git a/src/test/java/org/apache/commons/imaging/formats/MalformedInputTest.java b/src/test/java/org/apache/commons/imaging/formats/MalformedInputTest.java new file mode 100644 index 0000000000..8276ebc5ab --- /dev/null +++ b/src/test/java/org/apache/commons/imaging/formats/MalformedInputTest.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.imaging.formats; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.Base64; + +import org.apache.commons.imaging.Imaging; +import org.apache.commons.imaging.ImagingException; +import org.apache.commons.imaging.bytesource.ByteSource; +import org.apache.commons.imaging.formats.wbmp.WbmpImageParser; +import org.apache.commons.imaging.formats.xpm.XpmImageParser; +import org.junit.jupiter.api.Test; + +/** + * Malformed inputs must be reported with {@link ImagingException}, not with unchecked exceptions. + */ +class MalformedInputTest { + + private static byte[] b64(final String s) { + return Base64.getDecoder().decode(s); + } + + @Test + void testWbmpHugeDimensions() { + // width 0x0fffffff, height 0x40: row length * height overflows int + final byte[] bytes = b64("AAD///9/QAAAAAAAAAAAAAAAAAAAAAA="); + assertThrows(ImagingException.class, () -> new WbmpImageParser().getBufferedImage(ByteSource.array(bytes), null)); + } + + @Test + void testPngEmptyScalChunk() { + // 1x1 PNG with a zero-length sCAL chunk + final byte[] bytes = b64("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAHNDQUy8GpAIAAAADElEQVR4nGPgEpEDAABoAD1UCKP3AAAAAElFTkSuQmCC"); + assertThrows(ImagingException.class, () -> Imaging.getImageInfo(bytes)); + } + + @Test + void testXpmEndsAfterArrayOpen() { + // "/* XPM */\nstatic char *x[] = {\n" and nothing else + final byte[] bytes = b64("LyogWFBNICovCnN0YXRpYyBjaGFyICp4W10gPSB7Cg=="); + assertThrows(ImagingException.class, () -> new XpmImageParser().getBufferedImage(ByteSource.array(bytes), null)); + } + + @Test + void testIptcTruncatedRecordHeader() { + // JPEG with an APP13 IPTC block that ends right after the record type / one size byte + for (final String s : new String[] { "/9j/7QAgUGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAMcAQUA/9k=", + "/9j/7QAgUGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAQcAQUA/9k=" }) { + // the truncated record is ignored + assertDoesNotThrow(() -> Imaging.getMetadata(b64(s))); + } + } +}