Skip to content

Commit ad7a79b

Browse files
committed
fix: guard bad optional property types
1 parent 5050097 commit ad7a79b

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
const contacts = require('bindings')('contacts.node')
22

3+
const validProperties = [
4+
'jobTitle',
5+
'departmentName',
6+
'organizationName',
7+
'middleName',
8+
'note',
9+
'contactImage',
10+
'contactThumbnailImage',
11+
'instantMessageAddresses',
12+
'socialProfiles',
13+
]
14+
315
function getAllContacts(extraProperties = []) {
416
if (!Array.isArray(extraProperties)) {
517
throw new TypeError('extraProperties must be an array')
618
}
719

20+
if (!extraProperties.every((p) => validProperties.includes(p))) {
21+
throw new TypeError(
22+
`properties in extraProperties must be one of ${validProperties.join(', ')}`,
23+
)
24+
}
25+
826
return contacts.getAllContacts.call(this, extraProperties)
927
}
1028

@@ -17,6 +35,12 @@ function getContactsByName(name, extraProperties = []) {
1735
throw new TypeError('extraProperties must be an array')
1836
}
1937

38+
if (!extraProperties.every((p) => validProperties.includes(p))) {
39+
throw new TypeError(
40+
`properties in extraProperties must be one of ${validProperties.join(', ')}`,
41+
)
42+
}
43+
2044
return contacts.getContactsByName.call(this, name, extraProperties)
2145
}
2246

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"build": "node-gyp build",
88
"build:dev": "node-gyp build --debug",
99
"clean": "node-gyp clean",
10-
"lint": "prettier --check index.js",
11-
"format": "clang-format -i contacts.mm && prettier --write index.js",
10+
"lint": "prettier --check '**/*.js'",
11+
"format": "clang-format -i contacts.mm && prettier --write '**/*.js'",
1212
"rebuild": "node-gyp rebuild",
1313
"rebuild:dev": "node-gyp rebuild --debug",
1414
"test": "./node_modules/.bin/mocha --reporter spec"

test/module.spec.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
const { expect } = require('chai')
2-
const {
2+
const {
33
getAuthStatus,
44
getContactsByName,
55
getAllContacts,
66
addNewContact,
77
deleteContact,
8-
updateContact
8+
updateContact,
99
} = require('../index')
1010

1111
describe('node-mac-contacts', () => {
1212
describe('getAuthStatus()', () => {
1313
it('should not throw', () => {
14-
expect(() => { getAuthStatus() }).to.not.throw()
14+
expect(() => {
15+
getAuthStatus()
16+
}).to.not.throw()
1517
})
1618

1719
it('should return a string', () => {
@@ -32,6 +34,15 @@ describe('node-mac-contacts', () => {
3234
getContactsByName('jim-bob', 12345)
3335
}).to.throw(/extraProperties must be an array/)
3436
})
37+
38+
it('should throw if extraProperties contains invalid properties', () => {
39+
const errorMessage =
40+
'properties in extraProperties must be one of jobTitle, departmentName, organizationName, middleName, note, contactImage, contactThumbnailImage, instantMessageAddresses, socialProfiles'
41+
42+
expect(() => {
43+
getContactsByName('jim-bob', ['bad-property'])
44+
}).to.throw(errorMessage)
45+
})
3546
})
3647

3748
describe('getAllContacts([extraProperties])', () => {
@@ -45,6 +56,15 @@ describe('node-mac-contacts', () => {
4556
getAllContacts('tsk-bad-array')
4657
}).to.throw(/extraProperties must be an array/)
4758
})
59+
60+
it('should throw if extraProperties contains invalid properties', () => {
61+
const errorMessage =
62+
'properties in extraProperties must be one of jobTitle, departmentName, organizationName, middleName, note, contactImage, contactThumbnailImage, instantMessageAddresses, socialProfiles'
63+
64+
expect(() => {
65+
getAllContacts(['bad-property'])
66+
}).to.throw(errorMessage)
67+
})
4868
})
4969

5070
describe('addNewContact(contact)', () => {
@@ -150,4 +170,4 @@ describe('node-mac-contacts', () => {
150170
}).to.throw(/emailAddresses must be an array/)
151171
})
152172
})
153-
})
173+
})

0 commit comments

Comments
 (0)