Skip to content

Commit 0532c92

Browse files
committed
Do not create missing nested attributes
1 parent 7be3a0b commit 0532c92

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/encrypted-attr.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ function EncryptedAttributes (attributes, options) {
3333

3434
function encryptAll (obj) {
3535
for (let attr of attributes) {
36-
set(obj, attr, encryptAttribute(obj, get(obj, attr)))
36+
let val = get(obj, attr)
37+
if (val != null) {
38+
set(obj, attr, encryptAttribute(obj, val))
39+
}
3740
}
3841
return obj
3942
}
@@ -65,7 +68,10 @@ function EncryptedAttributes (attributes, options) {
6568

6669
function decryptAll (obj) {
6770
for (let attr of attributes) {
68-
set(obj, attr, decryptAttribute(obj, get(obj, attr)))
71+
let val = get(obj, attr)
72+
if (val != null) {
73+
set(obj, attr, decryptAttribute(obj, val))
74+
}
6975
}
7076
return obj
7177
}

test/encrypted-attr.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ describe('encrypted attributes', function () {
4040
expect(obj.secret.dataLength, 'to equal', 5)
4141
})
4242

43+
it('should not create missing nested attributes', function () {
44+
let enc = EncryptedAttributes(['secret.data'], this.options)
45+
let obj = {id: 1}
46+
enc.encryptAll(obj)
47+
expect(obj.secret, 'to be undefined')
48+
})
49+
4350
it('should not encrypt null', function () {
4451
let enc = EncryptedAttributes(['secret'], this.options)
4552
let obj = {id: 1}
@@ -99,6 +106,13 @@ describe('encrypted attributes', function () {
99106
expect(obj.secret.dataLength, 'to equal', 5)
100107
})
101108

109+
it('should not create missing nested attributes', function () {
110+
let enc = EncryptedAttributes(['secret.data'], this.options)
111+
let obj = {id: 1}
112+
enc.decryptAll(obj)
113+
expect(obj.secret, 'to be undefined')
114+
})
115+
102116
it('should not decrypt null', function () {
103117
let enc = EncryptedAttributes(['secret'], this.options)
104118
let obj = {id: 1}

0 commit comments

Comments
 (0)