Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions sjsonnet/src/sjsonnet/Importer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,21 @@ object CachedResolver {
case _: ujson.ParsingFailedException | _: DuplicateJsonKey | _: InvalidJsonNumber |
_: JsonParseDepthExceeded | _: NumberFormatException =>
None
case e: Exception if isInvalidJsonSurrogateException(e) =>
None
}
}

private def isInvalidJsonSurrogateException(e: Exception): Boolean = {
if (e.getClass != classOf[Exception]) return false
val message = e.getMessage
message != null && (
message.startsWith("Unexpected character following high surrogate") ||
message.startsWith("Duplicate high surrogate") ||
message.startsWith("Un-paired low surrogate")
Comment thread
He-Pin marked this conversation as resolved.
)
}

private final class JsonImportVisitor(
fileScope: FileScope,
internedStrings: mutable.HashMap[String, String],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"key": "before\uDC00after"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'error.import_json_lone_low_surrogate.json'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sjsonnet.ParseError: Expected "\"":1:16, found "\\uDC00afte"
at [<root>].(error.import_json_lone_low_surrogate.jsonnet:1:1)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"key": "\uDE00\uD83D"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'error.import_json_reversed_surrogate.json'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sjsonnet.ParseError: Expected "\"":1:10, found "\\uDE00\\uD8"
at [<root>].(error.import_json_reversed_surrogate.jsonnet:1:1)
29 changes: 29 additions & 0 deletions sjsonnet/test/src/sjsonnet/JsonImportFastPathTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,35 @@ object JsonImportFastPathTests extends TestSuite {
}
}

test("invalid unicode surrogate exceptions fall back without internal errors") {
val inputs = Map(
"high-before-non-low-escape.json" -> "{\"key\":\"\\uD800\\u0041\"}",
"duplicate-high.json" -> "{\"key\":\"\\uD800\\uD801\"}",
"lone-low.json" -> "{\"key\":\"before\\uDC00after\"}",
"reversed.json" -> "{\"key\":\"\\uDE00\\uD83D\"}"
)

inputs.foreach { case (fileName, json) =>
val result = eval(Map(fileName -> json), s"""import "$fileName"""")
assert(result.isLeft)
result match {
case Left(error) =>
assert(error.startsWith("sjsonnet.ParseError"))
assert(!error.contains("Internal Error"))
case Right(_) => assert(false)
}
}
}

test("valid unicode surrogate pairs stay on the json fast path") {
val files = Map(
"unicode.json" -> "{\"emoji\":\"\\uD83D\\uDE00\"}"
)

eval(files, """import "unicode.json"""") ==>
Right(ujson.Obj("emoji" -> "\uD83D\uDE00"))
}

test("deep json imports keep parser recursion guard") {
val files = Map("deep.json" -> """[[[]]]""")

Expand Down
Loading