Skip to content

Commit

Permalink
Handle input being a closed resource
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-davis committed Sep 6, 2024
1 parent bfcbec1 commit f9815bd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ public function parse($input, ?string $contextUri = null, ?string &$rootElementN
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 @@ -156,7 +163,14 @@ public function expect($rootElementName, $input, ?string $contextUri = null)
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
13 changes: 13 additions & 0 deletions tests/Sabre/Xml/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,19 @@ 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+');
self:assertIsResource($stream);

Check failure on line 392 in tests/Sabre/Xml/ServiceTest.php

View workflow job for this annotation

GitHub Actions / PHPUnit (PHP 7.4)

Function assertIsResource not found.

Check failure on line 392 in tests/Sabre/Xml/ServiceTest.php

View workflow job for this annotation

GitHub Actions / PHPUnit (PHP 8.4)

Function assertIsResource not found.
fwrite($stream, $xml);
rewind($stream);
fclose($stream);
$data[] = [$stream];

return $data;
}

Expand Down

0 comments on commit f9815bd

Please sign in to comment.