From e0902e5c56b09d1a15848d2eebf897a258529784 Mon Sep 17 00:00:00 2001 From: Zibran Khan Date: Mon, 9 Nov 2026 12:10:15 +0530 Subject: [PATCH] bound ApplicationXMLBuilder and DataSourceBuilder request bodies --- .../axis2/builder/ApplicationXMLBuilder.java | 11 ++- .../axis2/builder/DataSourceBuilder.java | 8 ++- .../axis2/builder/RequestSizeLimitsTest.java | 69 +++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/modules/kernel/src/org/apache/axis2/builder/ApplicationXMLBuilder.java b/modules/kernel/src/org/apache/axis2/builder/ApplicationXMLBuilder.java index da9e8de26b..26cd0fa296 100644 --- a/modules/kernel/src/org/apache/axis2/builder/ApplicationXMLBuilder.java +++ b/modules/kernel/src/org/apache/axis2/builder/ApplicationXMLBuilder.java @@ -48,8 +48,15 @@ public OMElement processDocument(InputStream inputStream, String contentType, SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope(); if (inputStream != null) { try { - - PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream); + // Bounded for the same reason as SOAPBuilder: this reads the + // transport stream directly, and application/xml is registered in + // every shipped config, so the plain SOAP/POX ceiling was avoided + // by sending the same body with this Content-Type. + long maxRequestSize = RequestSizeLimits.resolve(messageContext, + RequestSizeLimits.SOAP_MAX_REQUEST_SIZE, + RequestSizeLimits.DEFAULT_SOAP_MAX_REQUEST_SIZE); + PushbackInputStream pushbackInputStream = new PushbackInputStream( + BoundedInputStream.wrap(inputStream, maxRequestSize)); int b; if ((b = pushbackInputStream.read()) > 0) { pushbackInputStream.unread(b); diff --git a/modules/kernel/src/org/apache/axis2/builder/DataSourceBuilder.java b/modules/kernel/src/org/apache/axis2/builder/DataSourceBuilder.java index 4c96effcb8..c14a018568 100644 --- a/modules/kernel/src/org/apache/axis2/builder/DataSourceBuilder.java +++ b/modules/kernel/src/org/apache/axis2/builder/DataSourceBuilder.java @@ -48,10 +48,16 @@ public OMElement processDocument(InputStream inputStream, String contentType, OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace ns = factory.createOMNamespace("", ""); + // Bounded for the same reason as the other builders that read the + // transport stream directly: this one copies the whole body into a + // byte[], so without a ceiling an anonymous caller sizes the allocation. + long maxRequestSize = RequestSizeLimits.resolve(msgContext, + RequestSizeLimits.SOAP_MAX_REQUEST_SIZE, + RequestSizeLimits.DEFAULT_SOAP_MAX_REQUEST_SIZE); byte[] bytes; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - IOUtils.copy(inputStream, baos, -1); + IOUtils.copy(BoundedInputStream.wrap(inputStream, maxRequestSize), baos, -1); baos.flush(); bytes = baos.toByteArray(); } catch (IOException e) { diff --git a/modules/kernel/test/org/apache/axis2/builder/RequestSizeLimitsTest.java b/modules/kernel/test/org/apache/axis2/builder/RequestSizeLimitsTest.java index 6ce056919a..95f6ad09b8 100644 --- a/modules/kernel/test/org/apache/axis2/builder/RequestSizeLimitsTest.java +++ b/modules/kernel/test/org/apache/axis2/builder/RequestSizeLimitsTest.java @@ -20,6 +20,7 @@ package org.apache.axis2.builder; import junit.framework.TestCase; +import org.apache.axiom.soap.SOAPEnvelope; import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.MessageContext; import org.apache.axis2.description.Parameter; @@ -159,4 +160,72 @@ public void testSoapCeilingCanBeOptedOut() throws Exception { RequestSizeLimits.SOAP_MAX_REQUEST_SIZE, RequestSizeLimits.DEFAULT_SOAP_MAX_REQUEST_SIZE)); } + + /** + * application/xml is registered for every configuration, so the plain SOAP + * ceiling was avoided by sending the same body with that Content-Type. + */ + public void testApplicationXMLBuilderIsBounded() throws Exception { + axisConfiguration.addParameter( + new Parameter(RequestSizeLimits.SOAP_MAX_REQUEST_SIZE, "64")); + try { + readPOXText(1000); + fail("An over-sized application/xml body was read in full"); + } catch (Exception e) { + assertExceedsCeiling(e); + } + } + + public void testApplicationXMLBuilderAcceptsABodyUnderTheCeiling() throws Exception { + axisConfiguration.addParameter( + new Parameter(RequestSizeLimits.SOAP_MAX_REQUEST_SIZE, "64")); + assertEquals("AAAAAAAA", readPOXText(8)); + } + + /** + * DataSourceBuilder copies the whole body into a byte[], so it was unbounded + * for any content type a deployment registers it for. + */ + public void testDataSourceBuilderIsBounded() throws Exception { + axisConfiguration.addParameter( + new Parameter(RequestSizeLimits.SOAP_MAX_REQUEST_SIZE, "64")); + try { + new DataSourceBuilder().processDocument( + new ByteArrayInputStream(new byte[4096]), "application/octet-stream", + messageContext); + fail("An over-sized body was copied in full"); + } catch (Exception e) { + assertExceedsCeiling(e); + } + } + + public void testDataSourceBuilderAcceptsABodyUnderTheCeiling() throws Exception { + axisConfiguration.addParameter( + new Parameter(RequestSizeLimits.SOAP_MAX_REQUEST_SIZE, "64")); + assertNotNull(new DataSourceBuilder().processDocument( + new ByteArrayInputStream(new byte[8]), "application/octet-stream", + messageContext)); + } + + /** Builds an application/xml body and reads its content the way a receiver does. */ + private String readPOXText(int length) throws Exception { + StringBuilder sb = new StringBuilder(""); + for (int i = 0; i < length; i++) { + sb.append('A'); + } + sb.append(""); + SOAPEnvelope envelope = (SOAPEnvelope) new ApplicationXMLBuilder().processDocument( + new ByteArrayInputStream(sb.toString().getBytes("UTF-8")), + "application/xml", messageContext); + return envelope.getBody().getFirstElement().getText(); + } + + private static void assertExceedsCeiling(Throwable t) { + for (Throwable c = t; c != null; c = c.getCause()) { + if (c.getMessage() != null && c.getMessage().contains("exceeds the configured maximum")) { + return; + } + } + fail("Expected the request-size ceiling to be hit, got " + t); + } }