Skip to content

Commit c47cffc

Browse files
committed
CAMEL-9309: Make it easier to turn on|off java transport over http
1 parent 94330f9 commit c47cffc

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ public DefaultHttpBinding(HeaderFilterStrategy headerFilterStrategy) {
8989
public DefaultHttpBinding(HttpCommonEndpoint endpoint) {
9090
this.headerFilterStrategy = endpoint.getHeaderFilterStrategy();
9191
this.transferException = endpoint.isTransferException();
92-
this.allowJavaSerializedObject = endpoint.getComponent().isAllowJavaSerializedObject();
92+
if (endpoint.getComponent() != null) {
93+
this.allowJavaSerializedObject = endpoint.getComponent().isAllowJavaSerializedObject();
94+
}
9395
}
9496

9597
public void readRequest(HttpServletRequest request, HttpMessage message) {
@@ -153,6 +155,7 @@ public void readRequest(HttpServletRequest request, HttpMessage message) {
153155

154156
// if content type is serialized java object, then de-serialize it to a Java object
155157
if (request.getContentType() != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(request.getContentType())) {
158+
// only deserialize java if allowed
156159
if (allowJavaSerializedObject || isTransferException()) {
157160
try {
158161
InputStream is = message.getExchange().getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, body);
@@ -164,7 +167,8 @@ public void readRequest(HttpServletRequest request, HttpMessage message) {
164167
throw new RuntimeCamelException("Cannot deserialize body to Java object", e);
165168
}
166169
} else {
167-
throw new RuntimeCamelException("Content-type " + HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed");
170+
// set empty body
171+
message.setBody(null);
168172
}
169173
}
170174

components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,16 @@ public void setHttpConfiguration(HttpConfiguration httpConfiguration) {
337337
// need to override and call super for component docs
338338
super.setHttpConfiguration(httpConfiguration);
339339
}
340+
341+
/**
342+
* Whether to allow java serialization when a request uses context-type=application/x-java-serialized-object
343+
* <p/>
344+
* This is by default turned off. If you enable this then be aware that Java will deserialize the incoming
345+
* data from the request to Java and that can be a potential security risk.
346+
*/
347+
@Override
348+
public void setAllowJavaSerializedObject(boolean allowJavaSerializedObject) {
349+
// need to override and call super for component docs
350+
super.setAllowJavaSerializedObject(allowJavaSerializedObject);
351+
}
340352
}

components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.camel.CamelExchangeException;
3333
import org.apache.camel.Exchange;
3434
import org.apache.camel.Message;
35+
import org.apache.camel.RuntimeCamelException;
3536
import org.apache.camel.component.file.GenericFile;
3637
import org.apache.camel.converter.stream.CachedOutputStream;
3738
import org.apache.camel.http.common.HttpConstants;
@@ -280,7 +281,7 @@ protected static Map<String, String> extractResponseHeaders(Header[] responseHea
280281
* @return the response either as a stream, or as a deserialized java object
281282
* @throws IOException can be thrown
282283
*/
283-
protected static Object extractResponseBody(HttpMethod method, Exchange exchange, boolean ignoreResponseBody) throws IOException, ClassNotFoundException {
284+
protected Object extractResponseBody(HttpMethod method, Exchange exchange, boolean ignoreResponseBody) throws IOException, ClassNotFoundException {
284285
InputStream is = method.getResponseBodyAsStream();
285286
if (is == null) {
286287
return null;
@@ -304,7 +305,13 @@ protected static Object extractResponseBody(HttpMethod method, Exchange exchange
304305

305306
// if content type is a serialized java object then de-serialize it back to a Java object
306307
if (contentType != null && contentType.equals(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT)) {
307-
return HttpHelper.deserializeJavaObjectFromStream(is, exchange.getContext());
308+
// only deserialize java if allowed
309+
if (getEndpoint().getComponent().isAllowJavaSerializedObject() || getEndpoint().isTransferException()) {
310+
return HttpHelper.deserializeJavaObjectFromStream(is, exchange.getContext());
311+
} else {
312+
// empty response
313+
return null;
314+
}
308315
} else {
309316
InputStream response = null;
310317
if (!ignoreResponseBody) {
@@ -418,6 +425,9 @@ protected RequestEntity createRequestEntity(Exchange exchange) throws CamelExcha
418425
String contentType = ExchangeHelper.getContentType(exchange);
419426

420427
if (contentType != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) {
428+
if (!getEndpoint().getComponent().isAllowJavaSerializedObject()) {
429+
throw new CamelExchangeException("Content-type " + HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed", exchange);
430+
}
421431
// serialized java object
422432
Serializable obj = in.getMandatoryBody(Serializable.class);
423433
// write object to output stream

0 commit comments

Comments
 (0)