Skip to content

Commit

Permalink
🔒️ Fixed Best Practice, Bug, and Security Issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sugeng-sulistiyawan committed Oct 20, 2024
1 parent e627b10 commit 57d795d
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 78 deletions.
17 changes: 17 additions & 0 deletions src/AbstractComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use League\Flysystem\PathNormalizer;
use League\Flysystem\WhitespacePathNormalizer;
use yii\base\Component;
use yii\base\InvalidConfigException;

/**
* Class AbstractComponent
Expand Down Expand Up @@ -132,4 +133,20 @@ public function convertToDateTime($dateValue)
* @return FilesystemAdapter
*/
abstract protected function initAdapter();

/**
* Validate property
*
* @param array $properties
* @return void
* @throws InvalidConfigException
*/
public function validateProperties(array $properties = [])
{
foreach ($properties as $property) {
if (empty($this->$property)) {
throw new InvalidConfigException("The \"$property\" property must be set.");
}
}
}
}
15 changes: 6 additions & 9 deletions src/AsyncAwsS3Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,15 @@ class AsyncAwsS3Component extends AbstractComponent

/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init()
{
if (empty($this->accessKeyId)) {
throw new InvalidConfigException('The "accessKeyId" property must be set.');
}
if (empty($this->accessKeySecret)) {
throw new InvalidConfigException('The "accessKeySecret" property must be set.');
}
if (empty($this->bucket)) {
throw new InvalidConfigException('The "bucket" property must be set.');
}
$this->validateProperties([
'bucket',
'accessKeyId',
'accessKeySecret',
]);

parent::init();
}
Expand Down
14 changes: 5 additions & 9 deletions src/AwsS3Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,16 @@ class AwsS3Component extends AbstractComponent

/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init()
{
$properties = ['bucket'];
if (empty($this->credentials)) {
if (empty($this->key)) {
throw new InvalidConfigException('The "key" property must be set.');
}
if (empty($this->secret)) {
throw new InvalidConfigException('The "secret" property must be set.');
}
}
if (empty($this->bucket)) {
throw new InvalidConfigException('The "bucket" property must be set.');
$properties[] = 'key';
$properties[] = 'secret';
}
$this->validateProperties($properties);

parent::init();
}
Expand Down
7 changes: 4 additions & 3 deletions src/GoogleCloudStorageComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,13 @@ class GoogleCloudStorageComponent extends AbstractComponent

/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init()
{
if (empty($this->bucket)) {
throw new InvalidConfigException('The "bucket" property must be set.');
}
$this->validateProperties([
'bucket',
]);

parent::init();
}
Expand Down
50 changes: 26 additions & 24 deletions src/GoogleDriveComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,26 @@ class GoogleDriveComponent extends AbstractComponent
*/
protected $client;

/**
* @var string[]
*/
protected $_availableOptions = [
'teamDriveId',
'sharedFolderId',
];

/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init()
{
if (empty($this->clientId)) {
throw new InvalidConfigException('The "clientId" property must be set.');
}

if (empty($this->clientSecret)) {
throw new InvalidConfigException('The "clientSecret" property must be set.');
}

if (empty($this->refreshToken)) {
throw new InvalidConfigException('The "refreshToken" property must be set.');
}

if (!empty($this->teamDriveId)) {
$this->options['teamDriveId'] = $this->teamDriveId;
}

if (!empty($this->sharedFolderId)) {
$this->options['sharedFolderId'] = $this->sharedFolderId;
}

if (empty($this->secret)) {
throw new InvalidConfigException('The "secret" property must be set.');
}
$this->validateProperties([
'clientId',
'clientSecret',
'refreshToken',
'secret',
]);

$this->initEncrypter($this->secret);

Expand All @@ -131,9 +123,19 @@ protected function initAdapter()
$client->setApplicationName($this->applicationName);
}

foreach ($this->_availableOptions as $property) {
if (!empty($this->$property)) {
$this->options[$property] = $this->$property;
}
}

$this->client = $client;
$service = new Drive($this->client);

return new GoogleDriveAdapter($service, $this->prefix, $this->options);
$adapter = new GoogleDriveAdapter($service, $this->prefix, $this->options);
// for UrlGeneratorAdapterTrait
$adapter->component = $this;

return $adapter;
}
}
11 changes: 5 additions & 6 deletions src/LocalComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ class LocalComponent extends AbstractComponent

