Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit 0fbacd8

Browse files
ahaengglijsumners
andauthored
skip attribute with empty name (#4)
* skip attribute with empty name * fire warning on each empty attribute name Co-authored-by: James Sumners <james@sumners.email> * Update process-warning --------- Co-authored-by: James Sumners <james@sumners.email>
1 parent b37b2f5 commit 0fbacd8

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

lib/deprecations.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ warning.create(clazz, 'LDAP_MESSAGE_DEP_003', 'abandonID is deprecated. Use aban
1111

1212
warning.create(clazz, 'LDAP_MESSAGE_DEP_004', 'errorMessage is deprecated. Use diagnosticMessage instead.')
1313

14+
warning.create(clazz, 'LDAP_ATTRIBUTE_SPEC_ERR_001', 'received attempt to define attribute with an empty name: attribute skipped.', { unlimited: true })
15+
1416
module.exports = warning

lib/messages/search-request.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { operations, search } = require('@ldapjs/protocol')
55
const { DN } = require('@ldapjs/dn')
66
const filter = require('@ldapjs/filter')
77
const { BerReader, BerTypes } = require('@ldapjs/asn1')
8+
const warning = require('../deprecations')
89

910
const recognizedScopes = new Map([
1011
['base', [search.SCOPE_BASE_OBJECT, 'base']],
@@ -198,6 +199,9 @@ class SearchRequest extends LdapMessage {
198199
for (const attr of attrs) {
199200
if (typeof attr === 'string' && isValidAttributeString(attr) === true) {
200201
newAttrs.push(attr)
202+
} else if (typeof attr === 'string' && attr === '') {
203+
// TODO: emit warning about spec violation via log and/or telemetry
204+
warning.emit('LDAP_ATTRIBUTE_SPEC_ERR_001')
201205
} else {
202206
throw Error('attribute must be a valid string')
203207
}

lib/messages/search-request.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const filter = require('@ldapjs/filter')
66
const SearchRequest = require('./search-request')
77
const { DN } = require('@ldapjs/dn')
88
const { BerReader, BerWriter } = require('@ldapjs/asn1')
9+
const warning = require('../deprecations')
10+
11+
// Silence the standard warning logs. We will test the messages explicitly.
12+
process.removeAllListeners('warning')
913

1014
const {
1115
searchRequestBytes
@@ -107,6 +111,24 @@ tap.test('.attributes', t => {
107111
t.strictSame(req.attributes, ['a'])
108112
})
109113

114+
t.test('skip empty attribute name', async t => {
115+
process.on('warning', handler)
116+
t.teardown(async () => {
117+
process.removeListener('warning', handler)
118+
warning.emitted.set('LDAP_ATTRIBUTE_SPEC_ERR_001', false)
119+
})
120+
121+
const req = new SearchRequest({
122+
attributes: ['abc', '']
123+
})
124+
t.strictSame(req.attributes, ['abc'])
125+
126+
function handler (error) {
127+
t.equal(error.message, 'received attempt to define attribute with an empty name: attribute skipped.')
128+
t.end()
129+
}
130+
})
131+
110132
t.end()
111133
})
112134

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@ldapjs/dn": "1.0.0",
1818
"@ldapjs/filter": "2.0.0",
1919
"@ldapjs/protocol": "1.2.1",
20-
"process-warning": "^2.1.0"
20+
"process-warning": "^2.2.0"
2121
},
2222
"devDependencies": {
2323
"@fastify/pre-commit": "^2.0.2",

0 commit comments

Comments
 (0)