Skip to content

Commit 9f64ae0

Browse files
authored
Merge pull request #298 from phil-davis/backport-296-to-2.2
Backport fix for "Passing a closed resource to Service parse or expect" to 2.2
2 parents 0734b7a + fc72abd commit 9f64ae0

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
}
4545
},
4646
"require-dev": {
47-
"friendsofphp/php-cs-fixer": "~2.17.1||^3.63",
47+
"friendsofphp/php-cs-fixer": "~2.17.1||3.63.2",
4848
"phpstan/phpstan": "^0.12",
4949
"phpunit/phpunit" : "^7.5 || ^8.5 || ^9.6"
5050
},

lib/Service.php

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

120127
// If input is empty, then it's safe to throw an exception
@@ -155,10 +162,17 @@ public function parse($input, ?string $contextUri = null, ?string &$rootElementN
155162
*/
156163
public function expect($rootElementName, $input, ?string $contextUri = null)
157164
{
158-
if (is_resource($input)) {
165+
if (!is_string($input)) {
159166
// Unfortunately the XMLReader doesn't support streams. When it
160167
// does, we can optimize this.
161-
$input = (string) stream_get_contents($input);
168+
if (is_resource($input)) {
169+
$input = (string) stream_get_contents($input);
170+
} else {
171+
// Input is not a string and not a resource.
172+
// Therefore, it has to be a closed resource.
173+
// Effectively empty input has been passed in.
174+
$input = '';
175+
}
162176
}
163177

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

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ parameters:
55
-
66
message: '!Parameter #3 \$namespace of method XMLWriter::startElementNs\(\) expects string, null given.!'
77
path: lib/Writer.php
8+
-
9+
message: '!Else branch is unreachable because previous condition is always true.!'
10+
path: lib/Service.php

tests/Sabre/Xml/ServiceTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,18 @@ public function providesEmptyInput()
365365
$data[] = [$emptyResource];
366366
$data[] = [''];
367367

368+
// Also test trying to parse a resource stream that has already been closed.
369+
$xml = <<<XML
370+
<root xmlns="http://sabre.io/ns">
371+
<child>value</child>
372+
</root>
373+
XML;
374+
$stream = fopen('php://memory', 'r+');
375+
fwrite($stream, $xml);
376+
rewind($stream);
377+
fclose($stream);
378+
$data[] = [$stream];
379+
368380
return $data;
369381
}
370382

0 commit comments

Comments
 (0)