Skip to content

Commit 6b68196

Browse files
committed
LDAP: Updated default user filter placeholder format
To not conflict with env variables, and to align with placeholders used for PDF gen command. Added test to cover, including old format supported for back-compatibility. For #4967
1 parent e1149a2 commit 6b68196

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

.env.example.complete

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ LDAP_SERVER=false
215215
LDAP_BASE_DN=false
216216
LDAP_DN=false
217217
LDAP_PASS=false
218-
LDAP_USER_FILTER=false
218+
LDAP_USER_FILTER="(&(uid={user}))"
219219
LDAP_VERSION=false
220220
LDAP_START_TLS=false
221221
LDAP_TLS_INSECURE=false

app/Access/LdapService.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,18 @@ protected function parseServerString(string $serverString): string
249249

250250
/**
251251
* Build a filter string by injecting common variables.
252+
* Both "${var}" and "{var}" style placeholders are supported.
253+
* Dollar based are old format but supported for compatibility.
252254
*/
253255
protected function buildFilter(string $filterString, array $attrs): string
254256
{
255257
$newAttrs = [];
256258
foreach ($attrs as $key => $attrText) {
257-
$newKey = '${' . $key . '}';
258-
$newAttrs[$newKey] = $this->ldap->escape($attrText);
259+
$escapedText = $this->ldap->escape($attrText);
260+
$oldVarKey = '${' . $key . '}';
261+
$newVarKey = '{' . $key . '}';
262+
$newAttrs[$oldVarKey] = $escapedText;
263+
$newAttrs[$newVarKey] = $escapedText;
259264
}
260265

261266
return strtr($filterString, $newAttrs);

app/Config/services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
'dn' => env('LDAP_DN', false),
124124
'pass' => env('LDAP_PASS', false),
125125
'base_dn' => env('LDAP_BASE_DN', false),
126-
'user_filter' => env('LDAP_USER_FILTER', '(&(uid=${user}))'),
126+
'user_filter' => env('LDAP_USER_FILTER', '(&(uid={user}))'),
127127
'version' => env('LDAP_VERSION', false),
128128
'id_attribute' => env('LDAP_ID_ATTRIBUTE', 'uid'),
129129
'email_attribute' => env('LDAP_EMAIL_ATTRIBUTE', 'mail'),

tests/Auth/LdapTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected function setUp(): void
3232
'services.ldap.id_attribute' => 'uid',
3333
'services.ldap.user_to_groups' => false,
3434
'services.ldap.version' => '3',
35-
'services.ldap.user_filter' => '(&(uid=${user}))',
35+
'services.ldap.user_filter' => '(&(uid={user}))',
3636
'services.ldap.follow_referrals' => false,
3737
'services.ldap.tls_insecure' => false,
3838
'services.ldap.thumbnail_attribute' => null,
@@ -178,6 +178,38 @@ public function test_a_custom_uid_attribute_can_be_specified_and_is_used_properl
178178
$this->assertDatabaseHas('users', ['email' => $this->mockUser->email, 'email_confirmed' => false, 'external_auth_id' => 'cooluser456']);
179179
}
180180

181+
public function test_user_filter_default_placeholder_format()
182+
{
183+
config()->set('services.ldap.user_filter', '(&(uid={user}))');
184+
$this->mockUser->name = 'barryldapuser';
185+
$expectedFilter = '(&(uid=\62\61\72\72\79\6c\64\61\70\75\73\65\72))';
186+
187+
$this->commonLdapMocks(1, 1, 1, 1, 1);
188+
$this->mockLdap->shouldReceive('searchAndGetEntries')
189+
->once()
190+
->with($this->resourceId, config('services.ldap.base_dn'), $expectedFilter, \Mockery::type('array'))
191+
->andReturn(['count' => 0, 0 => []]);
192+
193+
$resp = $this->mockUserLogin();
194+
$resp->assertRedirect('/login');
195+
}
196+
197+
public function test_user_filter_old_placeholder_format()
198+
{
199+
config()->set('services.ldap.user_filter', '(&(username=${user}))');
200+
$this->mockUser->name = 'barryldapuser';
201+
$expectedFilter = '(&(username=\62\61\72\72\79\6c\64\61\70\75\73\65\72))';
202+
203+
$this->commonLdapMocks(1, 1, 1, 1, 1);
204+
$this->mockLdap->shouldReceive('searchAndGetEntries')
205+
->once()
206+
->with($this->resourceId, config('services.ldap.base_dn'), $expectedFilter, \Mockery::type('array'))
207+
->andReturn(['count' => 0, 0 => []]);
208+
209+
$resp = $this->mockUserLogin();
210+
$resp->assertRedirect('/login');
211+
}
212+
181213
public function test_initial_incorrect_credentials()
182214
{
183215
$this->commonLdapMocks(1, 1, 1, 0, 1);

0 commit comments

Comments
 (0)