Skip to content

fix(sessions): harden parsing of server-provided settings - #16752

Merged
paulb777 merged 2 commits into
mainfrom
pb-harden-sessions-settings
Oct 26, 2026
Merged

paulb777 merged 2 commits into
mainfrom
pb-harden-sessions-settings

Conversation

@paulb777

@paulb777 paulb777 commented Oct 1, 2026

Copy link
Copy Markdown
Member

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.com and caches in
user defaults.

Hardened code paths

  • SettingsCache.updateContents: JSONSerialization turns JSON null into
    NSNull, which isn't a property list value. Before this change, the raw
    response was written to user defaults as-is:

    • With GoogleUtilities 8.1.3 (our minimum supported version),
      GULUserDefaults only checks the top-level type. A nested NSNull then
      reaches -[NSUserDefaults setObject:forKey:], which crashed when I tried
      it in a standalone macOS script.
    • With GoogleUtilities 8.1.4 or later, the whole dictionary is rejected
      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 and
    infinity, are ignored and the SDK default is used.

  • RemoteSettings.sessionTimeout: negative or non-finite values are ignored
    and the SDK default is used.

  • SettingsCache.cacheDuration: negative or non-finite cache_duration
    values 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 RemoteSettingsTests and SessionsSettingsTests:

  • test_nullValuesInResponse_areDroppedAndValidValuesPersisted: fails before
    the fix (with GoogleUtilities 8.1.4 nothing is persisted, so a force-unwrap
    hits nil after the simulated relaunch).
  • test_outOfRangeValues_returnNilSettings: fails before the fix.
  • test_negativeCacheDuration_usesDefaultCacheDuration: fails before the fix.
  • test_InvalidRemoteAndDefaultsPresent_DefaultConfigsApply: fails before the
    fix.
  • test_boundaryValues_areReturned, test_wrongTypesInResponse_returnNilSettings,
    test_namespaceWithWrongType_returnsNilSettings,
    test_cacheContentWithWrongType_returnsNilSettings,
    test_cacheKeyWithWrongSchema_fetchNewSettings: regression coverage. These
    already 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.

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)
@gemini-code-assist

Copy link
Copy Markdown
Contributor
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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.

@paulb777

paulb777 commented Oct 1, 2026

Copy link
Copy Markdown
Member Author

/gemini review

@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 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.
@paulb777 paulb777 added this to the 13.1.0 - M188 milestone Oct 1, 2026
@paulb777
paulb777 merged commit 75e1ca7 into main Oct 26, 2026
117 of 118 checks passed
@paulb777
paulb777 deleted the pb-harden-sessions-settings branch October 26, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants