Repository navigation
[#385] Decode HTTP deflate bodies in zlib format - #386
Conversation
maximthomas
left a comment
There was a problem hiding this comment.
praise: The zlib fix sits where #385's failure is, and it is careful about the error paths around it.
- A zlib body is never retried as raw (
DeflateDecoder.java:59). A corrupt body therefore still reportsincorrect data checkorUnexpected end of ZLIB input stream, not a misleading raw-inflate error. - The
close()override (DeflateDecoder.java:61-69) ends the caller-suppliedInflater, whichInflaterInputStream.close()leaves allocated. - The 32 new cases ran on every finished CI row: http-framework core went from 1059 tests at the base to 1091 on ubuntu 11/17/21/25/27, windows 11/27 and macos 27, with 0 failures.
question (non-blocking): Is it intended that decode() now reads the header eagerly, so an empty or 1-byte deflate body throws from Entity.newDecodedContentInputStream() / newDecodedContentReader() itself?
commons/http-framework/core/src/main/java/org/forgerock/http/decoder/internal/DeflateDecoder.java:45-52
At the base, decode() made no reads, and the EOFException came on the first read(). At this head, decode() reads two bytes and throws EOFException("Truncated deflate header"). This affects a message that carries Content-Encoding: deflate with no body (HEAD/204/304). The in-repo readers (getString/getBytes/getJson) already failed on such a body. A caller that opens the stream and handles a failed read is affected, though. OpenIG's EntityExtractFilter (openig-core, :129-136) used to map every pattern key to null; it now sets an empty map. No caller was found that opens the stream before the body arrives, so the two-byte wait only moves from read() into decode(). Either answer keeps this minor:
- Not intended: sniff the header on the first
read/skip/availableinstead. That also removes the new path in the next comment. - Intended: pin it. Today
rejectsMalformedBodies(DeflateDecoderTest.java:136-141) wrapsdecode()andreadAllBytes()in onetry, so a lazy refactor would still pass.
@Test
public void reportsATruncatedHeaderWhenDecodingStarts() {
assertThatThrownBy(() -> new DeflateDecoder().decode(new ByteArrayInputStream(new byte[] { 0x78 })))
.isInstanceOf(EOFException.class)
.hasMessage("Truncated deflate header");
}issue (non-blocking): With Content-Encoding: deflate, gzip, a failing DeflateDecoder.decode() orphans the GZIPInputStream that is already built, so its Inflater is freed only by GC.
commons/http-framework/core/src/main/java/org/forgerock/http/header/ContentEncodingHeader.java:98-109, commons/http-framework/core/src/main/java/org/forgerock/http/protocol/Entity.java:297-305
The loop decodes in reverse order and closes nothing when a decoder throws. Entity closes only headBranch. A gzip payload shorter than 2 bytes, or one with a zlib header that has FDICT set, now throws inside decode(). At the base, the stream was returned and the caller's close() ended the gzip Inflater. The leak is bounded (one per bad request, until GC). The same orphaning already existed for gzip, gzip and for an unsupported coding listed before gzip. A fix in the loop covers all three cases; it can go in a follow-up:
// import static org.forgerock.util.Utils.closeSilently;
public InputStream decode(InputStream in) throws IOException {
InputStream decoded = in;
try {
// decode in the reverse order that encoding was applied
for (ListIterator<String> i = codings.listIterator(codings.size()); i.hasPrevious();) {
String name = i.previous();
Decoder decoder = Decoder.SERVICES.get(name);
if (decoder == null) {
throw new UnsupportedEncodingException(name);
}
decoded = decoder.decode(decoded);
}
return decoded;
} catch (IOException | RuntimeException e) {
if (decoded != in) {
closeSilently(decoded); // ends the decoders built so far, and closes in
}
throw e;
}
}On failure, this also closes in. That is harmless for Entity.java:486, the only caller in this repo, because BranchingStreamWrapper.close() tolerates a second close (:163). For external callers of this public method, though, it is a contract change. The lazy sniff from the previous comment is the alternative, and it covers only the deflate path.
suggestion (non-blocking): No test pins that closing the decoded stream ends the Inflater.
commons/http-framework/core/src/test/java/org/forgerock/http/decoder/internal/DeflateDecoderTest.java:154, commons/http-framework/core/src/main/java/org/forgerock/http/decoder/internal/DeflateDecoder.java:61-69
closingDecodedStreamClosesSource asserts only that the source was closed, and InflaterInputStream.close() already does that. If the override, or just its inf.end(), is deleted, all 32 cases stay green. inf is protected, and the surefire argLine (pom.xml:1507) does not open java.util.zip, so a test needs a package-private seam:
// DeflateDecoder.decode(): return new InflaterReleasingInputStream(input, new Inflater(!zlib));
/** Ends the explicitly supplied Inflater on close, which InflaterInputStream does not do. */
static final class InflaterReleasingInputStream extends InflaterInputStream {
InflaterReleasingInputStream(InputStream in, Inflater inflater) {
super(in, inflater);
}
Inflater inflater() {
return inf;
}
@Override
public void close() throws IOException {
try {
super.close();
} finally {
inf.end();
}
}
}
// DeflateDecoderTest
@Test
public void closingDecodedStreamEndsTheInflater() throws IOException {
DeflateDecoder.InflaterReleasingInputStream decoded = (DeflateDecoder.InflaterReleasingInputStream)
new DeflateDecoder().decode(new ByteArrayInputStream(
compress(CONTENT, false, Deflater.DEFAULT_COMPRESSION)));
Inflater inflater = decoded.inflater();
decoded.close();
// NullPointerException on JDK 11-21, IllegalStateException on JDK 25-26
assertThatThrownBy(inflater::getBytesRead).hasMessage("Inflater has been closed");
}Pin: without inf.end(), getBytesRead() returns 0 and the assertion fails. The message is the same on JDK 11.0.25, 17.0.11, 21.0.2, 25.0.2 and 26 (macOS). JDK 27 was not checked. getRemaining() and finished() do not throw after end(), so they cannot pin this.
suggestion (non-blocking): The malformed-body rows accept any IOException, so the "no raw retry of corrupt zlib" behaviour is not pinned.
commons/http-framework/core/src/test/java/org/forgerock/http/decoder/internal/DeflateDecoderTest.java:123-141
All 7 malformedBodies rows, and the preset-dictionary body, also throw at the base. Suppose a variant inflates as zlib first and retries as raw. It turns badChecksum and the truncated zlib row into ZipException: invalid stored block lengths, and it still passes every case.
@Test
public void reportsZlibChecksumAndTruncationErrorsWithoutRawRetry() throws IOException {
byte[] zlib = compress(CONTENT, false, Deflater.DEFAULT_COMPRESSION);
byte[] badChecksum = zlib.clone();
badChecksum[badChecksum.length - 1] ^= 1;
assertThatThrownBy(() -> decodeFully(badChecksum))
.isInstanceOf(ZipException.class)
.hasMessage("incorrect data check");
assertThatThrownBy(() -> decodeFully(Arrays.copyOf(zlib, zlib.length - 1)))
.isInstanceOf(EOFException.class)
.hasMessage("Unexpected end of ZLIB input stream");
}
private static byte[] decodeFully(byte[] compressed) throws IOException {
try (InputStream decoded = new DeflateDecoder().decode(new ByteArrayInputStream(compressed))) {
return decoded.readAllBytes();
}
}Both messages were checked on macOS JDK 11-26. The Linux/Windows rows, JDK 27 and system-zlib builds were not checked.
suggestion (non-blocking): No case pins the CINFO <= 7 and FCHECK terms that keep raw streams on the raw path.
commons/http-framework/core/src/main/java/org/forgerock/http/decoder/internal/DeflateDecoder.java:55, commons/http-framework/core/src/test/java/org/forgerock/http/decoder/internal/DeflateDecoderTest.java:43-58
Every raw row starts with 73, 01 or 03, so (cmf & 0x0f) == 8 alone routes all 32 cases the same way. Reducing zlib to that test, or dropping either term on its own, survives 32/32. The rows below are raw streams whose first byte has compression method 8, built from a non-final stored block with non-zero padding:
@DataProvider
public Object[][] rawStreamsWithNonZeroStoredBlockPadding() {
return new Object[][] {
{ (byte) 0x08, "hello".getBytes(UTF_8) }, // header 08 05: CM 8, CINFO 0, FCHECK fails
{ (byte) 0x88, Arrays.copyOf(CONTENT, 28) } // header 88 1c: FCHECK passes, CINFO 8
};
}
@Test(dataProvider = "rawStreamsWithNonZeroStoredBlockPadding")
public void decodesRawStreamsThatOnlyResembleZlib(byte first, byte[] content) throws IOException {
ByteArrayOutputStream raw = new ByteArrayOutputStream();
raw.write(first); // non-final stored block, padding bits not all zero
raw.write(content.length); // LEN, little-endian
raw.write(0);
raw.write(~content.length); // NLEN
raw.write(0xff);
raw.write(content);
raw.write(new byte[] { 0x03, 0x00 }); // final empty fixed-Huffman block
try (InputStream decoded = new DeflateDecoder().decode(new ByteArrayInputStream(raw.toByteArray()))) {
assertThat(decoded.readAllBytes()).isEqualTo(content);
}
}Pin: the head decodes both rows as raw. Each mutation sends one row to Inflater(false), and that row fails:
- Dropping FCHECK sends
08 05there:incorrect header check. - Dropping CINFO sends
88 1cthere:invalid window size.
This was checked on macOS JDK 11-26.
|
@maximthomas Addressed all five points in 00b8e02.
Validation: |
maximthomas
left a comment
There was a problem hiding this comment.
praise: Both round-2 changes are pinned by the new tests, and CI agrees so far.
- Making
DeflateDecoder.decodeeager again fails 10 of the 82 cases inDeflateDecoderTest+ContentEncodingHeaderTest(6 on the lazy contract, 4 inclosingDecodedStreamEndsInflater). - With
ContentEncodingHeader.javaput back to its base version, exactly the 9 chain-cleanup cases the description names fail (cleanupPreservesOriginalFailurex4,failedDecodeClosesConstructedWrappersx5). - http-framework core runs 1117 cases with 0 failures on build-maven macos-latest 27, windows-latest 27 and ubuntu-latest 21.
issue (non-blocking): DeflateDecoder only remembers an IOException from header detection. After a RuntimeException, the next read sniffs the header again from a shifted offset.
commons/http-framework/core/src/main/java/org/forgerock/http/decoder/internal/DeflateDecoder.java:62-71
decodeStream reads CMF and FLG through a local PushbackInputStream, but only catch (IOException e) sets failure. Take a source that returns the first header byte and then throws UncheckedIOException, and a caller that reads again. The retry sniffs from the second byte and throws ZipException ("invalid stored block lengths" for the raw body cb 48 cd c9 c9 07 00, "invalid code lengths set" for zlib). The base decodes "hello". The description says this cannot happen ("later reads cannot reinterpret input from a different offset"). It needs a caller-built or container source: no in-repo source throws unchecked from read(). Probed on JDK 11.0.25 and 26.
} catch (IOException e) {
// The header may already have been consumed. Never retry at a new offset.
failure = e;
throw e;
} catch (RuntimeException e) {
failure = new IOException("Deflate header detection failed", e);
throw e;
}With this catch, the same probe gets the cached IOException on the retry (compiled and run on JDK 26 only).
question (non-blocking): If Entity.copyDecodedContentTo fails on an unpushed streaming entity, should it close the entity's content and every outstanding branch?
commons/http-framework/core/src/main/java/org/forgerock/http/header/ContentEncodingHeader.java:117-127, commons/http-framework/core/src/main/java/org/forgerock/http/protocol/Entity.java:161, :178
The new cleanup closes in. copyDecodedContentTo passes the entity's own head, and head is the trunk unless the caller pushed. Example: a branching trunk with Content-Encoding: unsupported, gzip (or gzip, deflate, or gzip, gzip with a bad inner payload). The failed copy now closes the trunk, so copyRawContentTo, held branches and push() throw "stream is closed", and isDecodedContentEmpty() returns true. The base left them readable. getBytes()/getString() push first, and OpenIG's MessageCapture is pushed, so no known caller is affected. If this is intended, one sentence in copyDecodedContentTo's Javadoc covers it. If not, it is a minor issue, or a major one if callers rely on the entity staying readable after a failed decoded copy.
suggestion (non-blocking): No raw test body has bit 0x20 set in its second byte, so nothing pins the zlib && in the FDICT guard.
commons/http-framework/core/src/main/java/org/forgerock/http/decoder/internal/DeflateDecoder.java:127
The mutant if ((flg & 0x20) != 0) would reject about half of legacy raw bodies with "Preset dictionaries are not supported", and both test classes stay green (82 run, 0 failures). The second bytes of the raw vectors are 0x54, 0x1d, 0x00, 0x05 and 0x1c, so the bit is never set.
{ 0x88, Arrays.copyOf(CONTENT, 28) }, // CM 8 and valid FCHECK, but CINFO 8.
{ 0x00, Arrays.copyOf(CONTENT, 62) } // CINFO 0, valid FCHECK and FDICT set, but CM 0.Pin: the row above in rawStreamsWithNonZeroStoredBlockPadding. The head decodes it; the mutant throws ZipException, and so does a mutant without the CM term. This was probed with the test's byte layout on JDK 26; the row itself was not run. Its padding is zero, so either rename the provider or give the row its own provider.
suggestion (non-blocking): No test vector has CM ≠ 8 with CINFO ≤ 7 and a valid FCHECK, so nothing pins the (cmf & 0x0f) == 8 term.
commons/http-framework/core/src/main/java/org/forgerock/http/decoder/internal/DeflateDecoder.java:125
Without the CM term, about 1 in 31 raw bodies go to Inflater(false) and fail with "unknown compression method", and the suite stays green (82 run, 0 failures). The raw headers in the tests fail FCHECK, and both padding rows and every zlib vector have CM 8.
{ 0x88, Arrays.copyOf(CONTENT, 28) }, // CM 8 and valid FCHECK, but CINFO 8.
{ 0x00, Arrays.copyOf(CONTENT, 31) } // CINFO 0 and valid FCHECK, but CM 0.Pin: the head decodes this row; the CM-less mutant throws ZipException "unknown compression method" (probed on JDK 26). The 62 row above kills this mutant too.
suggestion (non-blocking): No test has the source's close() rethrow the exception its read() threw, so nothing pins the closeFailure != e guard.
commons/http-framework/core/src/main/java/org/forgerock/http/header/ContentEncodingHeader.java:122, commons/http-framework/core/src/test/java/org/forgerock/http/header/ContentEncodingHeaderTest.java:209-246
The guard is reachable through the public decode(): use "gzip, gzip" over a source that throws one cached IOException from read() (after the 10 header bytes) and the same instance from close(). Without the guard, addSuppressed throws IllegalArgumentException: Self-suppression not permitted and the original failure is lost. All four cleanupPreservesOriginalFailure rows use distinct instances.
Pin: a variant of cleanupPreservesOriginalFailure that throws the same IOException from read and close and asserts isSameAs(failure), hasNoSuppressedExceptions() and source.closes == 1. A mutant with if (true) in place of the guard throws IllegalArgumentException there.
|
@maximthomas Addressed all five points in 8cd8e49.
Validation:
The PR description now reflects the final contracts and validation. |
maximthomas
left a comment
There was a problem hiding this comment.
praise: The round-3 changes close the gaps from round 2, and the PR's measurements reproduce.
- The new
catch (RuntimeException e)inDeflateDecoder.decode(DeflateDecoder.java:70) stops the re-sniff after an unchecked header failure. Removing it fails exactly the fouruncheckedHeaderFailureCannotBeBypassedByReadingAgaincases. - The stated mutation counts reproduce exactly on JDK 26: FDICT gating 1, CM term 2, self-suppression guard 2, chain cleanup 15.
DeflateDecoderTest,ContentEncodingHeaderTestandEntityTestpass 120/120 on JDK 11.0.25, 17.0.11, 21.0.2, 25.0.2 and 26 (macOS).
question (non-blocking): Should a java.lang.Error from the source bypass the header-detection cache?
commons/http-framework/core/src/main/java/org/forgerock/http/decoder/internal/DeflateDecoder.java:62-74
decoded() caches an IOException and a RuntimeException. An Error thrown by the source after CMF was read escapes both arms, so failure and decoded both stay null. If the caller catches the Error and reads again, the header is sniffed from offset 1. Take a source whose second read() throws an AssertionError once: raw deflate cb 48 cd c9 c9 07 00 decodes "hello" on the retry at BASE, but throws ZipException: invalid stored block lengths at HEAD. A zlib body throws invalid code lengths set instead of the original failure. This is Minor either way, because it needs a source that throws an Error and a caller that retries after it. If the gap is not intended, the multi-catch below makes every retry throw the cached IOException (probed on JDK 11.0.25 and 26). The description's "a RuntimeException is thrown unchanged" then becomes "a RuntimeException or Error".
} catch (RuntimeException | Error e) {
failure = new IOException("Deflate header detection failed", e);
throw e;
}Pin: a DeflateDecoderTest case whose source throws an AssertionError on its second read(). The case catches it, reads again, and asserts the cached IOException (with the fix) or the re-sniff (if the gap is intended). Today only RuntimeException retries are tested (DeflateDecoderTest.java:240-243).
nitpick (non-blocking): The new copyDecodedContentTo Javadoc says the entity's content and its branches are closed, but in-memory content is never closed.
commons/http-framework/core/src/main/java/org/forgerock/http/protocol/Entity.java:155-159, :177-181
Content set with setBytes()/setString() (Entity.java:397 → IO.java:92) and the shared empty stream are ByteArrayBranchingStreams. Their close() is empty (ByteArrayBranchingStream.java:121-122), so after a failed decode the content and its branches stay readable. The text errs in the safe direction, but it is wrong for those entities. For both blocks:
* If constructing the decoder chain fails after a wrapper has been created,
* the current stream is closed. Without a preceding {@link #push()}, this
* can close the entity's content and its child branches (content set with
* {@link #setBytes(byte[])} or {@link #setString(String)} stays readable);
* call {@code push()} before copying and {@link #pop()} in a {@code finally}
* block to preserve the previous state on failure.|
@maximthomas Addressed both points in 74eedb7.
Validation: The PR description now reflects the final behavior and validation. Requesting another review. |
maximthomas
left a comment
There was a problem hiding this comment.
praise: The round-3 gap is closed where it was opened, and the new test proves it.
DeflateDecoder.java:70now catchesRuntimeException | Error, so anErrorthrown during header detection is cached and later reads never re-sniff from a shifted offset.errorHeaderFailureCannotBeBypassedByReadingAgainpins exception identity acrossread(),read(byte[])andskip(), and the source read count (reads[0] == 2). With| Errorremoved, both rows fail.DeflateDecoderTestpasses 52/52 on macOS with JDK 11.0.25, 17.0.11, 21.0.2, 25.0.2 and Zulu 26.
Standard HTTP
Content-Encoding: deflatebodies fail withZipException: invalid stored block lengthsbecause the decoder expects raw DEFLATE. Decode the zlib format required by RFC 9110 while retaining compatibility with raw streams whose first two bytes are not a valid zlib header.Header detection is deferred until the first non-empty read or positive skip. Opening or closing an unread stream, querying
available(), and zero-length operations do not read the source. Empty and one-byte bodies therefore report their errors during reading, including through the entity stream and reader APIs. Header-detection failures are remembered so later reads cannot reinterpret input from a different offset. AnIOExceptionis retained directly; aRuntimeExceptionorErroris thrown unchanged on the first attempt and retained as the cause of a cachedIOExceptionfor subsequent reads and positive skips.Inspect only the two-byte header, without requiring mark/reset or buffering the body. Preserve zlib checksum and truncation errors instead of retrying corrupt zlib data as raw DEFLATE, reject unsupported preset dictionaries only for zlib streams, and end the inflater when the decoded stream closes, even if closing the source fails.
ContentEncodingHeader.decode()also closes successfully constructed decoder wrappers if a later decoder fails with anIOExceptionorRuntimeException. The original exception is retained, with distinct cleanup failures attached as suppressed exceptions; rethrowing the same exception during cleanup does not cause self-suppression. This closes the input source when a wrapper has already been created; when no wrapper has been created, the source remains the caller's responsibility. After successful construction, the caller owns and closes the returned stream. The ownership behavior is documented in Javadoc.Both
Entity.copyDecodedContentTo()overloads document the consequence of this cleanup: a decoder-chain construction failure after a wrapper was created callsclose()on the current stream. Depending on the stream implementation, this may make the stream and its child branches unreadable. Content set withsetBytes()orsetString()is not closed, but its position may have advanced. Callers that need to preserve the previous state on failure should callpush()before copying andpop()in afinallyblock.Validation:
DeflateDecoderTest,ContentEncodingHeaderTestandEntityTest, covering zlib/raw decoding, lazy initialization, fragmented input, entity readers and failed copies, stacked encodings, stream and inflater ownership, malformed data, checked/unchecked error preservation, self-suppression, and CM/FCHECK/CINFO/FDICT discrimination. The three affected classes contain 122 cases in total.mvn -o -pl commons/http-framework/core -am test: 1,602 tests passed on both JDK 11 and JDK 27 on macOS, with no failures, errors or skips. HTTP framework core accounts for 1,131 tests.Inflater.end(), FCHECK or CINFO, and a mutation that retries corrupt zlib as raw.git diff --checkpassed.Fixes #385