Skip to content

Commit fda4ac7

Browse files
likemusicclaude
andcommitted
ext/soap: report invalid SoapClient options without a bailout
The SoapClient constructor reported a missing "uri" or "location" option in non-WSDL mode and an invalid "encoding" option with php_error_docref(E_ERROR, ...). As with WSDL errors, SoapClient turned the fatal error into a SoapFault and caught the bailout in SOAP_CLIENT_END_CODE(), with gc_protect(1) already set, so the cycle collector stayed off for the rest of the request (GH-10026). Reported in #10026 (comment). These four errors now go through soap_client_option_error(), which throws the same SoapFault ("SoapClient::__construct(): <message>", fault code "Client", also stored in __soap_fault), and the constructor returns. With the "exceptions" option disabled it is the same fatal error as before. The option is read after "uri" and "location" are checked, so as before those two errors are a SoapFault even then. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 619add1 commit fda4ac7

4 files changed

Lines changed: 116 additions & 7 deletions

File tree

‎NEWS‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ PHP NEWS
150150
Content-Length and reusing the connection. (Ilia Alshanetsky)
151151
. Fixed bug GH-24315 (Harden SoapClient cross-origin redirect handling).
152152
(Ilia Alshanetsky)
153-
. SoapClient no longer bails out on WSDL and XML schema errors, which kept
154-
the cycle collector disabled for the rest of the request (GH-10026).
155-
(Valery Ivashchanka)
153+
. SoapClient no longer bails out on WSDL and XML schema errors and on
154+
invalid constructor options, which kept the cycle collector disabled for
155+
the rest of the request (GH-10026). (Valery Ivashchanka)
156156

