Skip to content

Invalid cast in GTMSessionFetcherService #190

Open

Description

Hello

This issue was described in several tickets but solution is still not there.

There are several third-party libraries that swizzle URLSession's delegate with their own proxy objects. For example TrustKit and SplunkMint do that. However GTMSessionFetcherService completely ignores this fact and hard-casts these proxy objects to its own class - GTMSessionFetcherSessionDelegateDispatcher:

return (GTMSessionFetcherSessionDelegateDispatcher *)fetcherDelegate;

    BOOL hasDispatcher = (fetcherDelegate != nil &&
                          ![fetcherDelegate isKindOfClass:[GTMSessionFetcher class]]);
    if (hasDispatcher) {
      GTMSESSION_ASSERT_DEBUG([fetcherDelegate isKindOfClass:[GTMSessionFetcherSessionDelegateDispatcher class]],
                              @"Fetcher delegate class: %@", [fetcherDelegate class]);
      return (GTMSessionFetcherSessionDelegateDispatcher *)fetcherDelegate;
    }

Obviously later we get crashes in different places due to "unrecognized selector sent to instance" exceptions.

The "dumb" solution is to check the class of object before casting it:

      if ([fetcherDelegate isKindOfClass:[GTMSessionFetcherSessionDelegateDispatcher class]]) {
        return (GTMSessionFetcherSessionDelegateDispatcher *)fetcherDelegate;
      }
      else {
        return nil;
      }

But having almost zero knowledge of how GTMSessionFetcher works, it's hard for me to understand if this code breaks something else. Any advice from the team?

We consume this library indirectly via FirebaseAuth.

Thanks,
Vlad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions