summaryrefslogtreecommitdiff
path: root/src/test/scala/spray/json/PrettyPrinterSpec.scala
diff options
context:
space:
mode:
authorJohannes Rudolph <johannes.rudolph@gmail.com>2018-10-30 13:52:29 +0100
committerJohannes Rudolph <johannes.rudolph@gmail.com>2018-11-07 14:51:49 +0100
commit855b35e6d65079085d580ab3063637d94c8f3e0a (patch)
treec703ec80d7544171cef23c92d0bbddd85ba7ea78 /src/test/scala/spray/json/PrettyPrinterSpec.scala
parentcaee7a06b554ca11d95bda2fd9302e4b3b31ef9c (diff)
downloadspray-json-855b35e6d65079085d580ab3063637d94c8f3e0a.tar.gz
spray-json-855b35e6d65079085d580ab3063637d94c8f3e0a.tar.bz2
spray-json-855b35e6d65079085d580ab3063637d94c8f3e0a.zip
CVE-2018-18854 Use TreeMap instead of HashMap for JsObject key/value pairs, fixes #277
The problem is that with String's hashCode implementation it is too simple to create synthetic collisions. This allows an attacker to create an object with keys that all collide which leads to a performance drop for the HashMap just for creating the map in the first place. See https://github.com/scala/bug/issues/11203 for more information about the underlying HashMap issue. For the time being, it seems safer to use a TreeMap which uses String ordering. Benchmarks suggest that using a TreeMap is only ~6% slower for reasonably sized JSON objects up to 100 keys. Benchmark for non-colliding keys: Benchmark (_size) (parser) Mode Cnt Score Error Units ExtractFieldsBenchmark.readSpray 1 HashMap thrpt 5 1195832.262 ± 64366.605 ops/s ExtractFieldsBenchmark.readSpray 1 TreeMap thrpt 5 1342009.641 ± 17307.555 ops/s ExtractFieldsBenchmark.readSpray 10 HashMap thrpt 5 237173.327 ± 70341.742 ops/s ExtractFieldsBenchmark.readSpray 10 TreeMap thrpt 5 233510.618 ± 69638.750 ops/s ExtractFieldsBenchmark.readSpray 100 HashMap thrpt 5 23202.016 ± 1514.763 ops/s ExtractFieldsBenchmark.readSpray 100 TreeMap thrpt 5 21899.072 ± 823.225 ops/s ExtractFieldsBenchmark.readSpray 1000 HashMap thrpt 5 2073.754 ± 66.093 ops/s ExtractFieldsBenchmark.readSpray 1000 TreeMap thrpt 5 1793.329 ± 43.603 ops/s ExtractFieldsBenchmark.readSpray 10000 HashMap thrpt 5 208.160 ± 7.466 ops/s ExtractFieldsBenchmark.readSpray 10000 TreeMap thrpt 5 160.349 ± 5.809 ops/s
Diffstat (limited to 'src/test/scala/spray/json/PrettyPrinterSpec.scala')
-rw-r--r--src/test/scala/spray/json/PrettyPrinterSpec.scala19
1 files changed, 12 insertions, 7 deletions
diff --git a/src/test/scala/spray/json/PrettyPrinterSpec.scala b/src/test/scala/spray/json/PrettyPrinterSpec.scala
index 6354ef0..b547f59 100644
--- a/src/test/scala/spray/json/PrettyPrinterSpec.scala
+++ b/src/test/scala/spray/json/PrettyPrinterSpec.scala
@@ -23,7 +23,7 @@ class PrettyPrinterSpec extends Specification {
"The PrettyPrinter" should {
"print a more complicated JsObject nicely aligned" in {
- val JsObject(fields) = JsonParser {
+ val js = JsonParser {
"""{
| "Boolean no": false,
| "Boolean yes":true,
@@ -40,7 +40,12 @@ class PrettyPrinterSpec extends Specification {
| "zero": 0
|}""".stripMargin
}
- PrettyPrinter(JsObject(ListMap(fields.toSeq.sortBy(_._1):_*))) mustEqual {
+ def fixedFieldOrder(js: JsValue): JsValue = js match {
+ case JsObject(fields) => JsObject(ListMap(fields.toSeq.sortBy(_._1).map { case (k, v) => (k, fixedFieldOrder(v)) }:_*))
+ case x => x
+ }
+
+ PrettyPrinter(fixedFieldOrder(js)) mustEqual {
"""{
| "Boolean no": false,
| "Boolean yes": true,
@@ -50,17 +55,17 @@ class PrettyPrinterSpec extends Specification {
| "number": -0.000012323424,
| "simpleKey": "some value",
| "sub object": {
- | "sub key": 26.5,
| "a": "b",
| "array": [1, 2, {
- | "yes": 1,
- | "no": 0
- | }, ["a", "b", null], false]
+ | "no": 0,
+ | "yes": 1
+ | }, ["a", "b", null], false],
+ | "sub key": 26.5
| },
| "zero": 0
|}""".stripMargin
}
}
}
-
+
} \ No newline at end of file