A simple symmetric encryption plugin for individual fields. The goal of this plugin is to encrypt data but still allow searching over fields with string values. This plugin relies on the Node
crypto module. Encryption and decryption happen transparently during save and find.
While this plugin works on individual fields of any type, note that for non-string fields, the original value is set to undefined after encryption. This is because if the schema has defined a field as an array, it would not be possible to replace it with a string value.
As of the stable 2.3.0 release, this plugin requires provision of a custom salt generation function (which would always provide a constant salt given the secret) in order to retain symmetric decryption capability.
Also consider mongoose-encryption if you are looking to encrypt the entire document.
How it works
Encryption is performed using
AES-256-CBC. To encrypt, the relevant fields are encrypted with the provided secret + random salt (or a custom salt via the provided
saltGenerator function). The generated salt and the resulting encrypted value is concatenated together using a
: character and the final string is put in place of the actual value for
string values. An extra
boolean field with the prefix
__enc_ is added to the document which indicates if the provided field is encrypted or not.
Fields which are either objects or of a different type are converted to strings using
JSON.stringify and the value stored in an extra marker field of type
string with a naming scheme of
__enc_ as prefix and
_d as suffix on the original field name. The original field is then set to
undefined. Please note that this might break any custom validation and application of this plugin on non-string fields needs to be done with care.
>=4.4.7 && <=6.x.x)
npm install mongoose-field-encryption --save-exact
- Always store your keys and secrets outside of version control and separate from your database. An environment variable on your application server works well for this.
- Additionally, store your encryption key offline somewhere safe. If you lose it, there is no way to retrieve your encrypted data.
- Encrypting passwords is no substitute for appropriately hashing them.
bcryptis one great option. You can also encrypt the password afer hashing it although it is not necessary.
- If an attacker gains access to your application server, they likely have access to both the database and the key. At that point, neither encryption nor authentication do you any good.
For example, given a schema as follows:
const mongoose = ;const mongooseFieldEncryption = fieldEncryption;const Schema = mongooseSchema;const PostSchema =title: Stringmessage: Stringreferences:author: Stringdate: Date;PostSchema;const Post = mongoose;const post = title: "some text" message: "hello all" ;post;
The resulting documents will have the following format:
_id: ObjectIdtitle: Stringmessage: String // encrypted salt and hex value as string, e.g. 9d6a0ca4ac2c80fc84df0a06de36b548:cee57185fed78c055ed31ca6a8be9bf20d303283200a280d0f4fc8a92902e0c1__enc_message: true // boolean marking if the field is encrypted or notreferences: undefined // encrypted object set to undefined__enc_references: true // boolean marking if the field is encrypted or not__enc_references_d: String // encrypted salt and hex object value as string, e.g. 6df2171f25fd1d32adc4a4059f867a82:5909152856cf9cdb7dc32c6af321c8fe69390c359c6b19d967eaa6e7a0a97216
find works transparently and you can make new documents as normal, but you should not use the
lean option on a find if you want the fields of the document to be decrypted.
save also all work as normal.
update works only for string fields and you would also need to manually set the
__enc_ field value to false if you're updating an encrypted field.
From the mongoose package documentation: Note that findAndUpdate/Remove do not execute any hooks or validation before making the change in the database. If you need hooks and validation, first query for the document and then save it.
Note that as of
1.2.0 release, support for
findOneAndUpdate has also been added. Note that you would need to specifically set the encryption field marker for it to be encrypted. For example:
The above also works for non-string fields. See changelog for more details.
Also note that if you manually set the value
__enc_ prefix field to true then the encryption is not run on the corresponding field and this may result in the plain value being stored in the db.
Search over encrypted fields
Note that in order to use this option a fixed salt generator must be provided. See example as follows:
const messageSchema =title: Stringmessage: Stringname: String;messageSchema;const title = "some text";const name = "victor";const message = "hello all";const Message = mongoose;const messageToSave = title message name ;await messageToSave;// note that we are only providing the field we would like to search withconst messageToSearchWith = name ;messageToSearchWith;// `messageToSearchWith.name` contains the encrypted string textconst results = await Message;// results is an array of length 1 (assuming that there is only 1 message with the name "victor" in the collection)// and the message in the results array corresponds to the one saved previously
fields(required): an array list of the required fields
secret(required): a string cipher which is used to encrypt the data (don't lose this!)
false): a boolean indicating whether the older
aes-256-ctralgorithm should be used. Note that this is strictly a backwards compatibility feature and for new installations it is recommended to leave this at default.
const defaultSaltGenerator = secret => crypto.randomBytes(16);): a function that should return either a
utf-8encoded string that is 16 characters in length or a
Bufferof length 16. This function is also passed the secret as shown in the default function example.
For performance reasons, once the document has been encrypted, it remains so. The following methods are thus added to the schema:
encryptFieldsSync(): synchronous call that encrypts all fields as given by the plugin options
decryptFieldsSync(): synchronous call that decrypts encrypted fields as given by the plugin options
stripEncryptionFieldMarkers(): synchronous call that removes the encryption field markers (useful for returning documents without letting the user know that something was encrypted)
Multiple calls to the above methods have no effect, i.e. once a field is encrypted and the
__enc_ marker field value is set to true then the ecrypt operation is ignored. Same for the decrypt operation. Of course if the field markers have been removed via the
stripEncryptionFieldMarkers() call, then the encryption will be executed if invoked.
To enable searching over the encrypted fields the
decrypt methods have also been exposed.
const fieldEncryption =const encrypted = fieldEncryption);const decrypted = fieldEncryption); // decrypted = 'some text'
As of version 3.0.5, one can setup a local development mongodb instance using docker:
docker-compose upin the
Feel free to make changes to the default docker configuration as required.
- Install dependencies with
npm installand install mongo if you don't have it yet.
- Start mongo via
docker-compose upunder the
- Run tests with
npm run test:auth. Additionally you can pass your own mongodb uri as an environment variable if you would like to test against your own database, for e.g.
URI='mongodb://username:email@example.com:27017/mongoose-field-encryption-test' npm test
npm version patch,minor,major
3.0.1, 3.0.2, 3.0.3, 3.0.4, 3.0.5, 3.0.6
- Update development dependencies, fix unit tests, no functionality affected
- Add development db via docker (3.0.5)
- BREAKING: Drop Node 4 support
- Update development dependencies, no functionality affected
2.3.2, 2.3.3, 2.3.4
- Update documentation, no functionality affected
- Update documentation, no functionality affected
- FEATURE: Add provision for a custom salt generator, PR #27. Note that by using a custom salt, fixed search capability is now restored.
- Update dependencies, no functionality affected
- FIX: Fix bug where decryption fails when the field in question is not retrieved, PR #26.
- FIX: Fix bug where data was not getting decrypted on a
cipherivinstead of plain
Note that this might break any fixed search capability as the encrypted values are now based on a random salt.
Also note that while this version maintains backward compatibility, i.e. decryption will automatically fall back to using the
aes-256-ctralgorithm, any further updates will lead to the value being encrypted with the salt. In order to fully maintain backwards compatibilty, an new option
useAes256Ctrhas been introduced (default
false), which can be set to
trueto continue using the plugin as before. It is highly recommended to start using the newer algorithm however, see issue for more details.
- FEATURE: Added support for
- FEATURE: Added support for mongoose 5 https://github.com/wheresvic/mongoose-field-encryption/pull/16.
- FIX: Removed mongoose dependency, moved to
- Formatted source code using prettier.