Skip to content

fix: Don't crash when decoding an out-of-range Timestamp string - #123

Merged
aashishpatil-g merged 4 commits into
firebase:mainfrom
dmorfav:fix/timestamp-malformed-string-crash
Oct 21, 2026
Merged

aashishpatil-g merged 4 commits into
firebase:mainfrom
dmorfav:fix/timestamp-malformed-string-crash

Conversation

@dmorfav

@dmorfav dmorfav commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Fixes firebase/firebase-ios-sdk#16888

Timestamp.init(from:) checks the string format with a regex but not the ranges, then parses it with try! Google_Protobuf_Timestamp(jsonString:). Strings like "2026-13-01T00:00:00Z" pass the regex, SwiftProtobuf rejects them, and the try! crashes the process. Since AnyCodableValue tries Timestamp first for every JSON string, one String field with such a value crashes the app while it caches the server response, and also when AnyValue decodes it.

Change

  • CodableTimestamp: try! → try in init(from:), which already throws. Decoding as Timestamp now throws, and AnyCodableValue falls back to .string through its existing try?.

Tests

TimestampCodableTests gets two new tests, with six values that match the regex but are out of range:

  • testOutOfRangeValuesShouldThrow
  • testOutOfRangeValuesInAnyValueAreKeptAsStrings

Without the fix, both crash with Fatal error: 'try!' expression unexpectedly raised an error. With the fix, swift test --filter FirebaseDataConnectUnit passes (82 tests), and so does scripts/style.sh test-only.

This PR is independent of #122: the two touch different lines. The only overlap is the # Unreleased heading in the CHANGELOG.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes a crash when decoding out-of-range timestamp strings by replacing try! with try during parsing, and adds corresponding unit tests. The review feedback suggests wrapping the raw SwiftProtobuf error in a domain-specific DataConnectCodecError.invalidTimestampFormat error to avoid leaking implementation details of the underlying library.

Comment thread Sources/Internal/CodableTimestamp.swift Outdated
@aashishpatil-g

Copy link
Copy Markdown
Collaborator

LGTM

@aashishpatil-g

Copy link
Copy Markdown
Collaborator

Thanks for the fix PR @dmorfav !!

@dmorfav

dmorfav commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the fix PR @dmorfav !!

Thanks for the review! The workflow runs (check, spm) are waiting for maintainer approval since this is my first contribution from a fork.

@dmorfav

dmorfav commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor Author

@aashishpatil-g the CI failure is unrelated to this change: spm-package-resolved fails resolving firebase-ios-sdk (it requires swift-tools 6.2.1, the runner has 6.1.0). The scheduled spm run on main has been failing the same way since 2026-09-25. Happy to rebase once CI is fixed, or feel free to merge if that check isn't required.

FYI @paulb777

@aashishpatil-g

Copy link
Copy Markdown
Collaborator

Yes, the CI failure is unrelated.

@dmorfav

dmorfav commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

I’m keeping an eye on PR #124 so I can update mine once it’s merged.

@ncooke3 ncooke3 closed this Oct 19, 2026
@ncooke3 ncooke3 reopened this Oct 19, 2026
@dmorfav

dmorfav commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

@aashishpatil-g this PR is also green and ready for review!

@aashishpatil-g

Copy link
Copy Markdown
Collaborator

LGTM - resolved the CHANGELOG conflict.

@aashishpatil-g
aashishpatil-g merged commit 35ad3e4 into firebase:main Oct 21, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data Connect crashes when decoding a String that matches the Timestamp format but is out of range (e.g. "2026-13-01T00:00:00Z")

3 participants