Skip to content

Commit c6db93a

Browse files
committed
Update security notes, closes #19
1 parent 64b8f38 commit c6db93a

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A simple symmetric encryption plugin for individual fields. The goal of this plu
66

77
As of the stable 1.0.0 release, this plugin works on individual fields of any type. However, 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.
88

9-
Also consider [mongoose-encryption](https://github.com/joegoldbeck/mongoose-encryption) if you have other requirements.
9+
Also consider [mongoose-encryption](https://github.com/joegoldbeck/mongoose-encryption) if you are looking to encrypt the entire document.
1010

1111
## How it works
1212

@@ -24,18 +24,23 @@ Fields which are either objects or of a different type are converted to strings
2424

2525
`npm install mongoose-field-encryption`
2626

27-
## Usage
27+
## Security Notes
28+
29+
- _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.
30+
- Additionally, store your encryption key offline somewhere safe. If you lose it, there is no way to retrieve your encrypted data.
31+
- Encrypting passwords is no substitute for appropriately hashing them. `bcrypt` is one great option. You can also encrypt the password afer hashing it although it is not necessary.
32+
- 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.
2833

29-
Keep your secret a secret. Ideally it should only live as an environment variable but definitely not stored anywhere in your repository.
34+
## Usage
3035

3136
### Basic
3237

3338
For example, given a schema as follows:
3439

3540
```js
36-
const mongoose = require('mongoose');
37-
const mongooseFieldEncryption = require('mongoose-field-encryption').fieldEncryption;
38-
const Schema = mongoose.Schema;
41+
const mongoose = require("mongoose");
42+
const mongooseFieldEncryption = require("mongoose-field-encryption").fieldEncryption;
43+
const Schema = mongoose.Schema;
3944

4045
const Post = new Schema({
4146
title: String,
@@ -46,7 +51,7 @@ const Post = new Schema({
4651
}
4752
});
4853

49-
Post.plugin(mongooseFieldEncyption, {fields: ['message', 'references'], secret: 'some secret key'});
54+
Post.plugin(mongooseFieldEncyption, { fields: ["message", "references"], secret: "some secret key" });
5055
```
5156

5257
The resulting documents will have the following format:
@@ -70,10 +75,7 @@ From the mongoose package documentation: _Note that findAndUpdate/Remove do not
7075
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:
7176

7277
```js
73-
Post.findOneAndUpdate(
74-
{ _id: postId },
75-
{ $set: { message: "snoop", __enc_message: false } }
76-
);
78+
Post.findOneAndUpdate({ _id: postId }, { $set: { message: "snoop", __enc_message: false } });
7779
```
7880

7981
The above also works for non-string fields. See changelog for more details.
@@ -122,9 +124,9 @@ const decrypted = fieldEncryption.decrypt(encrypted, 'secret')); // decrypted =
122124
### 2.0.0
123125

124126
- Use `cipheriv` instead of plain `cipher`, [#17](https://github.com/wheresvic/mongoose-field-encryption/issues/17).
125-
127+
126128
Note that this might break any _fixed_ search capability as the encrypted values are now based on a random salt.
127-
129+
128130
Also note that while this version maintains backward compatibility, i.e. decryption will automatically fall back to using the `aes-256-ctr` algorithm, any further updates will lead to the value being encrypted with the salt. In order to fully maintain backwards compatibilty, an new option `useAes256Ctr` has been introduced (default `false`), which can be set to `true` to continue using the plugin as before. It is highly recommended to start using the newer algorithm however, see issue for more details.
129131

130132
### 1.2.0

0 commit comments

Comments
 (0)