@@ -13,7 +13,7 @@ Encrypted model attributes in your favourite ORM.
13
13
* 128-bit authentication tag
14
14
* Additional authenticated data:
15
15
* Key id: use different keys for different attributes (or different users),
16
- rotate keys over time without re-encrypting all data
16
+ rotate keys over time without re-encrypting
17
17
* Object id: prevent substitution of encrypted values
18
18
19
19
All keys should be 32 bytes long, and cryptographically random. Manage these
@@ -28,10 +28,10 @@ node -p "require('crypto').randomBytes(32).toString('base64')"
28
28
This is designed to protect you from leaking sensitive user data under very
29
29
specific scenarios:
30
30
31
- * Full database dump
31
+ * Full data dump
32
32
* Misplaced unencrypted backups
33
33
* Compromised database host
34
- * Partial database dump
34
+ * Partial data dump
35
35
* Query injection via unsanitized input
36
36
37
37
Specifically, this does * not* provide any protection in cases of a compromised
@@ -60,7 +60,7 @@ const _ = require('lodash')
60
60
61
61
let Model = thinky .createModel (' Model' , {})
62
62
63
- Model . encryptedAttributes = EncryptedAttributes ([' secret' , ' nested.secret' ], {
63
+ let encryptedAttributes = EncryptedAttributes ([' secret' , ' nested.secret' ], {
64
64
keys: {
65
65
k1: crypto .randomBytes (32 ).toString (' base64' ) // use an actual key here
66
66
},
@@ -69,40 +69,28 @@ Model.encryptedAttributes = EncryptedAttributes(['secret', 'nested.secret'], {
69
69
})
70
70
71
71
// Pre-save hook: encrypt model attributes that need to be encrypted.
72
- Model .pre (' save' , function (next ) {
73
- try {
74
- Model .encryptedAttributes .encryptAll (this )
75
- process .nextTick (next)
76
- } catch (err) {
77
- process .nextTick (next, err)
78
- }
72
+ Model .docOn (' saving' , function (doc ) {
73
+ encryptedAttributes .encryptAll (doc)
79
74
})
80
75
81
76
// Post-save hook: decrypt model attributes that need to be decrypted.
82
- Model .post (' save' , function (next ) {
83
- try {
84
- Model .encryptedAttributes .decryptAll (this )
85
- process .nextTick (next)
86
- } catch (err) {
87
- process .nextTick (next, err)
88
- }
77
+ Model .docOn (' saved' , function (doc ) {
78
+ encryptedAttributes .decryptAll (doc)
89
79
})
90
80
91
81
// Post-retrieve hook: ditto.
92
- Model .post (' retrieve' , function (next ) {
93
- try {
94
- Model .encryptedAttributes .decryptAll (this )
95
- process .nextTick (next)
96
- } catch (err) {
97
- process .nextTick (next, err)
98
- }
82
+ Model .on (' retrieved' , function (doc ) {
83
+ encryptedAttributes .decryptAll (doc)
99
84
})
100
85
101
86
// Optionally, add some helpers in case we need to set or read the value
102
87
// directly (such as an update query), without going through model hooks.
103
- for (let attr of Model .encryptedAttributes .attributes ) {
104
- Model .define (_ .camelCase (` encrypted ${ attr} ` ), function (val ) {
105
- return Model .encryptedAttributes .encryptAttribute (this , val)
88
+ for (let attr of encryptedAttributes .attributes ) {
89
+ Model .define (_ .camelCase (` encrypt ${ attr} ` ), function (val ) {
90
+ return encryptedAttributes .encryptAttribute (this , val)
91
+ })
92
+ Model .define (_ .camelCase (` decrypt ${ attr} ` ), function (val ) {
93
+ return encryptedAttributes .decryptAttribute (this , val)
106
94
})
107
95
}
108
96
```
0 commit comments