Repository navigation
fix(sessions): harden parsing of server-provided settings - #16752
Conversation
Drop non-property-list values (e.g. NSNull from JSON null) before caching settings in user defaults, which could raise an exception or silently fail to persist. Ignore out-of-range sampling_rate, session_timeout_seconds and cache_duration values so SDK defaults are used instead. (#16728)
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request improves the robustness of Firebase Sessions when parsing and caching remote settings. It introduces sanitization to filter out non-property-list values (such as NSNull) from server-provided JSON before caching, and adds validation checks for sampling rate, session timeout, and cache duration to ensure invalid values are ignored in favor of SDK defaults. Comprehensive unit tests have also been added to cover these scenarios. The review feedback suggests using explicit Double literals (0.0 ... 1.0) for the sampling rate range check to avoid implicit type conversion, and explicitly checking for 'is Bool' in the property list compatibility helper to improve code readability.
Addresses review feedback: use explicit Double literals for the sampling rate range and list Bool explicitly among property-list-compatible types.
Context: #16728. In that issue, a malformed server payload crashed apps with no
client change. This PR applies the same kind of hardening to the settings that
Firebase Sessions gets from
firebase-settings.crashlytics.comand caches inuser defaults.
Hardened code paths
SettingsCache.updateContents:JSONSerializationturns JSONnullintoNSNull, which isn't a property list value. Before this change, the rawresponse was written to user defaults as-is:
GULUserDefaultsonly checks the top-level type. A nestedNSNullthenreaches
-[NSUserDefaults setObject:forKey:], which crashed when I triedit in a standalone macOS script.
without an error. Settings are lost on relaunch, and they are fetched
again on every launch.
Values that can't go in a property list are now dropped recursively before
anything is cached. Memory and disk get the same sanitized contents.
RemoteSettings.samplingRate: values outside[0, 1], including NaN andinfinity, are ignored and the SDK default is used.
RemoteSettings.sessionTimeout: negative or non-finite values are ignoredand the SDK default is used.
SettingsCache.cacheDuration: negative or non-finitecache_durationvalues fall back to the default of 1h. Before, a negative value marked the
cache expired on every session event, so settings were fetched over and over.
A NaN value would have kept the cache from ever expiring.
Valid payloads behave the same as before. Values with the wrong type were
already handled safely (they resolve to
nil).Tests
Added to
RemoteSettingsTestsandSessionsSettingsTests:test_nullValuesInResponse_areDroppedAndValidValuesPersisted: fails beforethe fix (with GoogleUtilities 8.1.4 nothing is persisted, so a force-unwrap
hits
nilafter the simulated relaunch).test_outOfRangeValues_returnNilSettings: fails before the fix.test_negativeCacheDuration_usesDefaultCacheDuration: fails before the fix.test_InvalidRemoteAndDefaultsPresent_DefaultConfigsApply: fails before thefix.
test_boundaryValues_areReturned,test_wrongTypesInResponse_returnNilSettings,test_namespaceWithWrongType_returnsNilSettings,test_cacheContentWithWrongType_returnsNilSettings,test_cacheKeyWithWrongSchema_fetchNewSettings: regression coverage. Thesealready passed before the fix.
Testing
xcodebuild -scheme FirebaseSessionsUnit test -destination 'platform=macOS':73 tests, 0 failures. I also ran the new tests against the unfixed sources and
confirmed the failures listed above.