File tree Expand file tree Collapse file tree
user_guide_src/source/changelogs Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -57,6 +57,17 @@ public static function forInvalidMime(string $mime)
5757 return new static (lang ('Format.invalidMime ' , [$ mime ]));
5858 }
5959
60+ /**
61+ * Thrown in XMLFormatter when a value cannot be converted
62+ * to a string.
63+ *
64+ * @return static
65+ */
66+ public static function forInvalidXMLValue (string $ type )
67+ {
68+ return new static (lang ('Format.invalidXMLValue ' , [$ type ]));
69+ }
70+
6071 /**
6172 * Thrown on XMLFormatter when the `simplexml` extension
6273 * is not installed.
Original file line number Diff line number Diff line change 1616use CodeIgniter \Format \Exceptions \FormatException ;
1717use Config \Format ;
1818use SimpleXMLElement ;
19+ use Stringable ;
1920
2021/**
2122 * XML data formatter
@@ -68,7 +69,11 @@ protected function arrayToXML(array $data, &$output)
6869 $ subnode = $ output ->addChild ("{$ key }" );
6970 $ this ->arrayToXML ($ value , $ subnode );
7071 } else {
71- $ output ->addChild ("{$ key }" , htmlspecialchars ("{$ value }" ));
72+ if (is_object ($ value ) && ! $ value instanceof Stringable) {
73+ throw FormatException ::forInvalidXMLValue ($ value ::class);
74+ }
75+
76+ $ output ->addChild ("{$ key }" , htmlspecialchars ((string ) $ value ));
7277 }
7378 }
7479 }
@@ -98,6 +103,8 @@ protected function normalizeXMLTag($key)
98103 $ key = preg_replace ("/[^ {$ validName }-]+/u " , '' , $ key );
99104 $ key = preg_replace ("/^[^ {$ startChar }]+/u " , 'item$0 ' , $ key );
100105
101- return preg_replace ('/^(xml).*/iu ' , 'item$0 ' , $ key ); // XML is a reserved starting word
106+ $ key = preg_replace ('/^(xml).*/iu ' , 'item$0 ' , $ key ); // XML is a reserved starting word
107+
108+ return $ key === '' ? 'item ' : $ key ;
102109 }
103110}
Original file line number Diff line number Diff line change 1616 'invalidFormatter ' => '"{0}" is not a valid Formatter class. ' ,
1717 'invalidJSON ' => 'Failed to parse JSON string. Error: {0} ' ,
1818 'invalidMime ' => 'No Formatter defined for mime type: "{0}". ' ,
19+ 'invalidXMLValue ' => 'Cannot convert the value of type "{0}" to a string for XML output. ' ,
1920 'missingExtension ' => 'The SimpleXML extension is required to format XML. ' ,
2021];
Original file line number Diff line number Diff line change 1313
1414namespace CodeIgniter \Format ;
1515
16+ use CodeIgniter \Format \Exceptions \FormatException ;
1617use CodeIgniter \Test \CIUnitTestCase ;
1718use DOMDocument ;
1819use PHPUnit \Framework \Attributes \DataProvider ;
1920use PHPUnit \Framework \Attributes \Group ;
21+ use stdClass ;
22+ use Stringable ;
2023
2124/**
2225 * @internal
@@ -152,6 +155,51 @@ public static function provideValidatingInvalidTags(): iterable
152155 ];
153156 }
154157
158+ public function testEmptyTagNameAfterSanitization (): void
159+ {
160+ $ data = [
161+ '!@# ' => 'value ' ,
162+ ];
163+
164+ $ expected = <<<'EOH'
165+ <?xml version="1.0"?>
166+ <response><item>value</item></response>
167+
168+ EOH;
169+
170+ $ this ->assertSame ($ expected , $ this ->xmlFormatter ->format ($ data ));
171+ }
172+
173+ public function testStringableObjectValue (): void
174+ {
175+ $ value = new class () implements Stringable {
176+ public function __toString (): string
177+ {
178+ return 'bar ' ;
179+ }
180+ };
181+
182+ $ data = [
183+ 'foo ' => $ value ,
184+ ];
185+
186+ $ expected = <<<'EOH'
187+ <?xml version="1.0"?>
188+ <response><foo>bar</foo></response>
189+
190+ EOH;
191+
192+ $ this ->assertSame ($ expected , $ this ->xmlFormatter ->format ($ data ));
193+ }
194+
195+ public function testNonStringableObjectValueThrows (): void
196+ {
197+ $ this ->expectException (FormatException::class);
198+ $ this ->expectExceptionMessage ('Cannot convert the value of type "stdClass" to a string for XML output. ' );
199+
200+ $ this ->xmlFormatter ->format (['foo ' => new stdClass ()]);
201+ }
202+
155203 public function testDeepNestedArrayToXml (): void
156204 {
157205 $ data = [
Original file line number Diff line number Diff line change @@ -18,6 +18,8 @@ BREAKING
1818Message Changes
1919***************
2020
21+ - Added the ``Format.invalidXMLValue `` language string.
22+
2123*******
2224Changes
2325*******
@@ -31,6 +33,11 @@ Bugs Fixed
3133**********
3234
3335- **Commands: ** Fixed a bug where generator commands did not sort the imports of a template with CRLF line endings.
36+ - **Format: ** Fixed ``XMLFormatter `` so that an array key that becomes empty after
37+ sanitization no longer triggers a ``ValueError ``; the fallback tag name ``item ``
38+ is used instead. Values that cannot be converted to a string, such as objects
39+ not implementing ``Stringable ``, now throw a ``FormatException `` instead of a
40+ fatal ``Error ``.
3441- **Security: ** Fixed a bug where a valid JSON request body that is not an object
3542 (for example a JSON array) was corrupted after CSRF verification when the
3643 token was sent via the ``X-CSRF-TOKEN `` header.
You can’t perform that action at this time.
0 commit comments