This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/CXF-9274 in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 17a438d0eb2c8aaa8df4abfe834ceabe1f5d0175 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Fri Nov 13 07:15:15 2026 +0000 Limit the decompressed size of gzip-encoded messages --- .../transport/common/gzip/GZIPInInterceptor.java | 82 +++++++++++++- .../cxf/transport/common/gzip/Messages.properties | 1 + .../common/gzip/GZIPInInterceptorTest.java | 119 +++++++++++++++++++++ 3 files changed, 201 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..f4e4446f2c7 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,71 @@ 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. + */ + private static final class SizeLimitedInputStream extends FilterInputStream { + private final long maxSize; + private long count; + + SizeLimitedInputStream(InputStream in, long maxSize) { + super(in); + this.maxSize = maxSize; + } + + @Override + public int read() throws IOException { + int b = super.read(); + if (b != -1) { + checkSize(1); + } + return b; + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + int n = super.read(b, off, len); + if (n > 0) { + checkSize(n); + } + return n; + } + + @Override + public long skip(long n) throws IOException { + 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) { + 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..d14afe69286 --- /dev/null +++ b/core/src/test/java/org/apache/cxf/transport/common/gzip/GZIPInInterceptorTest.java @@ -0,0 +1,119 @@ +/** + * 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.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; + +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 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) { + 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, new ByteArrayInputStream(body)); + return message; + } +}
