ソースを参照

Range check timestamps on all parsing paths.

Include the fuzz test that caught this. The bug here is this was being accepted
as valid input, when it couldn't be serialized back out into JSON, so this move
the parsing test to make sure if fails no matter how it is formatted at parsing
time.
Thomas Van Lenten 1 年間 前
コミット
b842955fa4

+ 1 - 0
FuzzTesting/FailCases/clusterfuzz-testcase-minimized-FuzzJSON_debug-6286338012282880

@@ -0,0 +1 @@
+{"wktTimestamp":"9999-12-31T23:59:60Z"}

+ 3 - 3
Sources/SwiftProtobuf/Google_Protobuf_Timestamp+Extensions.swift

@@ -167,9 +167,6 @@ private func parseTimestamp(s: String) throws -> (Int64, Int32) {
       adjusted += Int64(hourOffset) * Int64(3600)
       adjusted += Int64(minuteOffset) * Int64(60)
     }
-    if adjusted < minTimestampSeconds || adjusted > maxTimestampSeconds {
-      throw JSONDecodingError.malformedTimestamp
-    }
     seconds = adjusted
     pos += 6
   } else if value[pos] == letterZ { // "Z" indicator for UTC
@@ -181,6 +178,9 @@ private func parseTimestamp(s: String) throws -> (Int64, Int32) {
   if pos != value.count {
     throw JSONDecodingError.malformedTimestamp
   }
+  guard seconds >= minTimestampSeconds && seconds <= maxTimestampSeconds else {
+    throw JSONDecodingError.malformedTimestamp
+  }
   return (seconds, nanos)
 }
 

+ 3 - 0
Tests/SwiftProtobufTests/Test_FuzzTests.swift

@@ -121,6 +121,9 @@ final class Test_FuzzTests: XCTestCase {
     // This actually fails when the fuzzer was trying to write it back out again.
     let msg = try! SwiftProtoTesting_Fuzz_Message(jsonString: "   {\"wktAny\":  {}}  ")
     XCTAssertEqual(try! msg.jsonString(), "{\"wktAny\":{}}")
+
+    // FailCases/clusterfuzz-testcase-minimized-FuzzJSON_debug-6286338012282880
+    assertJSONFails("{\"wktTimestamp\":\"9999-12-31T23:59:60Z\"}")
   }
 
   func test_TextFormat() {