Skip to content

Commit

Permalink
CAMEL-20306 - Camel-CassandraQL: Add ObjectInputFilter String pattern…
Browse files Browse the repository at this point in the history
… parameter in CassandraAggregationRepository to be used in unmarshall operations (#12760) (#12790)

* CAMEL-20306 - Camel-CassandraQL: Add ObjectInputFilter String pattern parameter in CassandraAggregationRepository to be used in unmarshall operations



* CAMEL-20306 - Camel-CassandraQL: Add ObjectInputFilter String pattern parameter in CassandraAggregationRepository to be used in unmarshall operations - Docs



---------

Signed-off-by: Andrea Cosentino <[email protected]>
  • Loading branch information
oscerd authored Jan 15, 2024
1 parent f71ee8f commit 8a5202a
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 9 deletions.
4 changes: 4 additions & 0 deletions components/camel-cassandraql/src/main/docs/cql-component.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ Alternatively, the `CassandraAggregationRepository` does not have a
`LOCAL_QUORUM`…
|=======================================================================

While deserializing it's important to notice that the the unmarshallExchange method will allow only all java packages and subpackages
and org.apache.camel packages and subpackages. The remaining classes will be blacklisted. So you'll need to change the filter in case of need.
This could be accomplished by changing the deserializationFilter field on the repository.

== Examples

To insert something on a table you can use the following code:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ public class CassandraAggregationRepository extends ServiceSupport implements Re

private boolean allowSerializedHeaders;

/**
* Sets a deserialization filter while reading Object from Aggregation Repository. By default the filter will allow
* all java packages and subpackages and all org.apache.camel packages and subpackages, while the remaining will be
* blacklisted and not deserialized. This parameter should be customized if you're using classes you trust to be
* deserialized.
*/
private String deserializationFilter = "java.**;org.apache.camel.**;!*";

public CassandraAggregationRepository() {
}

Expand Down Expand Up @@ -211,7 +219,8 @@ public Exchange get(CamelContext camelContext, String key) {
Exchange exchange = null;
if (row != null) {
try {
exchange = exchangeCodec.unmarshallExchange(camelContext, row.getByteBuffer(exchangeColumn));
exchange = exchangeCodec.unmarshallExchange(camelContext, row.getByteBuffer(exchangeColumn),
deserializationFilter);
} catch (IOException iOException) {
throw new CassandraAggregationException("Failed to read exchange", exchange, iOException);
} catch (ClassNotFoundException classNotFoundException) {
Expand Down Expand Up @@ -468,4 +477,12 @@ public boolean isAllowSerializedHeaders() {
public void setAllowSerializedHeaders(boolean allowSerializedHeaders) {
this.allowSerializedHeaders = allowSerializedHeaders;
}

public String getDeserializationFilter() {
return deserializationFilter;
}

public void setDeserializationFilter(String deserializationFilter) {
this.deserializationFilter = deserializationFilter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
*/
package org.apache.camel.processor.aggregate.cassandra;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.*;
import java.nio.ByteBuffer;

import org.apache.camel.CamelContext;
Expand Down Expand Up @@ -63,9 +59,10 @@ public ByteBuffer marshallExchange(Exchange exchange, boolean allowSerializedHea
return ByteBuffer.wrap(serialize(pe));
}

public Exchange unmarshallExchange(CamelContext camelContext, ByteBuffer buffer)
public Exchange unmarshallExchange(CamelContext camelContext, ByteBuffer buffer, String deserializationFilter)
throws IOException, ClassNotFoundException {
DefaultExchangeHolder pe = (DefaultExchangeHolder) deserialize(camelContext, new ByteBufferInputStream(buffer));
DefaultExchangeHolder pe
= (DefaultExchangeHolder) deserialize(camelContext, new ByteBufferInputStream(buffer), deserializationFilter);
Exchange answer = new DefaultExchange(camelContext);
DefaultExchangeHolder.unmarshal(answer, pe);
// restore the from endpoint
Expand All @@ -87,9 +84,11 @@ private byte[] serialize(Object object) throws IOException {
return bytesOut.toByteArray();
}

private Object deserialize(CamelContext camelContext, InputStream bytes) throws IOException, ClassNotFoundException {
private Object deserialize(CamelContext camelContext, InputStream bytes, String deserializationFilter)
throws IOException, ClassNotFoundException {
ClassLoader classLoader = camelContext.getApplicationContextClassLoader();
ObjectInputStream objectIn = new ClassLoadingAwareObjectInputStream(classLoader, bytes);
objectIn.setObjectInputFilter(ObjectInputFilter.Config.createFilter(deserializationFilter));
Object object = objectIn.readObject();
objectIn.close();
return object;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.processor.aggregate.cassandra;

import java.io.*;
import java.nio.ByteBuffer;

import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.malicious.example.Employee;

public class CassandraCamelCodecTest extends CamelTestSupport {

CassandraCamelCodec codec;

@Override
protected void startCamelContext() throws Exception {
super.startCamelContext();
codec = new CassandraCamelCodec();
}

@Test
public void shouldFailWithRejected() throws IOException, ClassNotFoundException {
Employee emp = new Employee("Mickey", "Mouse");

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);

oos.writeObject(emp);

oos.flush();
oos.close();

InputStream is = new ByteArrayInputStream(baos.toByteArray());
InvalidClassException thrown = Assertions.assertThrows(InvalidClassException.class, () -> {
codec.unmarshallExchange(context, ByteBuffer.wrap(is.readAllBytes()), "java.**;org.apache.camel.**;!*");
});

Assertions.assertEquals("filter status: REJECTED", thrown.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.malicious.example;

import java.io.Serializable;

public class Employee implements Serializable {

String name;
String surname;

public Employee(String name, String surname) {
this.name = name;
this.surname = surname;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}
}

0 comments on commit 8a5202a

Please sign in to comment.