Skip to content

Commit

Permalink
Merge 0a2e17a into e571a26
Browse files Browse the repository at this point in the history
  • Loading branch information
larowlan committed Aug 2, 2019
2 parents e571a26 + 0a2e17a commit 9c9e96c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Stream/ReadOnlyFile.php
Expand Up @@ -107,6 +107,17 @@ public function __construct($file, Key $key = null)
$this->fp = $file;
$this->pos = \ftell($this->fp);
$this->stat = \fstat($this->fp);
if (!$this->stat) {
// The resource is remote or a stream wrapper like php://input
$this->stat = [
'size' => 0,
];
\fseek($this->fp, 0);
while (!feof($this->fp)) {
$this->stat['size'] += \strlen(\fread($this->fp, 8192));
}
\fseek($this->fp, $this->pos);
}
} else {
throw new InvalidType(
'Argument 1: Expected a filename or resource'
Expand Down Expand Up @@ -311,6 +322,17 @@ public function toctouTest()
// @codeCoverageIgnoreEnd
}
$stat = \fstat($this->fp);
if (!$stat) {
// The resource is remote or a stream wrapper like php://input
$stat = [
'size' => 0,
];
\fseek($this->fp, 0);
while (!feof($this->fp)) {
$stat['size'] += \strlen(\fread($this->fp, 8192));
}
\fseek($this->fp, $this->pos);
}
if ($stat['size'] !== $this->stat['size']) {
throw new FileModified(
'Read-only file has been modified since it was opened for reading'
Expand Down
45 changes: 45 additions & 0 deletions test/unit/FileTest.php
Expand Up @@ -338,6 +338,51 @@ public function testSeal()
unlink(__DIR__.'/tmp/paragon_avatar.opened.png');
}

/**
* @throws CryptoException\CannotPerformOperation
* @throws CryptoException\FileAccessDenied
* @throws CryptoException\FileError
* @throws CryptoException\FileModified
* @throws CryptoException\InvalidDigestLength
* @throws CryptoException\InvalidMessage
* @throws CryptoException\InvalidType
* @throws Exception
* @throws TypeError
*/
public function testSealFromInput()
{

touch(__DIR__.'/tmp/input.sealed.png');
chmod(__DIR__.'/tmp/input.sealed.png', 0777);
touch(__DIR__.'/tmp/input.opened.png');
chmod(__DIR__.'/tmp/input.opened.png', 0777);

$keypair = KeyFactory::generateEncryptionKeyPair();
$secretkey = $keypair->getSecretKey();
$publickey = $keypair->getPublicKey();

$stream = fopen('php://input', 'rb');
File::seal(
$stream,
__DIR__.'/tmp/input.sealed.png',
$publickey
);

File::unseal(
__DIR__.'/tmp/input.sealed.png',
__DIR__.'/tmp/input.opened.png',
$secretkey
);

$this->assertSame(
hash('sha256', file_get_contents('php://input')),
hash_file('sha256', __DIR__.'/tmp/input.opened.png')
);

unlink(__DIR__.'/tmp/input.sealed.png');
unlink(__DIR__.'/tmp/input.opened.png');
}

/**
* @throws CryptoException\CannotPerformOperation
* @throws CryptoException\FileAccessDenied
Expand Down

0 comments on commit 9c9e96c

Please sign in to comment.