/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init()
{
if (empty($this->path)) {
throw new InvalidConfigException('The "path" property must be set.');
}
if (empty($this->secret)) {
throw new InvalidConfigException('The "secret" property must be set.');
}
$this->validateProperties([
'path',
'secret',
]);

$this->initEncrypter($this->secret);

Expand Down
11 changes: 5 additions & 6 deletions src/SftpComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,14 @@ class SftpComponent extends AbstractComponent

/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init()
{
if (empty($this->host)) {
throw new InvalidConfigException('The "host" property must be set.');
}
if (empty($this->username)) {
throw new InvalidConfigException('The "username" property must be set.');
}
$this->validateProperties([
'host',
'username',
]);

$this->passphrase = $this->passphrase ?: ($this->password ?: ($this->username ?: Yii::$app->id));
$this->initEncrypter($this->passphrase);
Expand Down
7 changes: 4 additions & 3 deletions src/WebDavComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ class WebDavComponent extends AbstractComponent

/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init()
{
if (empty($this->baseUri)) {
throw new InvalidConfigException('The "baseUri" property must be set.');
}
$this->validateProperties([
'baseUri',
]);

parent::init();
}
Expand Down
11 changes: 5 additions & 6 deletions src/ZipArchiveComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ class ZipArchiveComponent extends AbstractComponent

/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init()
{
if (empty($this->pathToZip)) {
throw new InvalidConfigException('The "pathToZip" property must be set.');
}
if (empty($this->secret)) {
throw new InvalidConfigException('The "secret" property must be set.');
}
$this->validateProperties([
'pathToZip',
'secret',
]);

$this->initEncrypter($this->secret);

Expand Down
2 changes: 1 addition & 1 deletion src/actions/FileAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function run($data = null)
$filesystem = Yii::$app->get($this->component);

try {
$params = Json::decode($filesystem->decrypt($data));
$params = Json::decode($filesystem?->decrypt($data));

$now = (int) (new DateTimeImmutable())->getTimestamp();
$expires = (int) $params['expires'];
Expand Down
10 changes: 6 additions & 4 deletions src/adapter/ZipArchiveAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ public function setVisibility(string $path, string $visibility): void
{
$archive = $this->zipArchiveProvider->createZipArchive();
$location = $this->pathPrefixer->prefixPath($path);

/** @var array|false $stats */
$stats = $archive->statName($location) ?: $archive->statName($location . '/');

if ($stats === false) {
Expand Down Expand Up @@ -377,7 +379,7 @@ public function copy(string $source, string $destination, Config $config): void
$this->writeStream($destination, $readStream, $config);
} catch (Throwable $exception) {
if (isset($readStream)) {
@fclose($readStream);
/** @scrutinizer ignore-unhandled */ @fclose($readStream);
}

throw UnableToCopyFile::fromLocationTo($source, $destination, $exception);
Expand Down Expand Up @@ -452,7 +454,7 @@ private function setVisibilityAttribute(string $statsName, string $visibility, Z
// =================================================

/**
* @var UrlGeneratorComponentTrait|AbstractComponent
* @var AbstractComponent|UrlGeneratorComponentTrait
*/
public $component;

Expand All @@ -464,7 +466,7 @@ public function publicUrl(string $path, Config $config): string
'expires' => 0,
];

return Url::toRoute([$this->component->action, 'data' => $this->component->encrypt(Json::encode($params))], true);
return Url::toRoute([$this->component?->action, 'data' => $this->component?->encrypt(Json::encode($params))], true);
}

public function temporaryUrl(string $path, DateTimeInterface $expiresAt, Config $config): string
Expand All @@ -475,6 +477,6 @@ public function temporaryUrl(string $path, DateTimeInterface $expiresAt, Config
'expires' => (int) $expiresAt->getTimestamp(),
];

return Url::toRoute([$this->component->action, 'data' => $this->component->encrypt(Json::encode($params))], true);
return Url::toRoute([$this->component?->action, 'data' => $this->component?->encrypt(Json::encode($params))], true);
}
}
14 changes: 7 additions & 7 deletions src/traits/UrlGeneratorAdapterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,37 @@
trait UrlGeneratorAdapterTrait
{
/**
* @var UrlGeneratorComponentTrait|AbstractComponent
* @var AbstractComponent|UrlGeneratorComponentTrait
*/
public $component;

public function publicUrl(string $path, /** @scrutinizer ignore-unused */Config $config): string
{
// TODO: Use absolute path and don't encrypt
if ($this->component->prefix) {
$prefixer = new PathPrefixer($this->component->prefix);
if ($this->component?->prefix) {
$prefixer = new PathPrefixer((string) $this->component?->prefix);
$path = $prefixer->stripPrefix($path);
}
$params = [
'path' => $path,
'expires' => 0,
];

return Url::toRoute([$this->component->action, 'data' => $this->component->encrypt(Json::encode($params))], true);
return Url::toRoute([$this->component?->action, 'data' => $this->component?->encrypt(Json::encode($params))], true);
}

public function temporaryUrl(string $path, DateTimeInterface $expiresAt, /** @scrutinizer ignore-unused */Config $config): string
{
// TODO: Use absolute path and don't encrypt
if ($this->component->prefix) {
$prefixer = new PathPrefixer($this->component->prefix);
if ($this->component?->prefix) {
$prefixer = new PathPrefixer((string) $this->component?->prefix);
$path = $prefixer->stripPrefix($path);
}
$params = [
'path' => $path,
'expires' => (int) $expiresAt->getTimestamp(),
];

return Url::toRoute([$this->component->action, 'data' => $this->component->encrypt(Json::encode($params))], true);
return Url::toRoute([$this->component?->action, 'data' => $this->component?->encrypt(Json::encode($params))], true);
}
}

0 comments on commit 57d795d

Please sign in to comment.