From 5e32b4004c45b37c7df178a7bc5223c13a25e4d6 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Mon, 29 Jul 2024 16:48:39 -0400 Subject: [PATCH] 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. --- ...rfuzz-testcase-minimized-FuzzJSON_debug-6286338012282880 | 1 + .../Google_Protobuf_Timestamp+Extensions.swift | 6 +++--- Tests/SwiftProtobufTests/Test_FuzzTests.swift | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 FuzzTesting/FailCases/clusterfuzz-testcase-minimized-FuzzJSON_debug-6286338012282880 diff --git a/FuzzTesting/FailCases/clusterfuzz-testcase-minimized-FuzzJSON_debug-6286338012282880 b/FuzzTesting/FailCases/clusterfuzz-testcase-minimized-FuzzJSON_debug-6286338012282880 new file mode 100644 index 000000000..064746ff3 --- /dev/null +++ b/FuzzTesting/FailCases/clusterfuzz-testcase-minimized-FuzzJSON_debug-6286338012282880 @@ -0,0 +1 @@ +{"wktTimestamp":"9999-12-31T23:59:60Z"} \ No newline at end of file diff --git a/Sources/SwiftProtobuf/Google_Protobuf_Timestamp+Extensions.swift b/Sources/SwiftProtobuf/Google_Protobuf_Timestamp+Extensions.swift index ab0a68b87..cad7355f2 100644 --- a/Sources/SwiftProtobuf/Google_Protobuf_Timestamp+Extensions.swift +++ b/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) } diff --git a/Tests/SwiftProtobufTests/Test_FuzzTests.swift b/Tests/SwiftProtobufTests/Test_FuzzTests.swift index 23ffd0f1b..463c7a056 100644 --- a/Tests/SwiftProtobufTests/Test_FuzzTests.swift +++ b/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() {