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
2 changes: 2 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ 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-400" type="fix" dev="pkarwasz" due-to="Piotr P. Karwasz, Gary Gregory">Inflate PNG zTXt and iTXt text only up to the Allocator limit, now throws AllocationRequestException (#583).</action>
<!-- ADD -->
<action type="fix" dev="ggregory" due-to="Sean R, Gary Gregory">Add unit tests (#553).</action>
<action issue="IMAGING-400" type="add" dev="pkarwasz" due-to="Piotr P. Karwasz, Gary Gregory">Add Allocator.getLimit() (#583).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 83 to 107 #527, #530, #539, #544, #546, #550.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-lang3 from 3.17.0 to 3.22.0 #515.</action>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/apache/commons/imaging/common/Allocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,19 @@ public static float[] floatArray(final int request) {
return new float[check(request, Float.BYTES)];
}

/**
* Gets the allocation limit in bytes.
* <p>
* The default limit is {@code #DEFAULT}, override with the system property "org.apache.commons.imaging.common.Allocator".
* </p>
*
* @return The allocation limit in bytes.
* @since 1.0.0-alpha7
*/
public static int getLimit() {
return LIMIT;
}

/**
* Allocates a int array of the requested size.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,61 @@
*/
package org.apache.commons.imaging.formats.png.chunks;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.zip.InflaterInputStream;

import org.apache.commons.imaging.common.AllocationRequestException;
import org.apache.commons.imaging.common.Allocator;
import org.apache.commons.imaging.formats.png.AbstractPngText;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BoundedInputStream;

/**
* Abstract class for PNG text chunks.
*/
public abstract class AbstractPngTextChunk extends PngChunk {

/**
* Inflates compressed text, up to the {@link Allocator} limit.
*
* @param bytes the chunk data.
* @param offset the offset of the compressed text in the chunk data.
* @param charset the charset of the text.
* @return the inflated text.
* @throws AllocationRequestException if the inflated text is larger than the {@link Allocator} limit.
* @throws IOException if the text cannot be inflated.
*/
static String inflateText(final byte[] bytes, final int offset, final Charset charset) throws IOException {
return inflateText(bytes, offset, charset, Allocator.getLimit());
}

/**
* Inflates compressed text, up to the given limit.
*
* @param bytes the chunk data.
* @param offset the offset of the compressed text in the chunk data.
* @param charset the charset of the text.
* @param limit the maximum size of the inflated text, in bytes.
* @return the inflated text.
* @throws AllocationRequestException if the inflated text is larger than {@code limit}.
* @throws IOException if the text cannot be inflated.
*/
static String inflateText(final byte[] bytes, final int offset, final Charset charset, final int limit) throws IOException {
try (InputStream in = BoundedInputStream.builder()
.setInputStream(new InflaterInputStream(new ByteArrayInputStream(bytes, offset, bytes.length - offset)))
// Reading one byte past the limit means that the text is too large.
.setMaxCount(limit + 1L)
.setOnMaxCount((max, count) -> {
throw new AllocationRequestException(limit, count, null);
})
.get()) {
return IOUtils.toString(in, charset);
}
}

/**
* Constructs a new PNG text chunk.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@
*/
package org.apache.commons.imaging.formats.png.chunks;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.InflaterInputStream;

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.formats.png.AbstractPngText;
import org.apache.commons.imaging.formats.png.PngConstants;
import org.apache.commons.io.IOUtils;

/**
* PNG iTXt chunk with international textual data.
Expand Down Expand Up @@ -85,13 +81,7 @@ public PngChunkItxt(final int length, final int chunkType, final int crc, final
index = terminator + 1;

if (compressed) {
final int compressedTextLength = bytes.length - index;

final byte[] compressedText = Allocator.byteArray(compressedTextLength);
System.arraycopy(bytes, index, compressedText, 0, compressedTextLength);

text = new String(IOUtils.toByteArray(new InflaterInputStream(new ByteArrayInputStream(compressedText))), StandardCharsets.UTF_8);

text = inflateText(bytes, index, StandardCharsets.UTF_8);
} else {
text = new String(bytes, index, bytes.length - index, StandardCharsets.UTF_8);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@
*/
package org.apache.commons.imaging.formats.png.chunks;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.InflaterInputStream;

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.formats.png.AbstractPngText;
import org.apache.commons.imaging.formats.png.PngConstants;
import org.apache.commons.io.IOUtils;

/**
* PNG zTXt chunk with compressed textual data.
Expand Down Expand Up @@ -58,11 +54,7 @@ public PngChunkZtxt(final int length, final int chunkType, final int crc, final
throw new ImagingException("PNG zTXt chunk has unexpected compression method: " + compressionMethod);
}

final int compressedTextLength = bytes.length - index;
final byte[] compressedText = Allocator.byteArray(compressedTextLength);
System.arraycopy(bytes, index, compressedText, 0, compressedTextLength);

text = new String(IOUtils.toByteArray(new InflaterInputStream(new ByteArrayInputStream(compressedText))), StandardCharsets.ISO_8859_1);
text = inflateText(bytes, index, StandardCharsets.ISO_8859_1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.commons.imaging.common;

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

import org.junit.jupiter.api.Test;
Expand All @@ -34,4 +35,9 @@ void testArrayListOverflowIsRejected() {
void testArrayOverflowIsRejected() {
assertThrows(AllocationRequestException.class, () -> Allocator.array(107374183, Object[]::new, 40));
}

@Test
void testGetLimit() {
assertEquals(Integer.getInteger(Allocator.class.getCanonicalName(), 1_073_741_824), Allocator.getLimit());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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
*
* https://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.png.chunks;

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

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.DeflaterOutputStream;

import org.apache.commons.imaging.common.AllocationRequestException;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

/**
* Tests {@link AbstractPngTextChunk#inflateText(byte[], int, java.nio.charset.Charset, int)}.
*/
class AbstractPngTextChunkTest {

private static final byte[] PREFIX = { 'k', 0, 0 };

/**
* Builds chunk data: a 3-byte prefix followed by the deflated text.
*/
private static byte[] chunkData(final String text) throws IOException {
final ByteArrayOutputStream data = new ByteArrayOutputStream();
data.write(PREFIX);
try (DeflaterOutputStream deflater = new DeflaterOutputStream(data)) {
deflater.write(text.getBytes(StandardCharsets.ISO_8859_1));
}
return data.toByteArray();
}

@Test
void testInflateTextAboveLimit() throws IOException {
final byte[] data = chunkData(StringUtils.repeat('a', 1001));
assertThrows(AllocationRequestException.class, () -> AbstractPngTextChunk.inflateText(data, PREFIX.length, StandardCharsets.ISO_8859_1, 1000));
}

@Test
void testInflateTextAtLimit() throws IOException {
final String text = StringUtils.repeat('a', 1000);
assertEquals(text, AbstractPngTextChunk.inflateText(chunkData(text), PREFIX.length, StandardCharsets.ISO_8859_1, 1000));
}

/**
* Tests that inflation stops at the limit, however large the text would become.
*/
@Test
void testInflateTextFarAboveLimit() throws IOException {
final byte[] data = chunkData(StringUtils.repeat('a', 10_000_000));
assertThrows(AllocationRequestException.class, () -> AbstractPngTextChunk.inflateText(data, PREFIX.length, StandardCharsets.ISO_8859_1, 1000));
}

@Test
void testInflateTextUtf8() throws IOException {
final String text = "Zażółć gęślą jaźń";
final ByteArrayOutputStream data = new ByteArrayOutputStream();
data.write(PREFIX);
try (DeflaterOutputStream deflater = new DeflaterOutputStream(data)) {
deflater.write(text.getBytes(StandardCharsets.UTF_8));
}
assertEquals(text, AbstractPngTextChunk.inflateText(data.toByteArray(), PREFIX.length, StandardCharsets.UTF_8));
}
}
Loading