Skip to content

Commit

Permalink
Merge pull request #301 from phil-davis/backport-296-to-v3
Browse files Browse the repository at this point in the history
Backport fix for "Passing a closed resource to Service parse or expect" to v3
  • Loading branch information
phil-davis authored Sep 6, 2024
2 parents b04f29d + 4abd5da commit 4d272f2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,17 @@ public function getWriter(): Writer
*/
public function parse($input, ?string $contextUri = null, ?string &$rootElementName = null)
{
if (is_resource($input)) {
if (!is_string($input)) {
// Unfortunately the XMLReader doesn't support streams. When it
// does, we can optimize this.
$input = (string) stream_get_contents($input);
if (is_resource($input)) {
$input = (string) stream_get_contents($input);
} else {
// Input is not a string and not a resource.
// Therefore, it has to be a closed resource.
// Effectively empty input has been passed in.
$input = '';
}
}

// If input is empty, then it's safe to throw an exception
Expand Down Expand Up @@ -153,10 +160,17 @@ public function parse($input, ?string $contextUri = null, ?string &$rootElementN
*/
public function expect($rootElementName, $input, ?string $contextUri = null)
{
if (is_resource($input)) {
if (!is_string($input)) {
// Unfortunately the XMLReader doesn't support streams. When it
// does, we can optimize this.
$input = (string) stream_get_contents($input);
if (is_resource($input)) {
$input = (string) stream_get_contents($input);
} else {
// Input is not a string and not a resource.
// Therefore, it has to be a closed resource.
// Effectively empty input has been passed in.
$input = '';
}
}

// If input is empty, then it's safe to throw an exception
Expand Down
12 changes: 12 additions & 0 deletions tests/Sabre/Xml/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,18 @@ public function providesEmptyInput(): array
$data[] = [$emptyResource];
$data[] = [''];

// Also test trying to parse a resource stream that has already been closed.
$xml = <<<XML
<root xmlns="http://sabre.io/ns">
<child>value</child>
</root>
XML;
$stream = fopen('php://memory', 'r+');
fwrite($stream, $xml);
rewind($stream);
fclose($stream);
$data[] = [$stream];

return $data;
}

Expand Down

0 comments on commit 4d272f2

Please sign in to comment.