-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CVE-2018-18854 Use TreeMap instead of HashMap for JsObject key/value pairs, fixes #277 #280
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package spray.json | ||
|
||
/** | ||
* Helper that creates strings that all share the same hashCode == 0. | ||
* | ||
* Adapted from MIT-licensed code by Andriy Plokhotnyuk | ||
* at https://github.com/plokhotnyuk/jsoniter-scala/blob/26b5ecdd4f8c2ab7e97bd8106cefdda4c1e701ce/jsoniter-scala-benchmark/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/macros/HashCodeCollider.scala#L6. | ||
*/ | ||
object HashCodeCollider { | ||
val visibleChars = (33 until 127).filterNot(c => c == '\\' || c == '"') | ||
def asciiChars: Iterator[Int] = visibleChars.toIterator | ||
def asciiCharsAndHash(previousHash: Int): Iterator[(Int, Int)] = visibleChars.toIterator.map(c => c -> (previousHash + c) * 31) | ||
|
||
/** Creates an iterator of Strings that all have hashCode == 0 */ | ||
def zeroHashCodeIterator(): Iterator[String] = | ||
for { | ||
(i0, h0) <- asciiCharsAndHash(0) | ||
(i1, h1) <- asciiCharsAndHash(h0) | ||
(i2, h2) <- asciiCharsAndHash(h1) if (((h2 + 32) * 923521) ^ ((h2 + 127) * 923521)) < 0 | ||
(i3, h3) <- asciiCharsAndHash(h2) if (((h3 + 32) * 29791) ^ ((h3 + 127) * 29791)) < 0 | ||
(i4, h4) <- asciiCharsAndHash(h3) if (((h4 + 32) * 961) ^ ((h4 + 127) * 961)) < 0 | ||
(i5, h5) <- asciiCharsAndHash(h4) if (((h5 + 32) * 31) ^ ((h5 + 127) * 31)) < 0 | ||
(i6, h6) <- asciiCharsAndHash(h5) if ((h6 + 32) ^ (h6 + 127)) < 0 | ||
(i7, h7) <- asciiCharsAndHash(h6) if h6 + i7 == 0 | ||
} yield new String(Array(i0, i1, i2, i3, i4, i5, i6, i7).map(_.toChar)) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New rendering output is now ordered by the key Strings. However, that's not guaranteed anywhere so I fixed the test not to assume anything. |
||
| "yes": 1 | ||
| }, ["a", "b", null], false], | ||
| "sub key": 26.5 | ||
| }, | ||
| "zero": 0 | ||
|}""".stripMargin | ||
} | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you undo the fix, the ratio is ~ 100x slowdown for this test on my machine.