rxdb: Getting 'Unexpected end of JSON input' exception in encryption.js

Case

bug

Issue

Getting the following exception:

encryption.js:25 Uncaught (in promise) SyntaxError: Unexpected end of JSON input
    at Object.parse (<anonymous>)
    at Crypter._decryptValue (encryption.js:25)
    at crypter.js:62
    at Array.map (<anonymous>)
    at Crypter.decrypt (crypter.js:59)
    at RxCollection._handleFromPouch (rx-collection.js:192)
    at rx-collection.js:314
    at Array.map (<anonymous>)
    at rx-collection.js:313

Not exactly sure which part of my code triggers it, since the stack trace references none of my files.

Info

  • Environment: browser
  • Adapter: IndexedDB
  • Stack: React

Code

Here’s my schema:

const schema = {
  title: 'User profile schema',
  description: 'Database schema for the profile of a user',
  version: 0,
  type: 'object',
  properties: {
    peerID: {
      type: 'string',
      primary: true,
    },
    handle: {
      type: 'string',
      encrypted: true,
    },    
    name: {
      type: 'string',
      encrypted: true,
    },
    location: {
      type: 'string',
      encrypted: true,
    },
    about: {
      type: 'string',
      encrypted: true,
    },
    shortDescription: {
      type: 'string',
      encrypted: true,
    },
    nsfw: {
      type: 'boolean',
      encrypted: true,
    },
    vendor: {
      type: 'boolean',
      encrypted: true,
    },
    moderator: {
      type: 'boolean',
      encrypted: true,
    },
    // Will work this in later
    moderatorInfo: {
      type: ['object', 'null'],
      encrypted: true,
    },
    contactInfo: {
      type: ['object', 'null'],
      encrypted: true,
      properties: {
        website: {
          type: 'string',
        },
        email: {
          type: 'string',
        },
        phoneNumber: {
          type: 'string',
        },
        social: {
          type: 'array',
          uniqueItems: true,
          items: {
            type: 'object',
            properties: {
              type: {
                type: 'string'
              },
              username: {
                type: 'string'
              },
              proof: {
                type: 'string'
              },
            }
          }
        }
      }
    },
    colors: {
      type: ['object', 'null'],
      encrypted: true,
      properties: {
        primary: {
          type: 'string',
        },
        secondary: {
          type: 'string',
        },
        text: {
          type: 'string',
        },
        highlight: {
          type: 'string',
        },
        highlightText: {
          type: 'string',
        },
      }
    },
    avatarHashes: {
      type: ['object', 'null'],
      encrypted: true,
      properties: {
        tiny: {
          type: 'string'
        },
        small: {
          type: 'string'
        },
        medium: {
          type: 'string'
        },
        large: {
          type: 'string'
        },
        original: {
          type: 'string'
        },
      }
    },    
  },
  required: ['peerID', 'name']
};

Here’s a dump of my db:

{
  "name": "a0e62fc96f1ca251998d512db1353c21f008d8c071b5c46a6212f9ab8e854c1c9",
  "instanceToken": "oodzoxksdi",
  "encrypted": true,
  "passwordHash": "a76bb4891fe30596c51e410f63e42bd9",
  "collections": [
    {
      "name": "profile",
      "schemaHash": "132ae4ac54c7552c18d7e47881deca12",
      "encrypted": true,
      "passwordHash": "a76bb4891fe30596c51e410f63e42bd9",
      "docs": [
        {
          "moderatorInfo": "U2FsdGVkX1+/wnlXNw6+M43WciG7cJLSWGAMFZswKiM=",
          "contactInfo": "U2FsdGVkX18vJeCWXulo+yzOH8LZvo/116TZz2G44xg=",
          "colors": "U2FsdGVkX19NDXFNWacnl6lhnIrvWZWVj1tCbxKyuik=",
          "avatarHashes": "U2FsdGVkX19H+VaarclSvJrfKQAENQ0UNM3N/zsmBIg=",
          "name": "U2FsdGVkX1/FV1R+rnIAokULhcZJLl+gkvcstrtgzgU=",
          "shortDescription": "U2FsdGVkX19oniIoqdi2wfrzfTtooaKoVhRGIWvTDRI=",
          "handle": "U2FsdGVkX1+ZlspmtKqfPLQiQ69poS6Yus5GE6MUHt4=",
          "location": "U2FsdGVkX19RpHIltTXvMcKJIX5mAJxiHgoMLbGA+sE=",
          "about": "U2FsdGVkX184Sc8xWQSI00mE/TwlPeiZSZi9/XxGw0o=",
          "nsfw": "U2FsdGVkX1+lfcIIxHwN6vvMHUiEPCMH33ux2frXpnI=",
          "vendor": "U2FsdGVkX1+nqr63Th0KW8qdgjzrZCTC+k+ytIdye4w=",
          "moderator": "U2FsdGVkX19qmzgLZJ2lO6b51u9C85Rt8VlXWlRB9oc=",
          "peerID": "QmRHvW41Ga6wQuayQNbrhwrTghasabxwWwqmT5hrWXQLoj"
        }
      ]
    }
  ]
}

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (7 by maintainers)

Most upvoted comments

@pubkey Here’s a test that reproduces this one.

https://github.com/rmisio/rxdb/blob/master/test/unit/encrypt-bug-917.test.js

I’ve found the issue is that it throws this exception on the read of an encrypted field of type boolean or string (maybe others, haven’t tried) which both doesn’t have a default declared in the schema and was not passed in a value when saving.

So, it does let you save without issue, just chokes on a subsequent read.