Skip to content

Commit e50ebed

Browse files
committed
Minor work wrt #686, use StreamReadException until better type found
1 parent d295986 commit e50ebed

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/main/java/com/fasterxml/jackson/core/sym/ByteQuadsCanonicalizer.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.Arrays;
44
import java.util.concurrent.atomic.AtomicReference;
55

6+
import com.fasterxml.jackson.core.JacksonException;
7+
import com.fasterxml.jackson.core.exc.StreamReadException;
68
import com.fasterxml.jackson.core.json.JsonFactory;
79
import com.fasterxml.jackson.core.util.InternCache;
810

@@ -1314,10 +1316,13 @@ protected void _reportTooManyCollisions()
13141316
if (_hashSize <= 1024) { // would have spill-over area of 128 entries
13151317
return;
13161318
}
1317-
throw new IllegalStateException("Spill-over slots in symbol table with "+_count
1318-
+" entries, hash area of "+_hashSize+" slots is now full (all "
1319-
+(_hashSize >> 3)+" slots -- suspect a DoS attack based on hash collisions."
1320-
+" You can disable the check via `JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW`");
1319+
// 20-Mar-2021, tatu: [core#686]: should use Jackson-specific exception
1320+
// (to use new "processing limit" exception when available)
1321+
throw new StreamReadException(null,
1322+
"Spill-over slots in symbol table with "+_count
1323+
+" entries, hash area of "+_hashSize+" slots is now full (all "
1324+
+(_hashSize >> 3)+" slots -- suspect a DoS attack based on hash collisions."
1325+
+" You can disable the check via `TokenStreamFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW`");
13211326
}
13221327

13231328
static int _calcTertiaryShift(int primarySlots)

src/main/java/com/fasterxml/jackson/core/sym/CharsToNameCanonicalizer.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.BitSet;
55
import java.util.concurrent.atomic.AtomicReference;
66

7+
import com.fasterxml.jackson.core.exc.StreamReadException;
78
import com.fasterxml.jackson.core.json.JsonFactory;
89
import com.fasterxml.jackson.core.util.InternCache;
910

@@ -531,7 +532,7 @@ private void _handleSpillOverflow(int bucketIndex, Bucket newBucket, int mainInd
531532
if (_overflows.get(bucketIndex)) {
532533
// Has happened once already for this bucket index, so probably not coincidental...
533534
if (JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW.enabledIn(_flags)) {
534-
reportTooManyCollisions(MAX_COLL_CHAIN_LENGTH);
535+
_reportTooManyCollisions(MAX_COLL_CHAIN_LENGTH);
535536
}
536537
// but even if we don't fail, we will stop canonicalizing as safety measure
537538
// (so as not to cause problems with PermGen)
@@ -700,9 +701,13 @@ private void rehash() {
700701
}
701702
}
702703

703-
protected void reportTooManyCollisions(int maxLen) {
704-
throw new IllegalStateException("Longest collision chain in symbol table (of size "+_size
705-
+") now exceeds maximum, "+maxLen+" -- suspect a DoS attack based on hash collisions");
704+
// 20-Mar-2021, tatu: [core#686]: should use Jackson-specific exception
705+
// (to use new "processing limit" exception when available)
706+
protected void _reportTooManyCollisions(int maxLen) {
707+
throw new StreamReadException(null,
708+
"Longest collision chain in symbol table (of size "+_size
709+
+") now exceeds maximum, "+maxLen+" -- suspect a DoS attack based on hash collisions."
710+
+" You can disable the check via `TokenStreamFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW`");
706711
}
707712

708713
// Diagnostics method that will verify that internal data structures are consistent;

src/test/java/com/fasterxml/jackson/core/sym/TestHashCollisionChars.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ public void testReaderCollisions() throws Exception
6363
;
6464
}
6565
fail("Should have failed");
66-
} catch (IllegalStateException e) {
67-
verifyException(e, "hash collision");
66+
} catch (JacksonException e) {
67+
verifyException(e, "Longest collision chain in symbol table");
68+
verifyException(e, "suspect a DoS attack");
6869
}
6970
p.close();
7071

0 commit comments

Comments
 (0)