@@ -8,19 +8,17 @@ Encrypted model attributes in your favourite ORM.
8
8
9
9
# Security model
10
10
11
- * AES-256-GCM
12
- * 96-bit random nonce
13
- * 128-bit authentication tag
11
+ * AES-256-GCM:
12
+ * 96-bit random nonce
13
+ * 128-bit authentication tag
14
14
* Additional authenticated data:
15
- * Key id, allowing use of different keys for different attributes, or
16
- rotating keys over time without re-encrypting all data
17
- * [ * Optional* ] Object id, allowing to detect substitutions of encrypted
18
- values
15
+ * Key id: use different keys for different attributes (or different users),
16
+ rotate keys over time without re-encrypting all data
17
+ * Object id: prevent substitution of encrypted values
19
18
20
19
All keys should be 32 bytes long, and cryptographically random. Manage these
21
20
keys as you would any other credentials (environment config, keychain, vault).
22
-
23
- Best way to generate keys:
21
+ Generate keys with:
24
22
```
25
23
node -p "require('crypto').randomBytes(32).toString('base64')"
26
24
```
@@ -37,9 +35,9 @@ specific scenarios:
37
35
* Query injection via unsanitized input
38
36
39
37
Specifically, this does * not* provide any protection in cases of a compromised
40
- web app host, app-level vulnerabilities, or accidental leaks into persistent
41
- logs. It is also in no way a substitute for actually encrypting your backups,
42
- sanitizing all you input, et cetera.
38
+ app host, app-level vulnerabilities, or accidentally leaking sensitive data
39
+ into logs. It is also not a substitute for actually encrypting your backups,
40
+ sanitizing your input, et cetera.
43
41
44
42
# Install
45
43
@@ -50,13 +48,12 @@ npm install node-encrypted-attr
50
48
# Use
51
49
52
50
While this module can be used stand-alone to encrypt individual values (see
53
- [ tests] ( /test/ ) ), it is designed to be wrapped in a plugin or hook for your
54
- favourite ORM. Eventually, this package may include such plugins for common
55
- ORMs, but for now, here's an example of integrating with [ thinky] ( https://github.com/neumino/thinky ) :
56
-
57
- ## Thinky
51
+ [ tests] ( /test/encrypted-attr.spec.js ) ), it is designed to be wrapped into a
52
+ plugin or hook for your favourite ORM. Eventually, this package may include
53
+ such plugins for common ORMs, but for now, here's an example of integrating
54
+ with [ thinky] ( https://github.com/neumino/thinky ) :
58
55
59
- ```
56
+ ``` js
60
57
const EncryptedAttributes = require (' node-encrypted-attr' )
61
58
const thinky = require (' thinky' )()
62
59
const _ = require (' lodash' )
@@ -67,23 +64,24 @@ Model.encryptedAttributes = EncryptedAttributes(['secret'], {
67
64
keys: {
68
65
k1: ' bocZRaBnmtHb2pXGTGixiQb9W2MmOtRBpbJn3ADX0cU='
69
66
},
70
- keyId: 'k1'
67
+ keyId: ' k1' ,
68
+ verifyId: true
71
69
})
72
70
73
- // Pre-save hook: encrypt any model attributes that need to be encrypted.
71
+ // Pre-save hook: encrypt model attributes that need to be encrypted.
74
72
Model .pre (' save' , function (next ) {
75
73
try {
76
- this.encryptedAttributes.encryptAll(obj )
74
+ this .encryptedAttributes .encryptAll (this )
77
75
process .nextTick (next)
78
76
} catch (err) {
79
77
process .nextTick (next, err)
80
78
}
81
79
})
82
80
83
- // Post-save hook: decrypt any model attributes that need to be decrypted.
81
+ // Post-save hook: decrypt model attributes that need to be decrypted.
84
82
Model .post (' save' , function (next ) {
85
83
try {
86
- this.encryptedAttributes.decryptAll(obj )
84
+ this .encryptedAttributes .decryptAll (this )
87
85
process .nextTick (next)
88
86
} catch (err) {
89
87
process .nextTick (next, err)
@@ -93,17 +91,17 @@ Model.post('save', function (next) {
93
91
// Post-retrieve hook: ditto.
94
92
Model .post (' retrieve' , function (next ) {
95
93
try {
96
- this.encryptedAttributes.decryptAll(obj )
94
+ this .encryptedAttributes .decryptAll (this )
97
95
process .nextTick (next)
98
96
} catch (err) {
99
97
process .nextTick (next, err)
100
98
}
101
99
})
102
100
103
- // Optionally, add some helper methods in case you need to set or read a value
104
- // directly, without going through model parser .
101
+ // Optionally, add some helpers in case we need to set or read the value
102
+ // directly (such as an update query) , without going through model hooks .
105
103
for (let attr of Model .encryptedAttributes .attributes ) {
106
- Mode .define(_.camelCase(`encrypted ${attr}`), function (val) {
104
+ Model .define (_ .camelCase (` encrypted ${ attr} ` ), function (val ) {
107
105
return Model .encryptedAttributes .encryptAttribute (this , val)
108
106
}
109
107
}
0 commit comments