Skip to content

[#385] Decode HTTP deflate bodies in zlib format - #386

Merged
vharseko merged 4 commits into
OpenIdentityPlatform:masterfrom
vharseko:fix/385-deflate-zlib
Nov 3, 2026
Merged

vharseko merged 4 commits into
OpenIdentityPlatform:masterfrom
vharseko:fix/385-deflate-zlib

Conversation

@vharseko

@vharseko vharseko commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Standard HTTP Content-Encoding: deflate bodies fail with ZipException: invalid stored block lengths because 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. An IOException is retained directly; a RuntimeException or Error is thrown unchanged on the first attempt and retained as the cause of a cached IOException for 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 an IOException or RuntimeException. 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 calls close() on the current stream. Depending on the stream implementation, this may make the stream and its child branches unreadable. Content set with setBytes() or setString() is not closed, but its position may have advanced. Callers that need to preserve the previous state on failure should call push() before copying and pop() in a finally block.

Validation:

  • Added 72 test cases across DeflateDecoderTest, ContentEncodingHeaderTest and EntityTest, 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.
  • Four runtime-header-failure cases and two Error-header-failure cases failed before their corresponding fixes and pass afterward. The Error cases cover raw and zlib streams and assert that the first Error is rethrown unchanged, subsequent reads and positive skips throw the same cached IOException with the original cause, and the source is not read again.
  • Earlier isolated mutation runs on JDK 27 confirmed that the tests detect removal of the runtime-failure cache, zlib gating of FDICT, CM check, self-suppression guard and chain cleanup.
  • Earlier validation also confirmed that the regression tests reject mutations removing Inflater.end(), FCHECK or CINFO, and a mutation that retries corrupt zlib as raw.
  • git diff --check passed.

Fixes #385

@vharseko vharseko added bug tests Test code changes java Pull requests that update java code labels Nov 3, 2026
@vharseko
vharseko requested a review from maximthomas November 3, 2026 07:25

@maximthomas maximthomas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 reports incorrect data check or Unexpected end of ZLIB input stream, not a misleading raw-inflate error.
  • The close() override (DeflateDecoder.java:61-69) ends the caller-supplied Inflater, which InflaterInputStream.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/available instead. That also removes the new path in the next comment.
  • Intended: pin it. Today rejectsMalformedBodies (DeflateDecoderTest.java:136-141) wraps decode() and readAllBytes() in one try, 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 05 there: incorrect header check.
  • Dropping CINFO sends 88 1c there: invalid window size.

This was checked on macOS JDK 11-26.

@vharseko

vharseko commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

@maximthomas Addressed all five points in 00b8e02.

  1. Lazy header detection. The eager reads were not intended as an API change. decode() now returns without reading; header detection starts on the first non-empty read or positive skip. available() conservatively returns zero before initialization, so it cannot block on the header. Tests cover empty/one-byte bodies through both entity stream and reader APIs, closing an unread stream, zero-length operations, and repeated initialization failures.

  2. Partially constructed decoder chains. Included the general cleanup fix in this PR as well. ContentEncodingHeader.decode() closes the outermost successfully created wrapper on IOException or RuntimeException. Cleanup failures are suppressed on the original exception. This also closes the source if a wrapper was created; failures before any wrapper leave the source caller-owned. The contract is documented and tested for nested gzip, unsupported codings, deflate combinations, and read/close failures. Once construction succeeds, the caller remains responsible for closing the stream, including after a lazy read fails.

  3. Inflater lifetime. Added a package-private inflater factory and tests using a real Inflater subclass that records end() calls. They cover both raw and zlib, source-close failure, and repeated close. This avoids reflection and dependence on JDK-specific exception types/messages for an already closed inflater. Removing inf.end() fails all four lifecycle cases.

  4. No raw retry for corrupt zlib. Added assertions for the checksum and truncation exception types and messages. Both pass on local JDK 11 and JDK 27. An isolated implementation that retries corrupt zlib as raw fails this test.

  5. FCHECK and CINFO. Added the raw stored-block fixtures beginning with 08 05 and 88 1c. Removing either discriminator fails the corresponding fixture.

Validation: mvn -o -pl commons/http-framework/core -am test passed 1,588 tests on each of JDK 11 and JDK 27 on macOS, with no failures, errors, or skips. The two affected test classes pass 82 cases. Restoring the old chain-cleanup behavior fails nine cases; removing end(), FCHECK, CINFO, and adding a corrupt-zlib raw retry are also detected. git diff --check passed. The PR description now reflects the final behavior and validation.

@vharseko
vharseko requested a review from maximthomas November 3, 2026 08:45

@maximthomas maximthomas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Both round-2 changes are pinned by the new tests, and CI agrees so far.

  • Making DeflateDecoder.decode eager again fails 10 of the 82 cases in DeflateDecoderTest + ContentEncodingHeaderTest (6 on the lazy contract, 4 in closingDecodedStreamEndsInflater).
  • With ContentEncodingHeader.java put back to its base version, exactly the 9 chain-cleanup cases the description names fail (cleanupPreservesOriginalFailure x4, failedDecodeClosesConstructedWrappers x5).
  • 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.

@vharseko

vharseko commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

@maximthomas Addressed all five points in 8cd8e49.

  1. Unchecked header-detection failures. A RuntimeException is now remembered as the cause of a cached IOException. The first read still throws the original runtime exception; later single-byte reads, bulk reads and positive skips fail without reading the source again. Four cases cover raw/zlib streams with UncheckedIOException and IllegalStateException, thrown after the first header byte. All four fail without the new catch.

  2. Failed decoded copies and entity ownership. The cleanup behavior is intentional. Both Entity.copyDecodedContentTo overloads now document that a decoder-chain construction failure after a wrapper was created closes the current stream and its child branches. Without push(), this closes the entity's content and its child branches. Callers that need the previous state should call push() before copying and pop() in a finally block. Four cases cover the writer/output-stream overloads, with and without a pushed branch: they verify closure of child branches, preservation of the prior stream and saved branches after pop(), and source closure exactly once. Removing chain cleanup fails all four cases.

  3. FDICT on raw bodies. Added the raw stored-block fixture beginning with 00 3e. It decodes successfully, while removing zlib && from the FDICT guard makes the case fail.

  4. Compression-method discrimination. Added the separate 00 1f fixture to isolate the CM check. Removing the CM term fails both new fixtures. Renamed the provider to rawStreamsResemblingZlibHeaders, since the new rows have zero padding.

  5. Self-suppression. Extended the cleanup tests with a shared exception instance thrown from both read and close, for both IOException and RuntimeException. They assert the original exception is returned, there are no suppressed exceptions, and the source closes once. Removing the identity guard fails both cases with IllegalArgumentException.

Validation:

  • mvn -o -pl commons/http-framework/core -am test: 1,600 tests passed on each of JDK 11 and JDK 27 on macOS, with no failures, errors or skips; HTTP framework core accounts for 1,129 tests.
  • The three affected test classes contain 120 cases. Isolated mutations removing the runtime-failure cache, zlib gating of FDICT, CM check, self-suppression guard and chain cleanup fail 4, 1, 2, 2 and 15 cases respectively.
  • git diff --check passed.

The PR description now reflects the final contracts and validation.

@vharseko
vharseko requested a review from maximthomas November 3, 2026 11:06
@vharseko vharseko added the documentation Documentation and Javadoc changes label Nov 3, 2026

@maximthomas maximthomas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: The round-3 changes close the gaps from round 2, and the PR's measurements reproduce.

  • The new catch (RuntimeException e) in DeflateDecoder.decode (DeflateDecoder.java:70) stops the re-sniff after an unchecked header failure. Removing it fails exactly the four uncheckedHeaderFailureCannotBeBypassedByReadingAgain cases.
  • The stated mutation counts reproduce exactly on JDK 26: FDICT gating 1, CM term 2, self-suppression guard 2, chain cleanup 15.
  • DeflateDecoderTest, ContentEncodingHeaderTest and EntityTest pass 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.

@vharseko

vharseko commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

@maximthomas Addressed both points in 74eedb7.

  1. Errors during header detection. DeflateDecoder now catches RuntimeException | Error when initializing the decoded stream. The first attempt rethrows the original error unchanged; later reads and positive skips throw the same cached IOException with that error as its cause, without reading the source again. Two new raw/zlib cases inject an AssertionError after the first header byte and verify single-byte reads, bulk reads, positive skips, exception identity and the source read count. Both cases failed before the fix and pass afterward.

  2. In-memory entity content. Both copyDecodedContentTo Javadocs now say that cleanup calls close() and that its effect depends on the stream implementation. Content set with setBytes() or setString() is not closed, but its position may already have advanced. The push()/pop() guidance remains necessary to preserve the previous state. In an isolated probe on JDK 11 and 27, unsupported, gzip consumed the 10-byte gzip header before failing: the entity retained 15 of 25 bytes, while a previously created child branch retained all 25. Both copy overloads behaved this way.

Validation: mvn -o -pl commons/http-framework/core -am test passed 1,602 tests on each of JDK 11 and JDK 27 on macOS, with no failures, errors or skips. HTTP framework core accounts for 1,131 tests; the three affected test classes contain 122 cases. git diff --check passed.

The PR description now reflects the final behavior and validation. Requesting another review.

@vharseko
vharseko requested a review from maximthomas November 3, 2026 12:32

@maximthomas maximthomas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: The round-3 gap is closed where it was opened, and the new test proves it.

  • DeflateDecoder.java:70 now catches RuntimeException | Error, so an Error thrown during header detection is cached and later reads never re-sniff from a shifted offset.
  • errorHeaderFailureCannotBeBypassedByReadingAgain pins exception identity across read(), read(byte[]) and skip(), and the source read count (reads[0] == 2). With | Error removed, both rows fail. DeflateDecoderTest passes 52/52 on macOS with JDK 11.0.25, 17.0.11, 21.0.2, 25.0.2 and Zulu 26.

@vharseko
vharseko merged commit b8dcdd1 into OpenIdentityPlatform:master Nov 3, 2026
14 checks passed
@vharseko
vharseko deleted the fix/385-deflate-zlib branch November 3, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug documentation Documentation and Javadoc changes java Pull requests that update java code tests Test code changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DeflateDecoder reads Content-Encoding: deflate as raw DEFLATE instead of the zlib format

2 participants