From f66f0e560b52286215709730df98f4c1df82c2b6 Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Sun, 23 Aug 2020 09:27:44 +0200 Subject: [PATCH] Prevent passing invalid resources. --- src/Filesystem.php | 6 +++--- tests/FilesystemTests.php | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Filesystem.php b/src/Filesystem.php index 0ce24845a..59cc82ee4 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -74,7 +74,7 @@ public function write($path, $contents, array $config = []) */ public function writeStream($path, $resource, array $config = []) { - if ( ! is_resource($resource)) { + if ( ! is_resource($resource) || get_resource_type($resource) !== 'stream') { throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.'); } @@ -107,7 +107,7 @@ public function put($path, $contents, array $config = []) */ public function putStream($path, $resource, array $config = []) { - if ( ! is_resource($resource)) { + if ( ! is_resource($resource) || get_resource_type($resource) !== 'stream') { throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.'); } @@ -158,7 +158,7 @@ public function update($path, $contents, array $config = []) */ public function updateStream($path, $resource, array $config = []) { - if ( ! is_resource($resource)) { + if ( ! is_resource($resource) || get_resource_type($resource) !== 'stream') { throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.'); } diff --git a/tests/FilesystemTests.php b/tests/FilesystemTests.php index e9a651123..1d08d7a57 100644 --- a/tests/FilesystemTests.php +++ b/tests/FilesystemTests.php @@ -156,6 +156,26 @@ public function testPutStreamInvalid() $this->filesystem->putStream('path.txt', '__INVALID__'); } + /** + * @dataProvider methodsThatGuardAgainstClosedResources + */ + public function testSupplyingClosedStreams($method) + { + $handle = tmpfile(); + fclose($handle); + $this->expectException('InvalidArgumentException'); + $this->filesystem->{$method}('path.txt', $handle); + } + + public function methodsThatGuardAgainstClosedResources() + { + return [ + ['putStream'], + ['writeStream'], + ['updateStream'], + ]; + } + public function testWriteStreamInvalid() { $this->expectException('InvalidArgumentException');