From b4ae29d0b3da483406542b598384a7aee689ac54 Mon Sep 17 00:00:00 2001 From: Netkas Date: Sat, 19 Aug 2023 04:48:23 -0400 Subject: [PATCH] - Implemented interface `BytecodeObjectInterface` into `\ncc\Objects > Package` - Implemented interface `BytecodeObjectInterface` into `\ncc\Objects > Vault` - Corrected code-smell and code style issues in `\ncc\Objects > Vault` - Implemented interface `BytecodeObjectInterface` into `\ncc\Objects > ProjectConfiguration` - Removed unused class `\ncc\Objects > SymlinkDictionary` --- CHANGELOG.md | 6 + .../Interfaces/BytecodeObjectInterface.php | 42 ++++ src/ncc/Managers/CredentialManager.php | 4 +- src/ncc/Objects/Package.php | 13 +- src/ncc/Objects/ProjectConfiguration.php | 215 +++++++++--------- src/ncc/Objects/SymlinkDictionary.php | 28 --- src/ncc/Objects/Vault.php | 69 +++--- 7 files changed, 195 insertions(+), 182 deletions(-) create mode 100644 src/ncc/Interfaces/BytecodeObjectInterface.php delete mode 100644 src/ncc/Objects/SymlinkDictionary.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b68eade..43a610b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `LICENSE.md` & `license.md` are now detected as license files in `\ncc\Classes\ComposerExtension > ComposerSourceBuiltin > convertProject()` - Added new exception `PathNotFoundException` and implemented it in replacement for `DirectoryNotFoundException` and `FileNotFoundException` in `\ncc\Exceptions` + - Added a new interface class `BytecodeObjectInterface` which will be used to implement object types for compiled assets ### Fixed - Fixed MITM attack vector in `\ncc\Classes > HttpClient > prepareCurl()` @@ -79,12 +80,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Corrected code-smell and code style issues in `\ncc\Classes\Extensions\PythonExtension > Python3Runner` - Corrected code-smell and code style issues in `\ncc\Classes\Extensions\PythonExtension > PythonRunner` - Corrected code-smell and code style issues in `\ncc\Interfaces > RunnerInterface` + - Implemented interface `BytecodeObjectInterface` into `\ncc\Objects > Package` + - Implemented interface `BytecodeObjectInterface` into `\ncc\Objects > Vault` + - Corrected code-smell and code style issues in `\ncc\Objects > Vault` + - Implemented interface `BytecodeObjectInterface` into `\ncc\Objects > ProjectConfiguration` ## Removed - Removed `FileNotFoundException` and `DirectoryNotFoundException` from `\ncc\Exceptions` - Removed the use of `InvalidScopeException` across the project - Removed references of Win32 from the project as Windows is not going supported - Removed unused exception `FileNotFoundException` in `\ncc\CLI > HelpMenu` + - Removed unused class `\ncc\Objects > SymlinkDictionary` diff --git a/src/ncc/Interfaces/BytecodeObjectInterface.php b/src/ncc/Interfaces/BytecodeObjectInterface.php new file mode 100644 index 0000000..1536dd1 --- /dev/null +++ b/src/ncc/Interfaces/BytecodeObjectInterface.php @@ -0,0 +1,42 @@ +Version = Versions::CREDENTIALS_STORE_VERSION; + $VaultObject->version = Versions::CREDENTIALS_STORE_VERSION; IO::fwrite($this->store_path, ZiProto::encode($VaultObject->toArray()), 0744); } @@ -127,7 +127,7 @@ $VaultArray = ZiProto::decode(IO::fread($this->store_path)); $VaultObject = Vault::fromArray($VaultArray); - if($VaultObject->Version !== Versions::CREDENTIALS_STORE_VERSION) + if($VaultObject->version !== Versions::CREDENTIALS_STORE_VERSION) { throw new RuntimeException('Credentials store version mismatch'); } diff --git a/src/ncc/Objects/Package.php b/src/ncc/Objects/Package.php index 5c2ede3..9c5919a 100644 --- a/src/ncc/Objects/Package.php +++ b/src/ncc/Objects/Package.php @@ -32,6 +32,7 @@ use ncc\Exceptions\IOException; use ncc\Exceptions\PackageParsingException; use ncc\Exceptions\PathNotFoundException; + use ncc\Interfaces\BytecodeObjectInterface; use ncc\Objects\Package\Component; use ncc\Objects\Package\ExecutionUnit; use ncc\Objects\Package\Header; @@ -44,7 +45,7 @@ use ncc\Utilities\IO; use ncc\ZiProto\ZiProto; - class Package + class Package implements BytecodeObjectInterface { /** * The parsed magic bytes of the package into an object representation @@ -347,10 +348,7 @@ } /** - * Constructs an array representation of the object - * - * @param bool $bytecode - * @return array + * @inheritDoc */ public function toArray(bool $bytecode=false): array { @@ -394,10 +392,9 @@ } /** - * @param array $data - * @return Package + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(array $data): Package { $object = new self(); diff --git a/src/ncc/Objects/ProjectConfiguration.php b/src/ncc/Objects/ProjectConfiguration.php index 8ebb07b..454e300 100644 --- a/src/ncc/Objects/ProjectConfiguration.php +++ b/src/ncc/Objects/ProjectConfiguration.php @@ -1,24 +1,24 @@ execution_policies !== null) - { - $execution_policies = []; - foreach($this->execution_policies as $executionPolicy) - { - $execution_policies[$executionPolicy->Name] = $executionPolicy->toArray($bytecode); - } - } - - $results = []; - if($this->project !== null) - { - $results[($bytecode ? Functions::cbc('project') : 'project')] = $this->project->toArray($bytecode); - } - - if($this->assembly !== null) - { - $results['assembly'] = $this->assembly->toArray($bytecode); - } - - if($this->build !== null) - { - $results[($bytecode ? Functions::cbc('build') : 'build')] = $this->build->toArray($bytecode); - } - - if($this->installer !== null) - { - $results[($bytecode ? Functions::cbc('installer') : 'installer')] = $this->installer->toArray($bytecode); - } - - if($execution_policies !== null && count($execution_policies) > 0) - { - $results[($bytecode ? Functions::cbc('execution_policies') : 'execution_policies')] = $execution_policies; - } - - return $results; - } - /** * Writes a json representation of the object to a file * @@ -444,48 +398,6 @@ Functions::encodeJsonFile($this->toArray($bytecode), $path, Functions::FORCE_ARRAY); } - /** - * Constructs the object from an array representation - * - * @param array $data - * @return ProjectConfiguration - */ - public static function fromArray(array $data): ProjectConfiguration - { - $object = new self(); - - if(isset($data['project'])) - { - $object->project = Project::fromArray($data['project']); - } - - if(isset($data['assembly'])) - { - $object->assembly = Assembly::fromArray($data['assembly']); - } - - if(isset($data['build'])) - { - $object->build = Build::fromArray($data['build']); - } - - if(isset($data['installer'])) - { - $object->installer = Installer::fromArray($data['installer']); - } - - if(isset($data['execution_policies'])) - { - $object->execution_policies = []; - foreach($data['execution_policies'] as $execution_policy) - { - $object->execution_policies[] = ExecutionPolicy::fromArray($execution_policy); - } - } - - return $object; - } - /** * Loads the object from a file representation * @@ -539,4 +451,87 @@ return $required_policies; } + + /** + * @inheritDoc + */ + public function toArray(bool $bytecode=false): array + { + $execution_policies = null; + if($this->execution_policies !== null) + { + $execution_policies = []; + foreach($this->execution_policies as $executionPolicy) + { + $execution_policies[$executionPolicy->Name] = $executionPolicy->toArray($bytecode); + } + } + + $results = []; + if($this->project !== null) + { + $results[($bytecode ? Functions::cbc('project') : 'project')] = $this->project->toArray($bytecode); + } + + if($this->assembly !== null) + { + $results['assembly'] = $this->assembly->toArray($bytecode); + } + + if($this->build !== null) + { + $results[($bytecode ? Functions::cbc('build') : 'build')] = $this->build->toArray($bytecode); + } + + if($this->installer !== null) + { + $results[($bytecode ? Functions::cbc('installer') : 'installer')] = $this->installer->toArray($bytecode); + } + + if($execution_policies !== null && count($execution_policies) > 0) + { + $results[($bytecode ? Functions::cbc('execution_policies') : 'execution_policies')] = $execution_policies; + } + + return $results; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): ProjectConfiguration + { + $object = new self(); + + if(isset($data['project'])) + { + $object->project = Project::fromArray($data['project']); + } + + if(isset($data['assembly'])) + { + $object->assembly = Assembly::fromArray($data['assembly']); + } + + if(isset($data['build'])) + { + $object->build = Build::fromArray($data['build']); + } + + if(isset($data['installer'])) + { + $object->installer = Installer::fromArray($data['installer']); + } + + if(isset($data['execution_policies'])) + { + $object->execution_policies = []; + foreach($data['execution_policies'] as $execution_policy) + { + $object->execution_policies[] = ExecutionPolicy::fromArray($execution_policy); + } + } + + return $object; + } } \ No newline at end of file diff --git a/src/ncc/Objects/SymlinkDictionary.php b/src/ncc/Objects/SymlinkDictionary.php deleted file mode 100644 index c1ea877..0000000 --- a/src/ncc/Objects/SymlinkDictionary.php +++ /dev/null @@ -1,28 +0,0 @@ -Version = Versions::CREDENTIALS_STORE_VERSION; - $this->Entries = []; + $this->version = Versions::CREDENTIALS_STORE_VERSION; + $this->entries = []; } /** @@ -68,10 +69,12 @@ public function addEntry(string $name, PasswordInterface $password, bool $encrypt=true): bool { // Check if the entry already exists - foreach($this->Entries as $entry) + foreach($this->entries as $entry) { if($entry->getName() === $name) + { return false; + } } // Create the new entry @@ -81,7 +84,7 @@ $entry->setAuthentication($password); // Add the entry to the vault - $this->Entries[] = $entry; + $this->entries[] = $entry; return true; } @@ -95,17 +98,17 @@ public function deleteEntry(string $name): bool { // Find the entry - foreach($this->Entries as $index => $entry) + foreach($this->entries as $index => $entry) { if($entry->getName() === $name) { // Remove the entry - unset($this->Entries[$index]); + unset($this->entries[$index]); return true; } } - // Entry not found + // Entry isn't found return false; } @@ -117,7 +120,7 @@ */ public function getEntries(): array { - return $this->Entries; + return $this->entries; } /** @@ -128,10 +131,12 @@ */ public function getEntry(string $name): ?Entry { - foreach($this->Entries as $entry) + foreach($this->entries as $entry) { if($entry->getName() === $name) + { return $entry; + } } return null; @@ -150,14 +155,13 @@ { $entry = $this->getEntry($name); if($entry === null) - return false; - - if($entry->getPassword() === null) { - if($entry->isEncrypted() && !$entry->isCurrentlyDecrypted()) - { - return $entry->unlock($password); - } + return false; + } + + if(($entry->getPassword() === null) && $entry->isEncrypted() && !$entry->isCurrentlyDecrypted()) + { + return $entry->unlock($password); } $input = []; @@ -175,40 +179,37 @@ } /** - * Returns an array representation of the object - * - * @param bool $bytecode - * @return array + * @inheritDoc */ public function toArray(bool $bytecode=false): array { $entries = []; - foreach($this->Entries as $entry) + + foreach($this->entries as $entry) { $entries[] = $entry->toArray($bytecode); } return [ - ($bytecode ? Functions::cbc('version') : 'version') => $this->Version, + ($bytecode ? Functions::cbc('version') : 'version') => $this->version, ($bytecode ? Functions::cbc('entries') : 'entries') => $entries, ]; } /** - * Constructs a new object from an array - * - * @param array $array - * @return Vault + * @inheritDoc */ - public static function fromArray(array $array): Vault + public static function fromArray(array $data): Vault { $vault = new Vault(); - $vault->Version = Functions::array_bc($array, 'version'); - $entries = Functions::array_bc($array, 'entries'); - $vault->Entries = []; + $vault->version = Functions::array_bc($data, 'version'); + $entries = Functions::array_bc($data, 'entries'); + $vault->entries = []; foreach($entries as $entry) - $vault->Entries[] = Entry::fromArray($entry); + { + $vault->entries[] = Entry::fromArray($entry); + } return $vault; }