Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport fix for "Passing a closed resource to Service parse or expect" to 2.2 #298

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
}
},
"require-dev": {
"friendsofphp/php-cs-fixer": "~2.17.1||^3.63",
"friendsofphp/php-cs-fixer": "~2.17.1||3.63.2",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit" : "^7.5 || ^8.5 || ^9.6"
},
Expand Down
22 changes: 18 additions & 4 deletions lib/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,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 @@ -155,10 +162,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
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ parameters:
-
message: '!Parameter #3 \$namespace of method XMLWriter::startElementNs\(\) expects string, null given.!'
path: lib/Writer.php
-
message: '!Else branch is unreachable because previous condition is always true.!'
path: lib/Service.php
12 changes: 12 additions & 0 deletions tests/Sabre/Xml/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,18 @@ public function providesEmptyInput()
$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
Loading