Skip to content

Fix crash when receiving an incorrect json object parsing Meta campaigns #1492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

fjmorant
Copy link

@fjmorant fjmorant commented May 20, 2025

Reference

SDK-XXXX -- <TITLE>.

Summary

This PR solves the issue described in this GitHub issue #1491

Motivation

We are receiving this crash when opening the app from a Facebook Ad

         Fatal Exception: NSInvalidArgumentException
         -[__NSArray0 objectForKey:]: unrecognized selector sent to instance 0x2054a4360
0  CoreFoundation                 0x11a2ec __exceptionPreprocess
1  libobjc.A.dylib                0x31a7c objc_exception_throw
2  CoreFoundation                 0x18467c +[NSObject(NSObject) _copyDescription]
3  CoreFoundation                 0x31b84 ___forwarding___
4  CoreFoundation                 0x314f0 _CF_forwarding_prep_0
5  yachtworld                     0x18620 -[BNCReferringURLUtility metaCampaignIDsFromDictionary:] + 104 (BNCReferringURLUtility.m:104)
6  yachtworld                     0x184c4 -[BNCReferringURLUtility processMetaQueryParameter:] + 89 (BNCReferringURLUtility.m:89)
7  yachtworld                     0x181f8 -[BNCReferringURLUtility parseReferringURL:] + 66 (BNCReferringURLUtility.m:66)
8  yachtworld                     0x28a9c -[Branch handleDeepLink:sceneIdentifier:] + 771 (Branch.m:771)
9  yachtworld                     0x29290 -[Branch application:openURL:options:] + 854 (Branch.m:854)
10 yachtworld                     0x289f3c +[RNBranch application:openURL:options:] + 186 (RNBranch.m:186)
11 yachtworld                     0x8e70 @objc AppDelegate.application(_:open:options:) + 52 (AppDelegate.swift:52)
12 yachtworld                     0x1e345c -[GULAppDelegateSwizzler application:openURL:options:] + 716 (GULAppDelegateSwizzler.m:716)
13 UIKitCore                      0x11880a4 __58-[UIApplication _applicationOpenURLAction:payload:origin:]_block_invoke
14 UIKitCore                      0x1187af8 -[UIApplication _applicationOpenURLAction:payload:origin:]
15 UIKitCore                      0x163a3b8 -[_UIOpenURLApplicationBSActionHandler _respondToApplicationActions:fromTransitionContext:]
16 UIKitCore                      0x57db0c -[UIApplication _handleNonLaunchSpecificActions:forScene:withTransitionContext:completion:]
17 UIKitCore                      0x230d34 -[UIScene _emitSceneSettingsUpdateResponseForCompletion:afterSceneUpdateWork:]
18 UIKitCore                      0x1dc764 -[UIScene scene:didUpdateWithDiff:transitionContext:completion:]
19 UIKitCore                      0x1dc5a4 -[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:]
20 FrontBoardServices             0x6414 __76-[FBSScene updater:didUpdateSettings:withDiff:transitionContext:completion:]_block_invoke.146
21 FrontBoardServices             0x62d8 -[FBSScene _callOutQueue_coalesceClientSettingsUpdates:]

Type Of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Testing Instructions

cc @BranchMetrics/saas-sdk-devs for visibility.

Copy link
Contributor

Code Quality bug fix

Summary By MatterAI MatterAI logo

🔄 What Changed

This PR fixes a crash that occurs when the Branch SDK receives an incorrect JSON object while parsing Meta campaigns. The changes add proper validation checks to ensure that:

  1. The input JSON is a valid dictionary
  2. The 'campaign_ids' value is a string before attempting to use it

🔍 Impact of the Change

This change improves the robustness of the Branch SDK by preventing crashes when handling malformed or unexpected data from Meta campaigns. The code now gracefully returns nil instead of attempting to process invalid data, which would previously lead to runtime crashes.

📁 Total Files Changed

1 file modified:

  • Sources/BranchSDK/BNCReferringURLUtility.m (+8, -4)

🧪 Test Added

No explicit tests were added in this PR. Ideally, unit tests should be added to verify the handling of various invalid inputs to ensure the fix works as expected.

🔒 Security Vulnerabilities

No security vulnerabilities were introduced. In fact, this change improves security by properly validating input data, which helps prevent potential crashes that could be triggered by malicious or malformed inputs.

Sequence Diagram

sequenceDiagram
    participant Client as Client Code
    participant BNCReferringURLUtility as BNCReferringURLUtility
    participant JSON as JSON Object
    
    Client->>BNCReferringURLUtility: metaCampaignIDsFromDictionary(json)
    Note over BNCReferringURLUtility: Check if json is valid dictionary
    alt json is nil or not a dictionary
        BNCReferringURLUtility-->>Client: return nil
    else json is valid
        BNCReferringURLUtility->>JSON: objectForKey:@"campaign_ids"
        JSON-->>BNCReferringURLUtility: value
        Note over BNCReferringURLUtility: Check if value is a string
        alt value is not a string
            BNCReferringURLUtility-->>Client: return nil
        else value is a string
            BNCReferringURLUtility-->>Client: return value as NSString
        end
    end
Loading

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.

1 participant