From bfcbec1aff29783a1deb8a58d967b4ad4e228932 Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Thu, 5 Sep 2024 20:29:18 +0545 Subject: [PATCH 1/3] check if parser input is not a string --- lib/Service.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Service.php b/lib/Service.php index 2ce8a8f..66ec010 100644 --- a/lib/Service.php +++ b/lib/Service.php @@ -109,7 +109,7 @@ 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); @@ -153,7 +153,7 @@ 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); From f9815bd0298d49c2e6fa2538da45e265c173d3d7 Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Thu, 5 Sep 2024 20:50:53 +0545 Subject: [PATCH 2/3] Handle input being a closed resource --- lib/Service.php | 18 ++++++++++++++++-- tests/Sabre/Xml/ServiceTest.php | 13 +++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/Service.php b/lib/Service.php index 66ec010..e7c0e81 100644 --- a/lib/Service.php +++ b/lib/Service.php @@ -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 @@ -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 diff --git a/tests/Sabre/Xml/ServiceTest.php b/tests/Sabre/Xml/ServiceTest.php index aa225b5..fae475b 100644 --- a/tests/Sabre/Xml/ServiceTest.php +++ b/tests/Sabre/Xml/ServiceTest.php @@ -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 = << + value + +XML; + $stream = fopen('php://memory', 'r+'); + self:assertIsResource($stream); + fwrite($stream, $xml); + rewind($stream); + fclose($stream); + $data[] = [$stream]; + return $data; } From 4abd5da9ab5008bde541c4d67e29ba842cc7590f Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Fri, 6 Sep 2024 13:30:30 +0545 Subject: [PATCH 3/3] Remove unknown assertIsResource from ServiceTest --- tests/Sabre/Xml/ServiceTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Sabre/Xml/ServiceTest.php b/tests/Sabre/Xml/ServiceTest.php index fae475b..efd79f9 100644 --- a/tests/Sabre/Xml/ServiceTest.php +++ b/tests/Sabre/Xml/ServiceTest.php @@ -389,7 +389,6 @@ public function providesEmptyInput(): array XML; $stream = fopen('php://memory', 'r+'); - self:assertIsResource($stream); fwrite($stream, $xml); rewind($stream); fclose($stream);