Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOLR-14457: Fix closing of malformed GZIPInputStream #283

Merged
merged 7 commits into from Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions solr/CHANGES.txt
Expand Up @@ -431,6 +431,8 @@ Bug Fixes

* SOLR-13209: Fix NPE when no grouping query specified (Collins Abanda via Mike Drob)

* SOLR-14457: SolrClient leaks connections on compressed responses if the response is malformed (Houston Putman)

Other Changes
---------------------
* SOLR-15566: Clarify ref guide documentation about SQL queries with `SELECT *` requiring a `LIMIT` clause (Timothy Potter)
Expand Down
Expand Up @@ -61,6 +61,7 @@
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.ObjectReleaseTracker;
import org.apache.solr.common.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -447,14 +448,39 @@ public void process(final HttpResponse response, final HttpContext context)
}
}

private static class GzipDecompressingEntity extends HttpEntityWrapper {
protected static class GzipDecompressingEntity extends HttpEntityWrapper {
private boolean gzipInputStreamCreated = false;
private InputStream gzipInputStream = null;

public GzipDecompressingEntity(final HttpEntity entity) {
super(entity);
}


/**
* Return a InputStream of uncompressed data.
* If there is an issue with the compression of the data, a null InputStream will be returned,
* and the underlying compressed InputStream will be closed.
*
* The same input stream will be returned if the underlying entity is not repeatable.
* If the underlying entity is repeatable, then a new input stream will be created.
HoustonPutman marked this conversation as resolved.
Show resolved Hide resolved
HoustonPutman marked this conversation as resolved.
Show resolved Hide resolved
*/
@Override
public InputStream getContent() throws IOException, IllegalStateException {
return new GZIPInputStream(wrappedEntity.getContent());
if (!gzipInputStreamCreated || wrappedEntity.isRepeatable()) {
gzipInputStreamCreated = true;
InputStream wrappedContent = wrappedEntity.getContent();
if (wrappedContent != null) {
try {
gzipInputStream = new GZIPInputStream(wrappedContent);
} catch (IOException ioException) {
try (wrappedContent) {
Utils.readFully(wrappedContent);
} catch (IOException ignored) {}
throw new IOException("Cannot open GZipInputStream for response", ioException);
}
}
}
return gzipInputStream;
}

@Override
Expand All @@ -468,7 +494,11 @@ private static class DeflateDecompressingEntity extends
public DeflateDecompressingEntity(final HttpEntity entity) {
super(entity);
}


/**
* InflaterInputStream does not throw a ZipException in the constructor,
* so it does not need the same checks as the GZIPInputStream.
*/
HoustonPutman marked this conversation as resolved.
Show resolved Hide resolved
@Override
public InputStream getContent() throws IOException, IllegalStateException {
return new InflaterInputStream(wrappedEntity.getContent());
Expand Down
2 changes: 1 addition & 1 deletion solr/solrj/src/java/org/apache/solr/common/util/Utils.java
Expand Up @@ -648,7 +648,7 @@ public static void consumeFully(HttpEntity entity) {
* @param is to read
* @throws IOException on problem with IO
*/
private static void readFully(InputStream is) throws IOException {
public static void readFully(InputStream is) throws IOException {
is.skip(is.available());
while (is.read() != -1) {
}
Expand Down
Expand Up @@ -17,8 +17,13 @@
package org.apache.solr.client.solrj.impl;

import javax.net.ssl.HostnameVerifier;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.http.HttpEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.solr.SolrTestCase;
import org.apache.solr.client.solrj.impl.HttpClientUtil.SocketFactoryRegistryProvider;

Expand Down Expand Up @@ -46,8 +51,7 @@ public void resetHttpClientBuilder() {
}

@Test
// commented out on: 24-Dec-2018 @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Sep-2018
public void testSSLSystemProperties() throws IOException {
public void testSSLSystemProperties() {

assertNotNull("HTTPS scheme could not be created using system defaults",
HttpClientUtil.getSocketFactoryRegistryProvider().getSocketFactoryRegistry().lookup("https"));
Expand Down Expand Up @@ -85,7 +89,6 @@ private void assertSSLHostnameVerifier(Class<? extends HostnameVerifier> expecte
}

@Test
// commented out on: 24-Dec-2018 @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Sep-2018
public void testToBooleanDefaultIfNull() throws Exception {
assertFalse(HttpClientUtil.toBooleanDefaultIfNull(Boolean.FALSE, true));
assertTrue(HttpClientUtil.toBooleanDefaultIfNull(Boolean.TRUE, false));
Expand All @@ -94,8 +97,7 @@ public void testToBooleanDefaultIfNull() throws Exception {
}

@Test
// commented out on: 24-Dec-2018 @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Sep-2018
public void testToBooleanObject() throws Exception {
public void testToBooleanObject() {
assertEquals(Boolean.TRUE, HttpClientUtil.toBooleanObject("true"));
assertEquals(Boolean.TRUE, HttpClientUtil.toBooleanObject("TRUE"));
assertEquals(Boolean.TRUE, HttpClientUtil.toBooleanObject("tRuE"));
Expand All @@ -109,4 +111,21 @@ public void testToBooleanObject() throws Exception {
assertEquals(null, HttpClientUtil.toBooleanObject("foo"));
assertEquals(null, HttpClientUtil.toBooleanObject(null));
}

@Test
public void testNonRepeatableMalformedGzipEntityAutoClosed() throws IOException {
HttpEntity baseEntity = new InputStreamEntity(new ByteArrayInputStream("this is not compressed".getBytes(StandardCharsets.UTF_8)));
HttpClientUtil.GzipDecompressingEntity gzipDecompressingEntity = new HttpClientUtil.GzipDecompressingEntity(baseEntity);
expectThrows(IOException.class, "An IOException wrapping a ZIPException should be thrown when loading a malformed GZIP Entity Content", gzipDecompressingEntity::getContent);
HoustonPutman marked this conversation as resolved.
Show resolved Hide resolved
assertNull("The second time getContent is called, null should be returned since the underlying entity is non-repeatable", gzipDecompressingEntity.getContent());
assertEquals("No more content should be available after the GZIP Entity failed to load", 0, baseEntity.getContent().available());
}

@Test
public void testRepeatableMalformedGzipEntity() throws IOException {
HttpEntity baseEntity = new StringEntity("this is not compressed");
HttpClientUtil.GzipDecompressingEntity gzipDecompressingEntity = new HttpClientUtil.GzipDecompressingEntity(baseEntity);
expectThrows(IOException.class, "An IOException wrapping a ZIPException should be thrown when loading a malformed GZIP Entity Content", gzipDecompressingEntity::getContent);
expectThrows(IOException.class, "An IOException should be thrown again when re-loading a repeatable malformed GZIP Entity Content", gzipDecompressingEntity::getContent);
}
}