Skip to content

[Ready for payment][$250] Add some basic runtime type validation in Onyx #39852

Closed

Description

@roryabraham

coming from https://github.com/Expensify/Expensify/issues/363913...

Problem

It is possible to queue incorrect Onyx updates from Auth. Twice now, we have seen an array used in place of an object in an Onyx update in Auth, which when merged completely removes the object and replaces it with an array.

Solution

While we recognize that there a number of more elegant solutions to this (compile time type validation of Onyx updates in the back-end being a prime candidate), we should apply a simple solution to prevent simple mistakes from having catastrophic effects, as we have seen occur in production twice already.

What this means is - let's update Onyx such that:

  • it will not apply any updates where we try to merge an array into an object, or vice-versa
  • it will log an alert if we do try to do that (except on dev, of course)
Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~0152270829f1a6f929
  • Upwork Job ID: 1777413537488273408
  • Last Price Increase: 2024-04-08
Issue OwnerCurrent Issue Owner: @kadiealexander

Activity

  1. added
    ExternalAdded to denote the issue can be worked on by a contributor
    BugSomething is broken. Auto assigns a BugZero manager.
    on Apr 8, 2024
  2. self-assigned this
    on Apr 8, 2024
  3. changed the title [-]Add some basic runtime type validation in Onyx[/-] [+][$250] Add some basic runtime type validation in Onyx[/+] on Apr 8, 2024
  4. melvin-bot commented on Apr 8, 2024

    @melvin-bot
  5. added
    Help WantedApply this label when an issue is open to proposals by contributors
    on Apr 8, 2024
  6. melvin-bot commented on Apr 8, 2024

    @melvin-bot

    Triggered auto assignment to Contributor-plus team member for initial proposal review - @mananjadhav (External)

  7. melvin-bot commented on Apr 8, 2024

    @melvin-bot

    Triggered auto assignment to @kadiealexander (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details.

  8. ShridharGoel commented on Apr 8, 2024

    @ShridharGoel
    Contributor

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    We need to validate if existing items and new changes have the same type before merging in Onyx.

    What is the root cause of that problem?

    New change.

    What changes do you think we should make in order to solve the problem?

    Below is the pseudo code which needs to be added in merge method in react-native-onyx:

    OnyxUtils.get(key).then(existingValue => {
      const existingValueType = Array.isArray(existingValue) ? 'array' : 'object';
      const incomingValueType = Array.isArray(changes) ? 'array' : 'object';
      if (existingValueType !== incomingValueType) {
          // Log an alert for the mismatched types except on dev
          return Promise.resolve();
      }
    
       // Continue with the merge
    
    }).catch(exception => {
        // Log the exception here
        return Promise.resolve();
    });
           
    

    For set:

    function set<TKey extends OnyxKey>(key: TKey, value: OnyxEntry<KeyValueMapping[TKey]>): Promise<void[]> {
      OnyxUtils.get(key).then(existingValue => {
        const existingValueType = Array.isArray(existingValue) ? 'array' : 'object';
        const incomingValueType = Array.isArray(changes) ? 'array' : 'object';
    
        if (existingValueType != incomingValueType) {
            // Log an alert for the mismatched types except on dev
            return Promise.resolve();
        }
    
       // Continue with existing logic
      })
    
    }
    

    For mergeCollection, change the existingKeys logic to below:

    const existingKeys = []
    
    keys.forEach((key) => {
        if (persistedKeys.has(key)) {
            OnyxUtils.get(key).then((existingValue) => {
                const existingValueType = Array.isArray(existingValue) ? 'array' : 'object';
                const incomingValueType = Array.isArray(mergedCollection[key]) ? 'array' : 'object';
                if (existingValueType === incomingValueType) {
                    existingKeys.push(key)
                } else {
                    // Log an alert for the mismatched types except on dev
                }
            }).catch(exception => {
                // Log the exception here
            });
        }
    });
    

    For multiSet, this would be the updated method:

    function multiSet(data: Partial<NullableKeyValueMapping>): Promise<void[]> {
        const keyValuePairs = OnyxUtils.prepareKeyValuePairsForStorage(data);
    
        const updatePromises = keyValuePairs.map(([key, value]) => {
            const prevValue = cache.getValue(key, false);
    
            const existingValueType = Array.isArray(prevValue) ? 'array' : 'object';
            const incomingValueType = Array.isArray(value) ? 'array' : 'object';
            if (existingValueType !== incomingValueType) {
                // Log an alert for the mismatched types except on dev
                return null
            }
    
            // Update cache and optimistically inform subscribers on the next tick
            cache.set(key, value);
            return OnyxUtils.scheduleSubscriberUpdate(key, value, prevValue);
        });
    
        return Storage.multiSet(keyValuePairs)
            .catch((error) => OnyxUtils.evictStorageAndRetry(error, multiSet, data))
            .then(() => {
                OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.MULTI_SET, undefined, data);
                return Promise.all(updatePromises.filter(promise => promise !== null));
            });
    }
    
  9. mananjadhav commented on Apr 8, 2024

    @mananjadhav
    Collaborator

    @ShridharGoel Wouldn't we need to check if it's an Array for the existingValue as well? I am assuming we're using the existing block with OnyxUtils.get(key).then(existingValue.

    @roryabraham

    1. The following is true for all/any environment right?

    it will not apply any updates where we try to merge an array into an object, or vice-versa

    1. Would Logger.logAlert suffice for the mismatch? do we need any sendActionToDevTools?
  10. ShridharGoel commented on Apr 9, 2024

    @ShridharGoel
    Contributor

    Proposal

    Updated to use similar check for existing value and changes.

  11. dominictb commented on Apr 9, 2024

    @dominictb
    Contributor

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    We want to:

    • It will not apply any updates where we try to merge an array into an object, or vice-versa
    • It will log an alert if we do try to do that (except on dev, of course)

    What is the root cause of that problem?

    We don't have such functionality in Onyx yet

    What changes do you think we should make in order to solve the problem?

    A. Updates for Onyx.merge:

    First there're a few considerations:

    • There're 3 cases possible for a change:
      • Nullish (null, undefined)
      • Object
      • Array
        If existing value is Array, it can be used with Nullish/Array change, but not Object change. If existing value is Object, it can be used with Nullish/Object change, but not Array change.

    So just checking Array.isArray and compare both is not enough, we need to check the Nullish case as well.

    We can define a method isUpdateCompatibleWithExistingValue to validate that the update and the existing value are compatible:

    function isUpdateCompatibleWithExistingValue(value, existingValue): boolean {
        if (existingValue && value && Array.isArray(existingValue) !== Array.isArray(value)) {
            return false;
        }
        return true;
    }
    
    • The changes are batched into an array, we should only remove the invalid changes, not discard the full batch just because one of the update happens to be of the wrong type.

    With the above considerations, here're the pseudo code of the changes we can make in Onyx here to filter out the invalid change, Log an alert, and check the change type compatibility properly:

    const validChanges = mergeQueue[key].filter((change) => isUpdateCompatibleWithExistingValue(change, existingValue));
    
    if (validChanges.length !== mergeQueue[key].length) {
        Logger.logAlert(`A warning occurred while applying merge for key: ${key}, Warning: Trying to merge array to object or vice versa`);
    }
    
    if (!validChanges.length) {
        return undefined;
    }
    

    Then below that, when we use mergeQueue[key] like in here, we should change to use validChanges instead.

    B. Updates for Onyx.set
    In here, we should get the existing value, then check if the new update is compatible with the existing value, if not, early return.
    (we can alternatively get the existing value from cache.getValue so it can be synchronous)

    OnyxUtils.get(key).then(existingValue => {
            if (!isUpdateCompatibleWithExistingValue(value, existingValue)) {
                Logger.logAlert(`A warning occurred while applying set for key: ${key}, Warning: Trying to set array to object or vice versa`);
                return Promise.resolve();
            }
            
            // now we do the Onyx.set as now
    

    C. Updates for Onyx.mergeCollection

    • In here, modify so getCachedCollection accepts an optional collectionMemberKeys param, if it's passed in, we'll use it, if not, we'll use our own existing collectionMemberKeys here
    • In here, get the collection value of existingKeys by using getCachedCollection and pass existingKeys as collectionMemberKeys
    • In here, we check that mergedCollection[key] is compatible with the existing value from existingKeys above, if they are not compatible, Log a warning by using Logger.logAlert, and do not set to obj (so that update will be ignored)

    What alternative solutions did you explore? (Optional)

    For A., an alternative is to check update compatibility right in here before we push the changes to the mergeQueue. We can get the existing value by using cache.getValue and check the compatibility using isUpdateCompatibleWithExistingValue, if they are not compatible, log a warning using Logger.logAlert and early return, so the changes will never end up in the merge queue.

  12. 66 remaining items

  13. roryabraham commented on Jun 20, 2024

    @roryabraham
    ContributorAuthor

    Ok, so this is definitely out in production right now, and I think it was deployed in #42772.

    So that means it's been on prod for about a week. Given the unrelated delays, I suggest we pay this out right away and then close this.

  14. mananjadhav commented on Jun 22, 2024

    @mananjadhav
    Collaborator

    @kadiealexander this is ready for payout. Can you update the payment summary?

  15. kadiealexander commented on Jun 24, 2024

    @kadiealexander
    Contributor

    Payouts due:

    Upwork job is here.

    @dominictb please apply on Upwork, I couldn't find your account.

  16. changed the title [-][$250] Add some basic runtime type validation in Onyx[/-] [+][Ready for payment][$250] Add some basic runtime type validation in Onyx[/+] on Jun 24, 2024
  17. mananjadhav commented on Jun 24, 2024

    @mananjadhav
    Collaborator

    @kadiealexander I don't need the Upwork payout. I have requested on NewDot.

  18. kadiealexander commented on Jun 24, 2024

    @kadiealexander
    Contributor

    Withdrawn, thanks Manan!

  19. dominictb commented on Jun 24, 2024

    @dominictb
    Contributor

    @kadiealexander I submitted the application on Upwork, my profile link is https://www.upwork.com/freelancers/~01f70bed1934fd35d5

    Thank you

  20. JmillsExpensify commented on Jun 24, 2024

    @JmillsExpensify
    Contributor

    $250 approved for @mananjadhav

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

Metadata

Metadata

Labels

BugSomething is broken. Auto assigns a BugZero manager.DailyKSv2ExternalAdded to denote the issue can be worked on by a contributor

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions