Skip to content

Commit aa44392

Browse files
committed
Fix GH-21262: ldap_modify() too strict controls argument validation.
make it impossible to unset an attribute. close GH-21263
1 parent c89b363 commit aa44392

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ PHP NEWS
3333
. Fixed bug GH-21097 (Accessing Dom\Node properties can can throw TypeError).
3434
(ndossche)
3535

36+
- LDAP:
37+
. Fixed bug GH-21262 (ldap_modify() too strict controls argument validation
38+
makes it impossible to unset attribute). (David Carlier)
39+
3640
- MBString:
3741
. Fixed bug GH-21223; mb_guess_encoding no longer crashes when passed huge
3842
list of candidate encodings (with 200,000+ entries). (Jordi Kroon)

ext/ldap/ldap.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,9 +2339,14 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, bool ext)
23392339
SEPARATE_ARRAY(attribute_values);
23402340
uint32_t num_values = zend_hash_num_elements(Z_ARRVAL_P(attribute_values));
23412341
if (num_values == 0) {
2342-
zend_argument_value_error(3, "attribute \"%s\" must be a non-empty list of attribute values", ZSTR_VAL(attribute));
2343-
RETVAL_FALSE;
2344-
goto cleanup;
2342+
if (UNEXPECTED(oper == LDAP_MOD_ADD)) {
2343+
zend_argument_value_error(3, "attribute \"%s\" must be a non-empty list of attribute values", ZSTR_VAL(attribute));
2344+
RETVAL_FALSE;
2345+
goto cleanup;
2346+
}
2347+
/* When we modify, we mean we delete the attribute */
2348+
attribute_index++;
2349+
continue;
23452350
}
23462351
if (!php_ldap_is_numerically_indexed_array(Z_ARRVAL_P(attribute_values))) {
23472352
zend_argument_value_error(3, "attribute \"%s\" must be an array of attribute values with numeric keys", ZSTR_VAL(attribute));

ext/ldap/tests/gh21262.phpt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
GH-21262 (ldap_modify() too strict controls argument validation)
3+
--EXTENSIONS--
4+
ldap
5+
--FILE--
6+
<?php
7+
/* We are assuming 3333 is not connectable */
8+
$ldap = ldap_connect('ldap://127.0.0.1:3333');
9+
$valid_dn = "cn=userA,something";
10+
11+
$entry_with_empty_array = [
12+
'attribute1' => 'value',
13+
'attribute2' => [],
14+
];
15+
16+
// ldap_add() should still reject empty arrays
17+
try {
18+
ldap_add($ldap, $valid_dn, $entry_with_empty_array);
19+
} catch (ValueError $e) {
20+
echo $e->getMessage(), PHP_EOL;
21+
}
22+
23+
// ldap_mod_add() should still reject empty arrays
24+
try {
25+
ldap_mod_add($ldap, $valid_dn, $entry_with_empty_array);
26+
} catch (ValueError $e) {
27+
echo $e->getMessage(), PHP_EOL;
28+
}
29+
30+
// ldap_modify() should accept empty arrays (delete attribute)
31+
try {
32+
@ldap_modify($ldap, $valid_dn, $entry_with_empty_array);
33+
echo "ldap_modify: no ValueError thrown", PHP_EOL;
34+
} catch (ValueError $e) {
35+
echo "ldap_modify: UNEXPECTED ValueError: ", $e->getMessage(), PHP_EOL;
36+
}
37+
38+
// ldap_mod_del() should accept empty arrays (delete attribute)
39+
try {
40+
@ldap_mod_del($ldap, $valid_dn, $entry_with_empty_array);
41+
echo "ldap_mod_del: no ValueError thrown", PHP_EOL;
42+
} catch (ValueError $e) {
43+
echo "ldap_mod_del: UNEXPECTED ValueError: ", $e->getMessage(), PHP_EOL;
44+
}
45+
?>
46+
--EXPECT--
47+
ldap_add(): Argument #3 ($entry) attribute "attribute2" must be a non-empty list of attribute values
48+
ldap_mod_add(): Argument #3 ($entry) attribute "attribute2" must be a non-empty list of attribute values
49+
ldap_modify: no ValueError thrown
50+
ldap_mod_del: no ValueError thrown

0 commit comments

Comments
 (0)