forked from hazelcast/hazelcast
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[hazelcast#15346] Fix deserialization filtering for Externalizables a…
…nd Deadlock in Map index
- Loading branch information
Showing
5 changed files
with
226 additions
and
32 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
.../java/com/hazelcast/client/serialization/ExternalizableDeserializationProtectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. | ||
* | ||
* Licensed 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 com.hazelcast.client.serialization; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.runner.RunWith; | ||
|
||
import com.hazelcast.client.config.ClientConfig; | ||
import com.hazelcast.client.test.TestHazelcastFactory; | ||
import com.hazelcast.config.Config; | ||
import com.hazelcast.config.JavaSerializationFilterConfig; | ||
import com.hazelcast.config.MapIndexConfig; | ||
import com.hazelcast.core.HazelcastInstance; | ||
import com.hazelcast.nio.serialization.HazelcastSerializationException; | ||
import com.hazelcast.test.HazelcastSerialClassRunner; | ||
import com.hazelcast.test.HazelcastTestSupport; | ||
import com.hazelcast.test.annotation.QuickTest; | ||
|
||
import example.serialization.TestExternalizableDeserialized; | ||
|
||
/** | ||
* Tests untrusted deserialization protection for Externalizables. | ||
* | ||
* <pre> | ||
* Given: Hazelcast member and clients are started. | ||
* </pre> | ||
*/ | ||
@RunWith(HazelcastSerialClassRunner.class) | ||
@Category(QuickTest.class) | ||
public class ExternalizableDeserializationProtectionTest extends HazelcastTestSupport { | ||
|
||
@Rule | ||
public ExpectedException expected = ExpectedException.none(); | ||
|
||
protected static TestHazelcastFactory hazelcastFactory = new TestHazelcastFactory(); | ||
|
||
@AfterClass | ||
public static final void stopHazelcastInstances() { | ||
hazelcastFactory.terminateAll(); | ||
} | ||
|
||
@Before | ||
public void killAllHazelcastInstances() throws IOException { | ||
hazelcastFactory.terminateAll(); | ||
TestExternalizableDeserialized.isDeserialized = false; | ||
} | ||
|
||
@Test | ||
public void testExternalizableProtectedOnMember() { | ||
JavaSerializationFilterConfig javaSerializationFilterConfig = new JavaSerializationFilterConfig() | ||
.setDefaultsDisabled(true); | ||
javaSerializationFilterConfig.getBlacklist().addClasses(TestExternalizableDeserialized.class.getName()); | ||
|
||
Config config = smallInstanceConfig(); | ||
config.getSerializationConfig().setJavaSerializationFilterConfig(javaSerializationFilterConfig); | ||
// the index will force deserialization | ||
config.getMapConfig("test").addMapIndexConfig(new MapIndexConfig("name", false)); | ||
hazelcastFactory.newHazelcastInstance(config); | ||
|
||
HazelcastInstance client = hazelcastFactory.newHazelcastClient(); | ||
|
||
expected.expect(HazelcastSerializationException.class); | ||
client.getMap("test").put("key", new TestExternalizableDeserialized()); | ||
} | ||
|
||
@Test | ||
public void testExternalizableProtectedOnClient() { | ||
JavaSerializationFilterConfig javaSerializationFilterConfig = new JavaSerializationFilterConfig() | ||
.setDefaultsDisabled(true); | ||
javaSerializationFilterConfig.getBlacklist().addClasses(TestExternalizableDeserialized.class.getName()); | ||
|
||
Config config = smallInstanceConfig(); | ||
hazelcastFactory.newHazelcastInstance(config); | ||
|
||
ClientConfig clientConfig1 = new ClientConfig(); | ||
HazelcastInstance client1 = hazelcastFactory.newHazelcastClient(clientConfig1); | ||
client1.getMap("test").put("key", new TestExternalizableDeserialized()); | ||
// we don't have an index on map, so the value should not be deserialized | ||
assertFalse(TestExternalizableDeserialized.isDeserialized); | ||
// deserialized on client | ||
client1.getMap("test").get("key"); | ||
assertTrue(TestExternalizableDeserialized.isDeserialized); | ||
TestExternalizableDeserialized.isDeserialized = false; | ||
|
||
ClientConfig clientConfig2 = new ClientConfig(); | ||
clientConfig2.getSerializationConfig().setJavaSerializationFilterConfig(javaSerializationFilterConfig); | ||
HazelcastInstance client2 = hazelcastFactory.newHazelcastClient(clientConfig2); | ||
expected.expect(HazelcastSerializationException.class); | ||
client2.getMap("test").get("key"); | ||
} | ||
|
||
@Test | ||
public void testExternalizableUnprotected() { | ||
Config config = smallInstanceConfig(); | ||
config.getMapConfig("test").addMapIndexConfig(new MapIndexConfig("name", false)); | ||
hazelcastFactory.newHazelcastInstance(config); | ||
|
||
HazelcastInstance client = hazelcastFactory.newHazelcastClient(); | ||
|
||
client.getMap("test").put("key", new TestExternalizableDeserialized()); | ||
assertTrue(TestExternalizableDeserialized.isDeserialized); | ||
TestExternalizableDeserialized.isDeserialized = false; | ||
client.getMap("test").get("key"); | ||
assertTrue(TestExternalizableDeserialized.isDeserialized); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
hazelcast/src/test/java/example/serialization/TestExternalizableDeserialized.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. | ||
* | ||
* Licensed 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 example.serialization; | ||
|
||
import java.io.Externalizable; | ||
import java.io.IOException; | ||
import java.io.ObjectInput; | ||
import java.io.ObjectOutput; | ||
|
||
public class TestExternalizableDeserialized implements Externalizable { | ||
|
||
public static volatile boolean isDeserialized = false; | ||
|
||
// field used for indexing in IMap | ||
private String name = "foo"; | ||
|
||
@Override | ||
public void writeExternal(ObjectOutput out) throws IOException { | ||
} | ||
|
||
@Override | ||
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { | ||
isDeserialized = true; | ||
} | ||
} |