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

Commit

Permalink
skip attribute with empty name (#4)
Browse files Browse the repository at this point in the history
* skip attribute with empty name

* fire warning on each empty attribute name

Co-authored-by: James Sumners <[email protected]>

* Update process-warning

---------

Co-authored-by: James Sumners <[email protected]>
  • Loading branch information
ahaenggli and jsumners authored Apr 21, 2023
1 parent b37b2f5 commit 0fbacd8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/deprecations.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ warning.create(clazz, 'LDAP_MESSAGE_DEP_003', 'abandonID is deprecated. Use aban

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

warning.create(clazz, 'LDAP_ATTRIBUTE_SPEC_ERR_001', 'received attempt to define attribute with an empty name: attribute skipped.', { unlimited: true })

module.exports = warning
4 changes: 4 additions & 0 deletions lib/messages/search-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { operations, search } = require('@ldapjs/protocol')
const { DN } = require('@ldapjs/dn')
const filter = require('@ldapjs/filter')
const { BerReader, BerTypes } = require('@ldapjs/asn1')
const warning = require('../deprecations')

const recognizedScopes = new Map([
['base', [search.SCOPE_BASE_OBJECT, 'base']],
Expand Down Expand Up @@ -198,6 +199,9 @@ class SearchRequest extends LdapMessage {
for (const attr of attrs) {
if (typeof attr === 'string' && isValidAttributeString(attr) === true) {
newAttrs.push(attr)
} else if (typeof attr === 'string' && attr === '') {
// TODO: emit warning about spec violation via log and/or telemetry
warning.emit('LDAP_ATTRIBUTE_SPEC_ERR_001')
} else {
throw Error('attribute must be a valid string')
}
Expand Down
22 changes: 22 additions & 0 deletions lib/messages/search-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const filter = require('@ldapjs/filter')
const SearchRequest = require('./search-request')
const { DN } = require('@ldapjs/dn')
const { BerReader, BerWriter } = require('@ldapjs/asn1')
const warning = require('../deprecations')

// Silence the standard warning logs. We will test the messages explicitly.
process.removeAllListeners('warning')

const {
searchRequestBytes
Expand Down Expand Up @@ -107,6 +111,24 @@ tap.test('.attributes', t => {
t.strictSame(req.attributes, ['a'])
})

t.test('skip empty attribute name', async t => {
process.on('warning', handler)
t.teardown(async () => {
process.removeListener('warning', handler)
warning.emitted.set('LDAP_ATTRIBUTE_SPEC_ERR_001', false)
})

const req = new SearchRequest({
attributes: ['abc', '']
})
t.strictSame(req.attributes, ['abc'])

function handler (error) {
t.equal(error.message, 'received attempt to define attribute with an empty name: attribute skipped.')
t.end()
}
})

t.end()
})

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@ldapjs/dn": "1.0.0",
"@ldapjs/filter": "2.0.0",
"@ldapjs/protocol": "1.2.1",
"process-warning": "^2.1.0"
"process-warning": "^2.2.0"
},
"devDependencies": {
"@fastify/pre-commit": "^2.0.2",
Expand Down

0 comments on commit 0fbacd8

Please sign in to comment.