Skip to content

Commit

Permalink
Range check timestamps on all parsing paths.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thomasvl committed Jul 29, 2024
1 parent 3a38104 commit 5e32b40
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"wktTimestamp":"9999-12-31T23:59:60Z"}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down
3 changes: 3 additions & 0 deletions Tests/SwiftProtobufTests/Test_FuzzTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 5e32b40

Please sign in to comment.