157157
- SPL:
158158
. Fixed bug GH-23385 (SplDoublyLinkedList::serialize() use-after-free when

‎ext/soap/soap.c‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static void delete_argv(struct _soap_class *class);
7070

7171
static void soap_error_handler(int error_num, zend_string *error_filename, uint32_t error_lineno, zend_string *message);
7272
ZEND_NORETURN static ZEND_COLD void soap_fault_to_error(char *code);
73+
static ZEND_COLD void soap_client_option_error(zval *this_ptr, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
7374

7475
#define SOAP_SERVER_BEGIN_CODE() \
7576
bool _old_handler = SOAP_GLOBAL(use_soap_error_handler);\
@@ -2049,6 +2050,40 @@ ZEND_NORETURN static ZEND_COLD void soap_fault_to_error(char *code) /* {{{ */
20492050
}
20502051
/* }}} */
20512052

2053+
/* Reports an invalid option of the SoapClient constructor. With exceptions it
2054+
* throws the same SoapFault as the fatal error used to be turned into,
2055+
* "SoapClient::__construct(): <message>", without a bailout, and the caller
2056+
* must return. Otherwise it is the same fatal error as before. */
2057+
static ZEND_COLD void soap_client_option_error(zval *this_ptr, const char *format, ...) /* {{{ */
2058+
{
2059+
va_list args;
2060+
zend_string *details, *function, *message;
2061+
zval fault;
2062+
const char *code = SOAP_GLOBAL(error_code);
2063+
2064+
va_start(args, format);
2065+
if (Z_TYPE_P(Z_CLIENT_EXCEPTIONS_P(this_ptr)) == IS_FALSE) {
2066+
php_verror(NULL, E_ERROR, format, args);
2067+
ZEND_UNREACHABLE();
2068+
}
2069+
details = zend_vstrpprintf(0, format, args);
2070+
va_end(args);
2071+
2072+
function = get_active_function_or_method_name();
2073+
message = zend_strpprintf(0, "%s(): %s", ZSTR_VAL(function), ZSTR_VAL(details));
2074+
zend_string_release(function);
2075+
zend_string_release(details);
2076+
2077+
if (code == NULL) {
2078+
code = "Client";
2079+
}
2080+
add_soap_fault_ex_en(&fault, this_ptr, code, ZSTR_VAL(message));
2081+
Z_ADDREF(fault);
2082+
zend_string_release(message);
2083+
zend_throw_exception_object(&fault);
2084+
}
2085+
/* }}} */
2086+
20522087
static zend_never_inline ZEND_COLD void soap_real_error_handler(int error_num, zend_string *error_filename, const uint32_t error_lineno, zend_string *message) /* {{{ */
20532088
{
20542089
bool _old_in_compilation;
@@ -2230,7 +2265,8 @@ PHP_METHOD(SoapClient, __construct)
22302265
Z_TYPE_P(tmp) == IS_STRING) {
22312266
ZVAL_STR_COPY(Z_CLIENT_URI_P(this_ptr), Z_STR_P(tmp));
22322267
} else {
2233-
php_error_docref(NULL, E_ERROR, "'uri' option is required in nonWSDL mode");
2268+
soap_client_option_error(this_ptr, "'uri' option is required in nonWSDL mode");
2269+
goto end;
22342270
}
22352271

22362272
if ((tmp = zend_hash_str_find(ht, "style", sizeof("style")-1)) != NULL &&
@@ -2250,7 +2286,8 @@ PHP_METHOD(SoapClient, __construct)
22502286
Z_TYPE_P(tmp) == IS_STRING) {
22512287
ZVAL_STR_COPY(Z_CLIENT_LOCATION_P(this_ptr), Z_STR_P(tmp));
22522288
} else if (!wsdl) {
2253-
php_error_docref(NULL, E_ERROR, "'location' option is required in nonWSDL mode");
2289+
soap_client_option_error(this_ptr, "'location' option is required in nonWSDL mode");
2290+
goto end;
22542291
}
22552292

22562293
if ((tmp = zend_hash_str_find(ht, "soap_version", sizeof("soap_version")-1)) != NULL) {
@@ -2318,7 +2355,8 @@ PHP_METHOD(SoapClient, __construct)
23182355

23192356
encoding = xmlFindCharEncodingHandler(Z_STRVAL_P(tmp));
23202357
if (UNEXPECTED(encoding == NULL)) {
2321-
php_error_docref(NULL, E_ERROR, "Invalid 'encoding' option - '%s'", Z_STRVAL_P(tmp));
2358+
soap_client_option_error(this_ptr, "Invalid 'encoding' option - '%s'", Z_STRVAL_P(tmp));
2359+
goto end;
23222360
} else {
23232361
xmlCharEncCloseFunc(encoding);
23242362
ZVAL_STR_COPY(Z_CLIENT_ENCODING_P(this_ptr), Z_STR_P(tmp));
@@ -2421,7 +2459,8 @@ PHP_METHOD(SoapClient, __construct)
24212459

24222460
}
24232461
} else if (!wsdl) {
2424-
php_error_docref(NULL, E_ERROR, "'location' and 'uri' options are required in nonWSDL mode");
2462+
soap_client_option_error(this_ptr, "'location' and 'uri' options are required in nonWSDL mode");
2463+
goto end;
24252464
}
24262465

24272466
ZVAL_LONG(Z_CLIENT_SOAP_VERSION_P(this_ptr), soap_version);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
SoapClient constructor option errors are fatal errors with the "exceptions" option disabled
3+
--EXTENSIONS--
4+
soap
5+
--FILE--
6+
<?php
7+
new SoapClient(null, ['location' => 'http://127.0.0.1:9/', 'uri' => 'urn:x', 'exceptions' => false, 'encoding' => 'no-such-encoding']);
8+
echo "not reached\n";
9+
?>
10+
--EXPECTF--
11+
Fatal error: SoapClient::__construct(): Invalid 'encoding' option - 'no-such-encoding' in %s on line %d
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
--TEST--
2+
SoapClient constructor option errors do not disable the cycle collector (GH-10026)
3+
--EXTENSIONS--
4+
soap
5+
--FILE--
6+
<?php
7+
class MyClient extends SoapClient {}
8+
9+
$cases = [
10+
'no uri' => fn() => new SoapClient(null, ['location' => 'http://127.0.0.1:9/']),
11+
'no uri, exceptions off' => fn() => new SoapClient(null, ['location' => 'http://127.0.0.1:9/', 'exceptions' => false]),
12+
'no location' => fn() => new SoapClient(null, ['uri' => 'urn:x']),
13+
'no options' => fn() => new SoapClient(null),
14+
'invalid encoding' => fn() => new SoapClient(null, ['location' => 'http://127.0.0.1:9/', 'uri' => 'urn:x', 'encoding' => 'no-such-encoding']),
15+
'subclass' => fn() => new MyClient(null, ['uri' => 'urn:x']),
16+
];
17+
18+
$fault = new ReflectionProperty(SoapClient::class, '__soap_fault');
19+
foreach ($cases as $name => $case) {
20+
try {
21+
$case();
22+
echo "$name: no exception\n";
23+
} catch (SoapFault $e) {
24+
echo "$name: [{$e->faultcode}] {$e->getMessage()}\n";
25+
}
26+
var_dump(gc_status()['protected']);
27+
}
28+
29+
/* The fault is also stored in __soap_fault, as before */
30+
$client = (new ReflectionClass(SoapClient::class))->newInstanceWithoutConstructor();
31+
try {
32+
$client->__construct(null, ['uri' => 'urn:x']);
33+
} catch (SoapFault $e) {
34+
var_dump($fault->getValue($client) === $e);
35+
}
36+
37+
for ($i = 0; $i < 20000; $i++) {
38+
$node = new stdClass();
39+
$node->self = $node;
40+
}
41+
unset($node);
42+
gc_collect_cycles();
43+
var_dump(gc_status()['collected'] >= 20000);
44+
?>
45+
--EXPECT--
46+
no uri: [Client] SoapClient::__construct(): 'uri' option is required in nonWSDL mode
47+
bool(false)
48+
no uri, exceptions off: [Client] SoapClient::__construct(): 'uri' option is required in nonWSDL mode
49+
bool(false)
50+
no location: [Client] SoapClient::__construct(): 'location' option is required in nonWSDL mode
51+
bool(false)
52+
no options: [Client] SoapClient::__construct(): 'location' and 'uri' options are required in nonWSDL mode
53+
bool(false)
54+
invalid encoding: [Client] SoapClient::__construct(): Invalid 'encoding' option - 'no-such-encoding'
55+
bool(false)
56+
subclass: [Client] SoapClient::__construct(): 'location' option is required in nonWSDL mode
57+
bool(false)
58+
bool(true)
59+
bool(true)

0 commit comments

Comments
 (0)