From 50d31c2dc3a886683d9ad5d6990ccab36c55f20a Mon Sep 17 00:00:00 2001 From: Nico Piel Date: Fri, 20 Nov 2026 13:39:59 +0100 Subject: [PATCH 1/2] Add characterization tests for HTTPUtil.parseHeaders Pin the behaviour of the current commons-httpclient 3 based implementation (folding, trimming, duplicates, termination, errors) before replacing it. Signed-off-by: Nico Piel --- .../connect/server/userutil/HTTPUtilTest.java | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 server/src/test/java/com/mirth/connect/server/userutil/HTTPUtilTest.java diff --git a/server/src/test/java/com/mirth/connect/server/userutil/HTTPUtilTest.java b/server/src/test/java/com/mirth/connect/server/userutil/HTTPUtilTest.java new file mode 100644 index 0000000000..c1d8936de2 --- /dev/null +++ b/server/src/test/java/com/mirth/connect/server/userutil/HTTPUtilTest.java @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: 2026 Nico Piel + +package com.mirth.connect.server.userutil; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.junit.Assume.assumeTrue; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +/** + * Characterization tests for {@link HTTPUtil#parseHeaders(String)}, pinning the behaviour of the + * original commons-httpclient 3 HttpParser based implementation. + */ +public class HTTPUtilTest { + + private static Map map(String... keyValues) { + Map map = new HashMap<>(); + for (int i = 0; i < keyValues.length; i += 2) { + map.put(keyValues[i], keyValues[i + 1]); + } + return map; + } + + @Test + public void testEmptyString() throws Exception { + assertEquals(map(), HTTPUtil.parseHeaders("")); + } + + @Test + public void testCrlfLines() throws Exception { + assertEquals(map("Content-Type", "text/plain", "Content-Length", "42"), HTTPUtil.parseHeaders("Content-Type: text/plain\r\nContent-Length: 42\r\n")); + } + + @Test + public void testLfLinesAndNoTrailingNewline() throws Exception { + assertEquals(map("A", "1", "B", "2"), HTTPUtil.parseHeaders("A: 1\nB: 2")); + } + + @Test + public void testNameAndValueAreTrimmed() throws Exception { + assertEquals(map("Name", "some value"), HTTPUtil.parseHeaders("Name \t: \t some value \t \r\n")); + assertEquals(map("X", "a"), HTTPUtil.parseHeaders("X:\u000ba\u0001\r\n")); + } + + @Test + public void testValueKeepsFurtherColons() throws Exception { + assertEquals(map("Host", "example.com:8080"), HTTPUtil.parseHeaders("Host: example.com:8080\r\n")); + } + + @Test + public void testEmptyNameAndEmptyValue() throws Exception { + assertEquals(map("", "v", "X", ""), HTTPUtil.parseHeaders(":v\r\nX:\r\n")); + } + + @Test + public void testFoldedContinuationLines() throws Exception { + assertEquals(map("X", "a b c", "Y", "d"), HTTPUtil.parseHeaders("X: a\r\n b \r\n\tc\r\nY: d\r\n")); + } + + @Test + public void testContinuationOfEmptyValue() throws Exception { + assertEquals(map("X", " b"), HTTPUtil.parseHeaders("X:\r\n b\r\n")); + } + + @Test + public void testLeadingContinuationLineIsIgnored() throws Exception { + assertEquals(map("X", "a"), HTTPUtil.parseHeaders(" orphan\r\nX: a\r\n")); + } + + @Test + public void testDuplicateNamesLastWins() throws Exception { + assertEquals(map("X", "second"), HTTPUtil.parseHeaders("X: first\r\nX: second\r\n")); + } + + @Test + public void testNamesAreCaseSensitive() throws Exception { + assertEquals(map("Foo", "1", "foo", "2"), HTTPUtil.parseHeaders("Foo: 1\r\nfoo: 2\r\n")); + } + + @Test + public void testStopsAtBlankLine() throws Exception { + assertEquals(map("A", "1"), HTTPUtil.parseHeaders("A: 1\r\n\r\nB: 2\r\nnot a header\r\n")); + assertEquals(map("A", "1"), HTTPUtil.parseHeaders("A: 1\n\nB: 2\n")); + } + + @Test + public void testStopsAtWhitespaceOnlyLine() throws Exception { + assertEquals(map("A", "1"), HTTPUtil.parseHeaders("A: 1\r\n \t \r\nB: 2\r\n")); + assertEquals(map("A", "1"), HTTPUtil.parseHeaders("A: 1\r\n\r\r\nB: 2\r\n")); + } + + @Test + public void testLeadingBlankLineYieldsNothing() throws Exception { + assertEquals(map(), HTTPUtil.parseHeaders("\r\nA: 1\r\n")); + } + + @Test + public void testCarriageReturnHandling() throws Exception { + // Only a single CR directly before the LF is stripped; others are trimmed or kept + assertEquals(map("A", "x\ry", "B", "w", "C", "z"), HTTPUtil.parseHeaders("A: x\ry\r\nB: w\r\r\nC: z\r")); + } + + @Test + public void testHttpStatusLineIsRejected() { + assertParseFailure("HTTP/1.1 200 OK\r\nA: 1\r\n", "HTTP/1.1 200 OK"); + } + + @Test + public void testLineWithoutColonIsRejected() { + assertParseFailure("A: 1\r\nbogus line\r\n", "bogus line"); + } + + @Test + public void testNonAsciiValue() throws Exception { + // The string is encoded with the platform charset and decoded as UTF-8 + assumeTrue(Charset.defaultCharset().equals(StandardCharsets.UTF_8)); + assertEquals(map("X-Name", "M\u00fcller \u20ac"), HTTPUtil.parseHeaders("X-Name: M\u00fcller \u20ac\r\n")); + } + + @Test(expected = NullPointerException.class) + public void testNullInput() throws Exception { + HTTPUtil.parseHeaders(null); + } + + private static void assertParseFailure(String input, String badLine) { + try { + HTTPUtil.parseHeaders(input); + fail("Expected an IOException"); + } catch (Exception e) { + assertTrue(e.getClass().getName(), e instanceof IOException); + assertEquals("Unable to parse header: " + badLine, e.getMessage()); + } + } +} From bad2d5f23ee6c131850f9757fda125c3f7a54f0a Mon Sep 17 00:00:00 2001 From: Nico Piel Date: Fri, 20 Nov 2026 13:39:59 +0100 Subject: [PATCH 2/2] Parse HTTPUtil headers with httpcore instead of httpclient 3 Delegate HTTPUtil.parseHeaders to httpcore 4.4's AbstractMessageParser.parseHeaders, which is already on the server classpath, so the userutil API no longer needs the EOL commons-httpclient 3 jar. Well-formed headers parse the same. Corner cases that change: - A malformed header throws org.apache.http.ProtocolException ("Invalid header: ..."), which is not an IOException, instead of httpclient 3's ProtocolException ("Unable to parse header"). - Empty header names, a folded line before the first header and bare-CR lines are rejected. - A whitespace-only line is treated as a continuation instead of ending the header block. - Folding onto an empty value no longer adds a leading space. - Trailing whitespace on a folded line is kept. - Only spaces and tabs are trimmed from values, not other control characters. - The string is encoded as UTF-8 instead of the platform charset. Signed-off-by: Nico Piel --- .../connect/server/userutil/HTTPUtil.java | 11 ++-- .../connect/server/userutil/HTTPUtilTest.java | 54 ++++++++++--------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/server/src/main/java/com/mirth/connect/server/userutil/HTTPUtil.java b/server/src/main/java/com/mirth/connect/server/userutil/HTTPUtil.java index c03704bb75..fefe87ba04 100644 --- a/server/src/main/java/com/mirth/connect/server/userutil/HTTPUtil.java +++ b/server/src/main/java/com/mirth/connect/server/userutil/HTTPUtil.java @@ -12,6 +12,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -21,10 +22,12 @@ import javax.xml.parsers.ParserConfigurationException; import org.apache.commons.fileupload.FileUploadBase; -import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.HttpParser; import org.apache.commons.io.IOUtils; +import org.apache.http.Header; import org.apache.http.entity.ContentType; +import org.apache.http.impl.io.AbstractMessageParser; +import org.apache.http.impl.io.HttpTransportMetricsImpl; +import org.apache.http.impl.io.SessionInputBufferImpl; import com.mirth.connect.connectors.http.HttpMessageConverter; import com.mirth.connect.donkey.util.DonkeyElement.DonkeyElementException; @@ -46,7 +49,9 @@ private HTTPUtil() {} */ public static Map parseHeaders(String str) throws Exception { Map headersMap = new HashMap(); - Header[] headers = HttpParser.parseHeaders(new ByteArrayInputStream(str.getBytes()), "UTF-8"); + SessionInputBufferImpl buffer = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), 8192, 8192, null, StandardCharsets.UTF_8.newDecoder()); + buffer.bind(new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8))); + Header[] headers = AbstractMessageParser.parseHeaders(buffer, -1, -1, null); for (int i = 0; i < headers.length; i++) { headersMap.put(headers[i].getName(), headers[i].getValue()); diff --git a/server/src/test/java/com/mirth/connect/server/userutil/HTTPUtilTest.java b/server/src/test/java/com/mirth/connect/server/userutil/HTTPUtilTest.java index c1d8936de2..bf200217da 100644 --- a/server/src/test/java/com/mirth/connect/server/userutil/HTTPUtilTest.java +++ b/server/src/test/java/com/mirth/connect/server/userutil/HTTPUtilTest.java @@ -4,21 +4,17 @@ package com.mirth.connect.server.userutil; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.junit.Assume.assumeTrue; +import static org.junit.Assert.assertThrows; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; +import org.apache.http.ProtocolException; import org.junit.Test; /** * Characterization tests for {@link HTTPUtil#parseHeaders(String)}, pinning the behaviour of the - * original commons-httpclient 3 HttpParser based implementation. + * httpcore 4 AbstractMessageParser based implementation. */ public class HTTPUtilTest { @@ -48,7 +44,8 @@ public void testLfLinesAndNoTrailingNewline() throws Exception { @Test public void testNameAndValueAreTrimmed() throws Exception { assertEquals(map("Name", "some value"), HTTPUtil.parseHeaders("Name \t: \t some value \t \r\n")); - assertEquals(map("X", "a"), HTTPUtil.parseHeaders("X:\u000ba\u0001\r\n")); + // Only spaces and tabs are trimmed, other control characters are kept + assertEquals(map("X", "\u000ba\u0001"), HTTPUtil.parseHeaders("X:\u000ba\u0001\r\n")); } @Test @@ -57,23 +54,29 @@ public void testValueKeepsFurtherColons() throws Exception { } @Test - public void testEmptyNameAndEmptyValue() throws Exception { - assertEquals(map("", "v", "X", ""), HTTPUtil.parseHeaders(":v\r\nX:\r\n")); + public void testEmptyNameIsRejected() { + assertParseFailure(":v\r\nX:\r\n", ":v"); + } + + @Test + public void testEmptyValue() throws Exception { + assertEquals(map("X", ""), HTTPUtil.parseHeaders("X:\r\n")); } @Test public void testFoldedContinuationLines() throws Exception { - assertEquals(map("X", "a b c", "Y", "d"), HTTPUtil.parseHeaders("X: a\r\n b \r\n\tc\r\nY: d\r\n")); + // Leading whitespace of a folded line is dropped, trailing whitespace before the next fold is kept + assertEquals(map("X", "a b c", "Y", "d"), HTTPUtil.parseHeaders("X: a\r\n b \r\n\tc\r\nY: d\r\n")); } @Test public void testContinuationOfEmptyValue() throws Exception { - assertEquals(map("X", " b"), HTTPUtil.parseHeaders("X:\r\n b\r\n")); + assertEquals(map("X", "b"), HTTPUtil.parseHeaders("X:\r\n b\r\n")); } @Test - public void testLeadingContinuationLineIsIgnored() throws Exception { - assertEquals(map("X", "a"), HTTPUtil.parseHeaders(" orphan\r\nX: a\r\n")); + public void testLeadingContinuationLineIsRejected() { + assertParseFailure(" orphan\r\nX: a\r\n", " orphan"); } @Test @@ -93,9 +96,14 @@ public void testStopsAtBlankLine() throws Exception { } @Test - public void testStopsAtWhitespaceOnlyLine() throws Exception { - assertEquals(map("A", "1"), HTTPUtil.parseHeaders("A: 1\r\n \t \r\nB: 2\r\n")); - assertEquals(map("A", "1"), HTTPUtil.parseHeaders("A: 1\r\n\r\r\nB: 2\r\n")); + public void testWhitespaceOnlyLineIsFolded() throws Exception { + assertEquals(map("A", "1", "B", "2"), HTTPUtil.parseHeaders("A: 1\r\n \t \r\nB: 2\r\n")); + } + + @Test + public void testBareCarriageReturnLineIsRejected() { + // httpcore escapes the control character in the message + assertParseFailure("A: 1\r\n\r\r\nB: 2\r\n", "[0x10]"); } @Test @@ -121,8 +129,7 @@ public void testLineWithoutColonIsRejected() { @Test public void testNonAsciiValue() throws Exception { - // The string is encoded with the platform charset and decoded as UTF-8 - assumeTrue(Charset.defaultCharset().equals(StandardCharsets.UTF_8)); + // The string is encoded and decoded as UTF-8, whatever the platform charset assertEquals(map("X-Name", "M\u00fcller \u20ac"), HTTPUtil.parseHeaders("X-Name: M\u00fcller \u20ac\r\n")); } @@ -132,12 +139,7 @@ public void testNullInput() throws Exception { } private static void assertParseFailure(String input, String badLine) { - try { - HTTPUtil.parseHeaders(input); - fail("Expected an IOException"); - } catch (Exception e) { - assertTrue(e.getClass().getName(), e instanceof IOException); - assertEquals("Unable to parse header: " + badLine, e.getMessage()); - } + ProtocolException e = assertThrows(ProtocolException.class, () -> HTTPUtil.parseHeaders(input)); + assertEquals("Invalid header: " + badLine, e.getMessage()); } }