Skip to content

Commit 2a00a0e

Browse files
Merge pull request #1326 from kali834x/rest-json-only-check
Keep both new rows in the 2.0.3 upgrade table. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2 parents 2d3597a + 4bb08c2 commit 2a00a0e

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

‎modules/transport/http/src/main/java/org/apache/axis2/transport/http/util/RESTUtil.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static Handler.InvocationResponse processXMLRequest(MessageContext msgCon
5353
InputStream in,
5454
OutputStream out, String contentType)
5555
throws AxisFault {
56+
checkJSONOnly(msgContext, contentType);
5657
try {
5758
msgContext.setDoingREST(true);
5859
String charSetEncoding = BuilderUtil.getCharSetEncoding(contentType);
@@ -95,6 +96,7 @@ public static Handler.InvocationResponse processXMLRequest(MessageContext msgCon
9596
OutputStream out, String contentType,
9697
Builder builder)
9798
throws AxisFault {
99+
checkJSONOnly(msgContext, contentType);
98100
try {
99101
msgContext.setDoingREST(true);
100102
String charSetEncoding = BuilderUtil.getCharSetEncoding(contentType);
@@ -136,6 +138,7 @@ public static Handler.InvocationResponse processXMLRequest(MessageContext msgCon
136138
public static Handler.InvocationResponse processURLRequest(MessageContext msgContext,
137139
OutputStream out, String contentType)
138140
throws AxisFault {
141+
checkJSONOnly(msgContext, contentType);
139142
// here, only the parameters in the URI are supported. Others will be discarded.
140143
try {
141144

@@ -188,6 +191,7 @@ public static Handler.InvocationResponse processURLRequest(MessageContext msgCon
188191
OutputStream out,
189192
String contentType, Builder builder)
190193
throws AxisFault {
194+
checkJSONOnly(msgContext, contentType);
191195
// here, only the parameters in the URI are supported. Others will be discarded.
192196
try {
193197

@@ -237,6 +241,24 @@ public static Handler.InvocationResponse processURLRequest(MessageContext msgCon
237241
return invokeAxisEngine(msgContext);
238242
}
239243

244+
/**
245+
* enableJSONOnly refuses any request whose content type is not application/json.
246+
* HTTPTransportUtils.processHTTPPostRequest applies that to POSTed messages; the
247+
* REST entry points need the same check, or a GET, PUT or DELETE (and a REST POST
248+
* on the standalone server) still reaches the operation through the XML and form
249+
* builders.
250+
*/
251+
private static void checkJSONOnly(MessageContext msgContext, String contentType)
252+
throws AxisFault {
253+
String enableJSONOnly = (String) msgContext.getConfigurationContext()
254+
.getAxisConfiguration().getParameterValue(Constants.Configuration.ENABLE_JSON_ONLY);
255+
if (enableJSONOnly != null && enableJSONOnly.equalsIgnoreCase("true")
256+
&& !HTTPTransportUtils.isJSONRequest(contentType)) {
257+
throw new AxisFault("JSON-only mode enabled but content-type is not application/json: "
258+
+ contentType);
259+
}
260+
}
261+
240262
private static Handler.InvocationResponse invokeAxisEngine(MessageContext messageContext)
241263
throws AxisFault {
242264
return AxisEngine.receive(messageContext);

‎modules/transport/http/src/test/java/org/apache/axis2/transport/http/AxisServletJSONOnlyTest.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.ByteArrayOutputStream;
2323
import java.io.PrintWriter;
2424
import java.io.StringWriter;
25+
import java.util.concurrent.atomic.AtomicInteger;
2526

2627
import jakarta.servlet.ServletConfig;
2728
import jakarta.servlet.ServletContext;
@@ -34,13 +35,17 @@
3435

3536
import org.apache.axis2.AxisFault;
3637
import org.apache.axis2.Constants;
38+
import org.apache.axis2.builder.ApplicationXMLBuilder;
39+
import org.apache.axis2.builder.XFormURLEncodedBuilder;
3740
import org.apache.axis2.context.ConfigurationContext;
3841
import org.apache.axis2.context.ConfigurationContextFactory;
3942
import org.apache.axis2.context.MessageContext;
43+
import org.apache.axis2.description.AxisOperation;
4044
import org.apache.axis2.description.AxisService;
4145
import org.apache.axis2.description.InOnlyAxisOperation;
4246
import org.apache.axis2.description.Parameter;
4347
import org.apache.axis2.engine.AxisConfiguration;
48+
import org.apache.axis2.engine.MessageReceiver;
4449
import org.mockito.Mockito;
4550

4651
import static org.mockito.ArgumentMatchers.any;
@@ -245,6 +250,47 @@ public void testEnableJSONOnlyRejectsNonJSONRequests() throws Exception {
245250
responseContent.contains("application/json is mandatory"));
246251
}
247252

253+
/**
254+
* GET and PUT go through RESTUtil rather than processHTTPPostRequest, and must
255+
* refuse a request that is not application/json as POST does, rather than
256+
* binding the operation from the query string or an XML body.
257+
*/
258+
public void testEnableJSONOnlyRejectsNonJSONRestRequests() throws Exception {
259+
axisConfig.addParameter(new Parameter(Constants.Configuration.ENABLE_JSON_ONLY, "true"));
260+
axisConfig.addMessageBuilder("application/x-www-form-urlencoded", new XFormURLEncodedBuilder());
261+
axisConfig.addMessageBuilder("application/xml", new ApplicationXMLBuilder());
262+
263+
final AtomicInteger invocations = new AtomicInteger();
264+
AxisOperation operation = axisConfig.getService("TestService")
265+
.getOperation(new QName("testOperation"));
266+
operation.setMessageReceiver(new MessageReceiver() {
267+
public void receive(MessageContext messageContext) {
268+
invocations.incrementAndGet();
269+
}
270+
});
271+
272+
String uri = "/axis2/services/TestService/testOperation";
273+
when(mockRequest.getRequestURI()).thenReturn(uri);
274+
when(mockRequest.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080" + uri));
275+
276+
when(mockServletContext.getAttribute(AxisServlet.CONFIGURATION_CONTEXT)).thenReturn(configContext);
277+
servlet.init(mockServletConfig);
278+
279+
when(mockRequest.getMethod()).thenReturn("GET");
280+
when(mockRequest.getQueryString()).thenReturn("value=1");
281+
servlet.doGet(mockRequest, mockResponse);
282+
283+
when(mockRequest.getMethod()).thenReturn("PUT");
284+
when(mockRequest.getQueryString()).thenReturn(null);
285+
when(mockRequest.getContentType()).thenReturn("application/xml");
286+
when(mockRequest.getInputStream()).thenReturn(
287+
new MockServletInputStream("<testOperation><value>1</value></testOperation>"));
288+
servlet.doPut(mockRequest, mockResponse);
289+
290+
assertEquals("non-JSON REST requests must not reach the operation", 0, invocations.get());
291+
verify(mockResponse, times(2)).setStatus(HttpServletResponse.SC_BAD_REQUEST);
292+
}
293+
248294
// Helper classes for mocking
249295
private static class MockServletInputStream extends jakarta.servlet.ServletInputStream {
250296
private final ByteArrayInputStream inputStream;

‎src/site/markdown/release-notes/2.0.3.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Apache Axis2 2.0.3 Release Notes
99
|---|---|
1010
| The JSON dispatcher no longer selects a service from the XML body namespace | `<parameter name="allowContentBasedServiceDispatch">true</parameter>` |
1111
| A WSDL read with no resolver refuses absolute `wsdl:import` and `xsd:import` locations | `WSDL11ToAxisServiceBuilder.setCustomWSDLResolver(...)` with a locator of your own |
12+
| `enableJSONOnly` refuses non-JSON GET, PUT and DELETE requests as it does POST | no switch; send `Content-Type: application/json` |
1213

1314
`JSONBasedDefaultDispatcher`, the Dispatch-phase dispatcher in the Spring Boot
1415
starter's `axis2-json.xml`, now honours `allowContentBasedServiceDispatch` as the
@@ -18,3 +19,9 @@ URI naming no service, relying on the root element's namespace to pick the servi
1819
now gets a service-not-found fault until the parameter is set. Set it only if
1920
services are genuinely addressed that way, and not alongside per-service security
2021
modules.
22+
23+
`enableJSONOnly` was only enforced for POST. A GET, PUT or DELETE, and on the
24+
standalone server a POST with an XML or form content type, took the REST path, which
25+
bound the operation's arguments from the query string or an XML body and invoked it.
26+
Those requests are now refused unless they carry `Content-Type: application/json`.
27+
Metadata requests (`?wsdl`, `?xsd`, the service listing) are unaffected.

0 commit comments

Comments
 (0)