Skip to content

Commit 4d272f2

Browse files
authored
Merge pull request #301 from phil-davis/backport-296-to-v3
Backport fix for "Passing a closed resource to Service parse or expect" to v3
2 parents b04f29d + 4abd5da commit 4d272f2

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/Service.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,17 @@ public function getWriter(): Writer
109109
*/
110110
public function parse($input, ?string $contextUri = null, ?string &$rootElementName = null)
111111
{
112-
if (is_resource($input)) {
112+
if (!is_string($input)) {
113113
// Unfortunately the XMLReader doesn't support streams. When it
114114
// does, we can optimize this.
115-
$input = (string) stream_get_contents($input);
115+
if (is_resource($input)) {
116+
$input = (string) stream_get_contents($input);
117+
} else {
118+
// Input is not a string and not a resource.
119+
// Therefore, it has to be a closed resource.
120+
// Effectively empty input has been passed in.
121+
$input = '';
122+
}
116123
}
117124

118125
// If input is empty, then it's safe to throw an exception
@@ -153,10 +160,17 @@ public function parse($input, ?string $contextUri = null, ?string &$rootElementN
153160
*/
154161
public function expect($rootElementName, $input, ?string $contextUri = null)
155162
{
156-
if (is_resource($input)) {
163+
if (!is_string($input)) {
157164
// Unfortunately the XMLReader doesn't support streams. When it
158165
// does, we can optimize this.
159-
$input = (string) stream_get_contents($input);
166+
if (is_resource($input)) {
167+
$input = (string) stream_get_contents($input);
168+
} else {
169+
// Input is not a string and not a resource.
170+
// Therefore, it has to be a closed resource.
171+
// Effectively empty input has been passed in.
172+
$input = '';
173+
}
160174
}
161175

162176
// If input is empty, then it's safe to throw an exception

tests/Sabre/Xml/ServiceTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,18 @@ public function providesEmptyInput(): array
382382
$data[] = [$emptyResource];
383383
$data[] = [''];
384384

385+
// Also test trying to parse a resource stream that has already been closed.
386+
$xml = <<<XML
387+
<root xmlns="http://sabre.io/ns">
388+
<child>value</child>
389+
</root>
390+
XML;
391+
$stream = fopen('php://memory', 'r+');
392+
fwrite($stream, $xml);
393+
rewind($stream);
394+
fclose($stream);
395+
$data[] = [$stream];
396+
385397
return $data;
386398
}
387399

0 commit comments

Comments
 (0)