couchbaselabs / ToDoLite-iOS

To-Do list sample app for Couchbase Lite, native iOS version
85 stars 41 forks source link

App can create invalid documents that cause map function to throw #60

Open snej opened 8 years ago

snej commented 8 years ago

It's possible for ToDoLite to create invalid documents with a type property but no other properties. These then trigger exceptions in the view map functions, which use properties like list_id that shouldn't be nil. (See couchbase/couchbase-lite-ios#1071).

The bug is in Titled.m:

- (void)awakeFromInitializer {
    self.type = [[self class] docType];
    if (!self.created_at)
        self.created_at = [NSDate date];
}

The steps that trigger the bug:

  1. Load a CBLDocument that doesn't exist yet (e.g. get a doc with an ID that's not in the db)
  2. Call [Task modelForDocument: doc] to instantiate a new Task object. When the Task awakes its type and created_at get set, but no other properties.
  3. Save the model. The document is now in the database with a missing list_id.

I'm not sure how step 1 happened, but the rest is pretty clear. The fix is to take out this method and set the type and created_at only when explicitly creating a new model.

snej commented 8 years ago

It might also be a good idea to add a validation function. Because even if the bug is fixed locally, if an earlier version of the app puts an invalid document in the database it'll get pushed to the server and then replicated to other clients that will then barf on it. A validation function would block it from being received by the other clients. (I had to do this to Grocery Sync a long time ago in response to an old version of the Android version of the app storing dates in an invalid format.)