-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid type pollution in StringCollectionDeserializer (#4848)
This patch contains test to guard against type pollution issue when deserializing a `List<String>` property, by adding explicit type checks for common subclasses (ArrayList, HashSet). (actual fix merged separately via #4862)
- Loading branch information
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
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
9 changes: 9 additions & 0 deletions
9
src/test-jdk17/java/com/fasterxml/jackson/databind/typepollution/TypePollutionSuite.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,9 @@ | ||
package com.fasterxml.jackson.databind.typepollution; | ||
|
||
import org.junit.platform.suite.api.SelectPackages; | ||
import org.junit.platform.suite.api.Suite; | ||
|
||
@Suite | ||
@SelectPackages("com.fasterxml.jackson.databind.typepollution") | ||
public class TypePollutionSuite { | ||
} |
91 changes: 91 additions & 0 deletions
91
src/test-jdk17/java/com/fasterxml/jackson/databind/typepollution/TypePollutionTest.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,91 @@ | ||
package com.fasterxml.jackson.databind.typepollution; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import io.micronaut.test.typepollution.FocusListener; | ||
import io.micronaut.test.typepollution.ThresholdFocusListener; | ||
import io.micronaut.test.typepollution.TypePollutionTransformer; | ||
import org.junit.jupiter.api.*; | ||
import org.junit.jupiter.api.function.Executable; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
|
||
public class TypePollutionTest { | ||
private static final int THRESHOLD = 1000; | ||
private ThresholdFocusListener focusListener; | ||
|
||
@BeforeAll | ||
static void setupAgent() { | ||
TypePollutionTransformer.install(net.bytebuddy.agent.ByteBuddyAgent.install()); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { | ||
focusListener = new ThresholdFocusListener(); | ||
FocusListener.setFocusListener(focusListener); | ||
} | ||
|
||
@AfterEach | ||
void verifyNoTypeThrashing() { | ||
FocusListener.setFocusListener(null); | ||
Assertions.assertTrue(focusListener.checkThresholds(THRESHOLD), "Threshold exceeded, check logs."); | ||
} | ||
|
||
private void doTest(Executable r) throws Throwable { | ||
for (int i = 0; i < THRESHOLD * 2; i++) { | ||
r.execute(); | ||
} | ||
} | ||
|
||
@Test | ||
@Disabled | ||
public void sample() throws Throwable { | ||
// example test. If you enable this, it will fail, and you can see what a type pollution error looks like. | ||
|
||
interface A { | ||
} | ||
|
||
interface B { | ||
} | ||
|
||
class Concrete implements A, B { | ||
} | ||
|
||
Object c = new Concrete(); | ||
AtomicInteger j = new AtomicInteger(); | ||
doTest(() -> { | ||
if (c instanceof A) { | ||
j.incrementAndGet(); | ||
} | ||
if (c instanceof B) { | ||
j.incrementAndGet(); | ||
} | ||
}); | ||
System.out.println(j); | ||
} | ||
|
||
@Test | ||
public void deserializeRecordWithList() throws Throwable { | ||
ObjectMapper mapper = JsonMapper.builder().build(); | ||
ListRecord input = new ListRecord(List.of("foo")); | ||
String json = mapper.writeValueAsString(input); | ||
doTest(() -> Assertions.assertEquals(input, mapper.readValue(json, ListRecord.class))); | ||
} | ||
|
||
record ListRecord(List<String> list) { | ||
} | ||
|
||
@Test | ||
public void deserializeRecordWithSet() throws Throwable { | ||
ObjectMapper mapper = JsonMapper.builder().build(); | ||
SetRecord input = new SetRecord(Set.of("foo")); | ||
String json = mapper.writeValueAsString(input); | ||
doTest(() -> Assertions.assertEquals(input, mapper.readValue(json, SetRecord.class))); | ||
} | ||
|
||
record SetRecord(Set<String> list) { | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/java/com/fasterxml/jackson/databind/PrimarySuite.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,15 @@ | ||
package com.fasterxml.jackson.databind; | ||
|
||
import org.junit.platform.suite.api.ExcludeClassNamePatterns; | ||
import org.junit.platform.suite.api.ExcludePackages; | ||
import org.junit.platform.suite.api.SelectPackages; | ||
import org.junit.platform.suite.api.Suite; | ||
|
||
@Suite | ||
@SelectPackages("com.fasterxml.jackson.databind") | ||
@ExcludePackages("com.fasterxml.jackson.databind.typepollution") | ||
@ExcludeClassNamePatterns({ | ||
"com\\.fasterxml\\.jackson\\.databind\\.MapperFootprintTest" | ||
}) | ||
public class PrimarySuite { | ||
} |