Skip to content

Commit 40d2a6a

Browse files
committed
chore(matrix-identity-server): fails when using weird or complex ldapfilter
Signed-off-by: Pierre 'McFly' Marty <[email protected]>
1 parent 53ab53b commit 40d2a6a

File tree

2 files changed

+279
-90
lines changed

2 files changed

+279
-90
lines changed

packages/matrix-identity-server/src/userdb/ldap.test.ts

Lines changed: 88 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ beforeAll((done) => {
4343
4444
age: 25
4545
}
46+
},
47+
{
48+
dn: 'cn=lskywalker,ou=users,o=example',
49+
attributes: {
50+
objectClass: ['inetOrgPerson'],
51+
uid: 'lskywalker',
52+
sn: ['skywalker'],
53+
givenName: 'Luke',
54+
55+
age: 23
56+
}
4657
}
4758
]
4859

@@ -97,46 +108,15 @@ describe('UserDBLDAP', () => {
97108
it('should return results with default fields for a single filter', async () => {
98109
userDB = createUserDB()
99110
await userDB.ready
100-
const list = await userDB.get('', [], { uid: 'dwho' })
101-
expect(list.length).toBe(1)
102-
expect(list[0].uid).toBe('dwho')
103-
expect(list[0].sn).toBe('doctor')
104-
expect(list[0].dn).toBe('cn=dwho,ou=users,o=example')
105-
expect(list[0].objectClass).toBe('inetOrgPerson')
106-
})
111+
const list1 = await userDB.get('', [], { uid: 'dwho', sn: ['doctor'] })
112+
expect(list1.length).toBe(1)
113+
expect(list1[0].dn).toBe('cn=dwho,ou=users,o=example')
107114

108-
it('should return results with specified fields', async () => {
109-
userDB = createUserDB()
110-
await userDB.ready
111-
const list = await userDB.get('', ['uid', 'mail'], { uid: 'jdoe' })
112-
expect(list.length).toBe(1)
113-
expect(list[0]).toEqual({ uid: 'jdoe', mail: '[email protected]' })
114-
expect(list[0].sn).toBeUndefined()
115-
})
115+
const list2 = await userDB.get('', ['uid'], { uid: 'dwho' })
116+
expect(list2[0]).toEqual({ uid: 'dwho' })
116117

117-
it('should return results for multiple filter fields using OR logic', async () => {
118-
userDB = createUserDB()
119-
await userDB.ready
120-
const list = await userDB.get('', ['uid', 'sn'], {
121-
uid: 'dwho',
122-
sn: ['doe']
123-
})
124-
expect(list.length).toBe(2)
125-
const uids = list.map((entry) => entry.uid).sort()
126-
expect(uids).toEqual(['dwho', 'jdoe'])
127-
expect(
128-
list.some((entry) => entry.uid === 'dwho' && entry.sn === 'doctor')
129-
).toBe(true)
130-
expect(
131-
list.some((entry) => entry.uid === 'jdoe' && entry.sn === 'doe')
132-
).toBe(true)
133-
})
134-
135-
it('should return an empty list if no match is found', async () => {
136-
userDB = createUserDB()
137-
await userDB.ready
138-
const list = await userDB.get('', [], { uid: 'nonexistent' })
139-
expect(list.length).toBe(0)
118+
const list3 = await userDB.get('', [], { uid: 'zz' })
119+
expect(list3.length).toBe(0)
140120
})
141121

142122
it('should display error message on connection error during ready state', async () => {
@@ -160,48 +140,49 @@ describe('UserDBLDAP', () => {
160140
it('should provide match results with wildcard search', async () => {
161141
userDB = createUserDB()
162142
await userDB.ready
163-
const list = await userDB.match('', ['uid', 'sn'], ['uid', 'sn'], 'do')
164-
expect(list.length).toBe(2)
165-
const uids = list.map((entry) => entry.uid).sort()
166-
expect(uids).toEqual(['dwho', 'jdoe'])
143+
const list = await userDB.match('', ['uid'], ['uid'], 'wh')
144+
expect(list.length).toBe(1)
145+
expect(list[0]).toEqual({ uid: 'dwho' })
167146
})
168147

169148
it('should provide getAll results with all entries', async () => {
170149
userDB = createUserDB()
171150
await userDB.ready
172-
const list = await userDB.getAll('', ['uid', 'sn'])
173-
expect(list.length).toBe(3)
151+
const list = await userDB.getAll('', ['uid'])
152+
expect(list.length).toBe(4)
174153
const uids = list.map((entry) => entry.uid).sort()
175-
expect(uids).toEqual(['asmith', 'dwho', 'jdoe'])
154+
expect(uids).toEqual(['asmith', 'dwho', 'jdoe', 'lskywalker'])
176155
})
177156

178157
it('should provide getAll results with order by a string field', async () => {
179158
userDB = createUserDB()
180159
await userDB.ready
181-
const list = await userDB.getAll('', ['uid', 'sn'], 'uid')
182-
expect(list.length).toBe(3)
160+
const list = await userDB.getAll('', ['uid'], 'uid')
161+
expect(list.length).toBe(4)
183162
expect(list[0].uid).toBe('asmith')
184163
expect(list[1].uid).toBe('dwho')
185164
expect(list[2].uid).toBe('jdoe')
165+
expect(list[3].uid).toBe('lskywalker')
186166
})
187167

188168
it('should provide getAll results with order by a numeric field', async () => {
189169
userDB = createUserDB()
190170
await userDB.ready
191171
const list = await userDB.getAll('', ['uid', 'age'], 'age')
192-
expect(list.length).toBe(3)
193-
expect(list[0].uid).toBe('asmith')
194-
expect(list[1].uid).toBe('jdoe')
195-
expect(list[2].uid).toBe('dwho')
172+
expect(list.length).toBe(4)
173+
expect(list[0].uid).toBe('lskywalker')
174+
expect(list[1].uid).toBe('asmith')
175+
expect(list[2].uid).toBe('jdoe')
176+
expect(list[3].uid).toBe('dwho')
196177
})
197178

198179
it('should handle empty filterFields in get method', async () => {
199180
userDB = createUserDB()
200181
await userDB.ready
201182
const list = await userDB.get('', ['uid'])
202-
expect(list.length).toBe(3)
183+
expect(list.length).toBe(4)
203184
const uids = list.map((entry) => entry.uid).sort()
204-
expect(uids).toEqual(['asmith', 'dwho', 'jdoe'])
185+
expect(uids).toEqual(['asmith', 'dwho', 'jdoe', 'lskywalker'])
205186
})
206187

207188
it('should handle empty searchFields in match method gracefully (though usually not intended)', async () => {
@@ -215,4 +196,58 @@ describe('UserDBLDAP', () => {
215196
userDB = createUserDB()
216197
expect(() => userDB.close()).not.toThrow()
217198
})
199+
200+
it('should handle ldapFilter with array values and wildcards (from createRoom example)', async () => {
201+
userDB = createUserDB()
202+
await userDB.ready
203+
const list = await userDB.get('', ['uid', 'mail'], {
204+
205+
})
206+
expect(list.length).toBe(2)
207+
const mails = list.map((user) => user.mail).sort()
208+
expect(mails).toEqual(['[email protected]', '[email protected]'])
209+
})
210+
211+
it('should handle complex AND filter with multiple attributes', async () => {
212+
userDB = createUserDB()
213+
await userDB.ready
214+
const list = await userDB.get('', ['uid', 'givenName'], {
215+
uid: 'dwho',
216+
givenName: 'Doctor'
217+
})
218+
expect(list.length).toBe(1)
219+
expect(list[0]).toEqual({ uid: 'dwho', givenName: 'Doctor' })
220+
})
221+
222+
// it('should handle complex OR filter with different attributes and wildcards', async () => {
223+
// userDB = createUserDB()
224+
// await userDB.ready
225+
// const list = await userDB.get('', ['uid', 'mail'], '(|(givenName=John)(mail=*smith*))')
226+
// expect(list.length).toBe(2)
227+
// const uids = list.map((entry) => entry.uid).sort()
228+
// expect(uids).toEqual(['asmith', 'jdoe'])
229+
// })
230+
231+
it('should handle filter with multiple wildcard parts in a single value', async () => {
232+
userDB = createUserDB()
233+
await userDB.ready
234+
const list = await userDB.get('', ['uid', 'mail'], {
235+
236+
})
237+
expect(list.length).toBe(1)
238+
expect(list[0]).toEqual({
239+
uid: 'lskywalker',
240+
241+
})
242+
})
243+
244+
it('should return empty list for complex filter with no matching results', async () => {
245+
userDB = createUserDB()
246+
await userDB.ready
247+
const list = await userDB.get('', ['uid'], {
248+
uid: 'nonexistent',
249+
mail: '*@example.org'
250+
})
251+
expect(list.length).toBe(0)
252+
})
218253
})

0 commit comments

Comments
 (0)