-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server side checksum verification #26655
- Loading branch information
Showing
5 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
/** | ||
* @author Ilja Neumann <[email protected]> | ||
* | ||
* @copyright Copyright (c) 2017, ownCloud GmbH. | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
namespace OC\Files\Storage\Wrapper; | ||
|
||
use OC\Files\Stream\Checksum as ChecksumStream; | ||
|
||
/** | ||
* Class Checksum | ||
* | ||
* Computes checksums (default: SHA1) on all files under the /files path. | ||
* The resulting checksum can be retrieved by call getMetadata($path) | ||
* | ||
* @package OC\Files\Storage\Wrapper | ||
*/ | ||
class Checksum extends Wrapper { | ||
|
||
/** | ||
* @param array $parameters | ||
*/ | ||
public function __construct($parameters) { | ||
if (isset($parameters['algo'])) { | ||
ChecksumStream::setAlgo($parameters['algo']); | ||
} | ||
parent::__construct($parameters); | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @param string $mode | ||
* @return false|resource | ||
*/ | ||
public function fopen($path, $mode) { | ||
$stream = $this->getWrapperStorage()->fopen($path, $mode); | ||
if (!self::requiresChecksum($path)) { | ||
return $stream; | ||
} | ||
|
||
return \OC\Files\Stream\Checksum::wrap($stream, $path); | ||
} | ||
|
||
|
||
/** | ||
* Checksum is only required for everything under files/ | ||
* @param $path | ||
* @return bool | ||
*/ | ||
private static function requiresChecksum($path) { | ||
return substr($path, 0, 6) === 'files/'; | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @param string $data | ||
* @return bool | ||
*/ | ||
public function file_put_contents($path, $data) { | ||
return parent::file_put_contents($path, $data); | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @return array | ||
*/ | ||
public function getMetaData($path) { | ||
$parentMetaData = parent::getMetaData($path); | ||
$parentMetaData['checksum'] = ChecksumStream::getChecksum($path); | ||
|
||
return $parentMetaData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
|
||
/** | ||
* @author Ilja Neumann <[email protected]> | ||
* | ||
* @copyright Copyright (c) 2017, ownCloud GmbH. | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace OC\Files\Stream; | ||
|
||
|
||
use Icewind\Streams\Wrapper; | ||
|
||
class Checksum extends Wrapper { | ||
|
||
private static $algo = 'sha1'; | ||
|
||
/** @var resource */ | ||
private $hashCtx; | ||
|
||
/** @var array Key is path, value is the checksum */ | ||
private static $checksums = []; | ||
|
||
public function __construct() { | ||
$this->hashCtx = hash_init(self::$algo); | ||
} | ||
|
||
|
||
/** | ||
* @param $source | ||
* @param $path | ||
* @return resource | ||
*/ | ||
public static function wrap($source, $path) { | ||
$context = stream_context_create([ | ||
'occhecksum' => [ | ||
'source' => $source, | ||
'path' => $path | ||
] | ||
]); | ||
|
||
return Wrapper::wrapSource( | ||
$source, $context, 'occhecksum', self::class | ||
); | ||
} | ||
|
||
|
||
/** | ||
* @param string $path | ||
* @param array $options | ||
* @return bool | ||
*/ | ||
public function dir_opendir($path, $options) { | ||
return true; | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @param string $mode | ||
* @param int $options | ||
* @param string $opened_path | ||
* @return bool | ||
*/ | ||
public function stream_open($path, $mode, $options, &$opened_path) { | ||
$context = parent::loadContext('occhecksum'); | ||
$this->setSourceStream($context['source']); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param int $count | ||
* @return string | ||
*/ | ||
public function stream_read($count) { | ||
$data = parent::stream_read($count); | ||
hash_update($this->hashCtx, $data); | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @param string $data | ||
* @return int | ||
*/ | ||
public function stream_write($data) { | ||
hash_update($this->hashCtx, $data); | ||
|
||
return parent::stream_write($data); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function stream_close() { | ||
self::$checksums[$this->getPathFromContext()] = sprintf( | ||
'%s:%s', strtoupper(self::$algo), hash_final($this->hashCtx) | ||
); | ||
|
||
return parent::stream_close(); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
* @return string | ||
*/ | ||
private function getPathFromContext() { | ||
$ctx = stream_context_get_options($this->context); | ||
|
||
return $ctx['occhecksum']['path']; | ||
} | ||
|
||
/** | ||
* @param $path | ||
* @return string | ||
*/ | ||
public static function getChecksum($path) { | ||
if (!isset(self::$checksums[$path])) { | ||
return null; | ||
} | ||
|
||
return self::$checksums[$path]; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public static function getChecksums() { | ||
return self::$checksums; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function getAlgo() { | ||
return self::$algo; | ||
} | ||
|
||
/** | ||
* @param string $algo | ||
*/ | ||
public static function setAlgo($algo) { | ||
self::$algo = $algo; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters