This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/main by this push:
new 250211b019d CXF-9274 - Limit the decompressed size of gzip-encoded
messages (#3715)
250211b019d is described below
commit 250211b019dbb0c836f5986ac849dbc3506d7329
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Sat Nov 14 18:48:07 2026 +0000
CXF-9274 - Limit the decompressed size of gzip-encoded messages (#3715)
* CXF-9274 - Limit the decompressed size of gzip-encoded messages
* Close the underlying stream as soon as the limit is exceeded
---
.../transport/common/gzip/GZIPInInterceptor.java | 100 +++++++++++++-
.../cxf/transport/common/gzip/Messages.properties | 1 +
.../common/gzip/GZIPInInterceptorTest.java | 148 +++++++++++++++++++++
3 files changed, 248 insertions(+), 1 deletion(-)
diff --git
a/core/src/main/java/org/apache/cxf/transport/common/gzip/GZIPInInterceptor.java
b/core/src/main/java/org/apache/cxf/transport/common/gzip/GZIPInInterceptor.java
index b2505a3bfcc..f0a4b1b8544 100644
---
a/core/src/main/java/org/apache/cxf/transport/common/gzip/GZIPInInterceptor.java
+++
b/core/src/main/java/org/apache/cxf/transport/common/gzip/GZIPInInterceptor.java
@@ -18,6 +18,7 @@
*/
package org.apache.cxf.transport.common.gzip;
+import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@@ -28,6 +29,7 @@ import java.util.zip.GZIPInputStream;
import org.apache.cxf.common.i18n.BundleUtils;
import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.util.SystemPropertyAction;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.helpers.HttpHeaderHelper;
@@ -50,6 +52,13 @@ import org.apache.cxf.phase.Phase;
*/
public class GZIPInInterceptor extends AbstractPhaseInterceptor<Message> {
+ /**
+ * The maximum size in bytes that a gzip-encoded message may decompress
to. A negative value means
+ * no limit. The default is 100 MB.
+ */
+ public static final String GZIP_MAX_DECOMPRESSED_SIZE =
"gzip-max-decompressed-size";
+ public static final long DEFAULT_GZIP_MAX_DECOMPRESSED_SIZE =
+
SystemPropertyAction.getInteger("org.apache.cxf.gzip-max-decompressed-size",
100 * 1024 * 1024);
private static final ResourceBundle BUNDLE =
BundleUtils.getBundle(GZIPInInterceptor.class);
private static final Logger LOG =
LogUtils.getL7dLogger(GZIPInInterceptor.class);
@@ -88,7 +97,11 @@ public class GZIPInInterceptor extends
AbstractPhaseInterceptor<Message> {
}
// wrap an unzipping stream around the original one
- GZIPInputStream zipInput = new GZIPInputStream(is);
+ InputStream zipInput = new GZIPInputStream(is);
+ long maxSize = getMaxDecompressedSize(message);
+ if (maxSize >= 0) {
+ zipInput = new SizeLimitedInputStream(zipInput,
maxSize);
+ }
message.setContent(InputStream.class, zipInput);
// remove content encoding header as we've now dealt with
it
@@ -112,4 +125,89 @@ public class GZIPInInterceptor extends
AbstractPhaseInterceptor<Message> {
}
}
+ private static long getMaxDecompressedSize(Message message) {
+ Object maxSize =
message.getContextualProperty(GZIP_MAX_DECOMPRESSED_SIZE);
+ if (maxSize instanceof Number) {
+ return ((Number)maxSize).longValue();
+ } else if (maxSize instanceof String) {
+ try {
+ return Long.parseLong(((String)maxSize).trim());
+ } catch (NumberFormatException ex) {
+ LOG.warning("Invalid value for " + GZIP_MAX_DECOMPRESSED_SIZE
+ ": " + maxSize);
+ }
+ }
+ return DEFAULT_GZIP_MAX_DECOMPRESSED_SIZE;
+ }
+
+ /**
+ * Throws an IOException once more than the given number of bytes has been
read, to guard against
+ * a small compressed message that decompresses to a very large one. The
underlying stream is closed
+ * at that point, and any further read throws the same exception.
+ */
+ private static final class SizeLimitedInputStream extends
FilterInputStream {
+ private final long maxSize;
+ private long count;
+ private boolean exceeded;
+
+ SizeLimitedInputStream(InputStream in, long maxSize) {
+ super(in);
+ this.maxSize = maxSize;
+ }
+
+ @Override
+ public int read() throws IOException {
+ checkNotExceeded();
+ int b = super.read();
+ if (b != -1) {
+ checkSize(1);
+ }
+ return b;
+ }
+
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException {
+ checkNotExceeded();
+ int n = super.read(b, off, len);
+ if (n > 0) {
+ checkSize(n);
+ }
+ return n;
+ }
+
+ @Override
+ public long skip(long n) throws IOException {
+ checkNotExceeded();
+ long skipped = super.skip(n);
+ if (skipped > 0) {
+ checkSize(skipped);
+ }
+ return skipped;
+ }
+
+ @Override
+ public boolean markSupported() {
+ return false;
+ }
+
+ private void checkSize(long n) throws IOException {
+ count += n;
+ if (count > maxSize) {
+ exceeded = true;
+ // the stream can't be used any further, so close it to
release the Inflater straight away
+ try {
+ in.close();
+ } catch (IOException ex) {
+ // ignore, the size limit error is the one to report
+ }
+ checkNotExceeded();
+ }
+ }
+
+ private void checkNotExceeded() throws IOException {
+ if (exceeded) {
+ throw new IOException(new
org.apache.cxf.common.i18n.Message("GZIP_MAX_SIZE_EXCEEDED",
+
BUNDLE, maxSize).toString());
+ }
+ }
+ }
}
diff --git
a/core/src/main/java/org/apache/cxf/transport/common/gzip/Messages.properties
b/core/src/main/java/org/apache/cxf/transport/common/gzip/Messages.properties
index 14d3735c0f6..52631adab5b 100644
---
a/core/src/main/java/org/apache/cxf/transport/common/gzip/Messages.properties
+++
b/core/src/main/java/org/apache/cxf/transport/common/gzip/Messages.properties
@@ -19,5 +19,6 @@
#
#
COULD_NOT_UNZIP=Could not unzip compressed message.
+GZIP_MAX_SIZE_EXCEEDED=The decompressed message exceeds the maximum allowed
size of {0} bytes.
COULD_NOT_ZIP=Could not compress message.
NO_SUPPORTED_ENCODING=No supported content encodings available.
diff --git
a/core/src/test/java/org/apache/cxf/transport/common/gzip/GZIPInInterceptorTest.java
b/core/src/test/java/org/apache/cxf/transport/common/gzip/GZIPInInterceptorTest.java
new file mode 100644
index 00000000000..1a0473e7678
--- /dev/null
+++
b/core/src/test/java/org/apache/cxf/transport/common/gzip/GZIPInInterceptorTest.java
@@ -0,0 +1,148 @@
+/**
+ * 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.cxf.transport.common.gzip;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.zip.GZIPOutputStream;
+
+import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+public class GZIPInInterceptorTest {
+
+ @Test
+ public void testDecompress() throws Exception {
+ byte[] data = "<hello>world</hello>".getBytes();
+ Message message = createMessage(gzip(data));
+ new GZIPInInterceptor().handleMessage(message);
+ assertArrayEquals(data,
IOUtils.readBytesFromStream(message.getContent(InputStream.class)));
+ }
+
+ @Test
+ public void testDecompressedSizeAtLimit() throws Exception {
+ byte[] data = new byte[1024 * 1024];
+ Message message = createMessage(gzip(data));
+ message.put(GZIPInInterceptor.GZIP_MAX_DECOMPRESSED_SIZE, data.length);
+ new GZIPInInterceptor().handleMessage(message);
+ assertEquals(data.length,
IOUtils.readBytesFromStream(message.getContent(InputStream.class)).length);
+ }
+
+ @Test
+ public void testDecompressedSizeExceeded() throws Exception {
+ // 10 MB of zeros compresses to about 10 KB
+ Message message = createMessage(gzip(new byte[10 * 1024 * 1024]));
+ message.put(GZIPInInterceptor.GZIP_MAX_DECOMPRESSED_SIZE, "1048576");
+ new GZIPInInterceptor().handleMessage(message);
+ InputStream in = message.getContent(InputStream.class);
+ assertThrows(IOException.class, () -> IOUtils.readBytesFromStream(in));
+ }
+
+ @Test
+ public void testStreamClosedWhenSizeExceeded() throws Exception {
+ AtomicBoolean closed = new AtomicBoolean();
+ InputStream body = new ByteArrayInputStream(gzip(new byte[10 * 1024 *
1024])) {
+ @Override
+ public void close() throws IOException {
+ closed.set(true);
+ super.close();
+ }
+ };
+ Message message = createMessage(body);
+ message.put(GZIPInInterceptor.GZIP_MAX_DECOMPRESSED_SIZE, 1024L *
1024);
+ new GZIPInInterceptor().handleMessage(message);
+ InputStream in = message.getContent(InputStream.class);
+
+ IOException ex = assertThrows(IOException.class, () ->
IOUtils.readBytesFromStream(in));
+ assertTrue(closed.get());
+ // any further read reports the same error rather than reading from
the closed stream
+ assertEquals(ex.getMessage(), assertThrows(IOException.class, () ->
in.read()).getMessage());
+ assertEquals(ex.getMessage(), assertThrows(IOException.class, () ->
in.read(new byte[16])).getMessage());
+ assertEquals(ex.getMessage(), assertThrows(IOException.class, () ->
in.skip(16)).getMessage());
+ }
+
+ @Test
+ public void testDecompressedSizeExceededSingleByteReads() throws Exception
{
+ Message message = createMessage(gzip(new byte[2048]));
+ message.put(GZIPInInterceptor.GZIP_MAX_DECOMPRESSED_SIZE, 1024L);
+ new GZIPInInterceptor().handleMessage(message);
+ InputStream in = message.getContent(InputStream.class);
+ assertThrows(IOException.class, () -> {
+ while (in.read() != -1) {
+ // keep reading
+ }
+ });
+ }
+
+ @Test
+ public void testDecompressedSizeUnlimited() throws Exception {
+ byte[] data = new byte[2 * 1024 * 1024];
+ Message message = createMessage(gzip(data));
+ message.put(GZIPInInterceptor.GZIP_MAX_DECOMPRESSED_SIZE, -1);
+ new GZIPInInterceptor().handleMessage(message);
+ assertEquals(data.length,
IOUtils.readBytesFromStream(message.getContent(InputStream.class)).length);
+ }
+
+ @Test
+ public void testDefaultMaxDecompressedSize() {
+ assertEquals(100L * 1024 * 1024,
GZIPInInterceptor.DEFAULT_GZIP_MAX_DECOMPRESSED_SIZE);
+ }
+
+ private static byte[] gzip(byte[] data) throws IOException {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ try (GZIPOutputStream out = new GZIPOutputStream(bos)) {
+ out.write(data);
+ }
+ return bos.toByteArray();
+ }
+
+ private static Message createMessage(byte[] body) {
+ return createMessage(new ByteArrayInputStream(body));
+ }
+
+ private static Message createMessage(InputStream body) {
+ Message message = new MessageImpl();
+ Exchange exchange = new ExchangeImpl();
+ exchange.setInMessage(message);
+ message.setExchange(exchange);
+ message.put(Message.HTTP_REQUEST_METHOD, "POST");
+ Map<String, List<String>> headers = new
TreeMap<>(String.CASE_INSENSITIVE_ORDER);
+ headers.put("Content-Encoding", Collections.singletonList("gzip"));
+ message.put(Message.PROTOCOL_HEADERS, headers);
+ message.setContent(InputStream.class, body);
+ return message;
+ }
+}