diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fee724eb..bb4f1b00 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -46,3 +46,9 @@ repos: language: system files: \.php$ args: [--standard=PSR2, --colors] + - id: php-l + name: php -l + entry: php + language: system + files: \.php$ + args: [-l] diff --git a/composer.json b/composer.json index 76750d1c..606629ac 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "require": { "psr/log": "1.1.4", - "phpseclib/phpseclib": "3.0.16", + "phpseclib/phpseclib": "3.0.43", "phpmailer/phpmailer": "6.6.4", "hakasapl/phpopenldaper": "1.0.5" } diff --git a/resources/lib/UnitySite.php b/resources/lib/UnitySite.php index 2bedf8a6..7ad484ff 100644 --- a/resources/lib/UnitySite.php +++ b/resources/lib/UnitySite.php @@ -36,15 +36,18 @@ public static function getGithubKeys($username) curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); - $output = json_decode(curl_exec($curl), true); + $keys = json_decode(curl_exec($curl), false); curl_close($curl); - $out = array(); - foreach ($output as $value) { - array_push($out, $value["key"]); + // normally returns array of objects each with a ->key attribute + // if bad URL or no such user, returns status=404 object + // if no keys, returns [] + if ((!is_array($keys)) || (count($keys) == 0)) { + return []; } - - return $out; + // phpcs:disable + return array_map(function($x){return $x->key;}, $keys); + // phpcs:enable } public static function testValidSSHKey($key_str) diff --git a/test/unit/UnitySiteTest.php b/test/unit/UnitySiteTest.php index 8eb06480..9c508e45 100644 --- a/test/unit/UnitySiteTest.php +++ b/test/unit/UnitySiteTest.php @@ -29,4 +29,27 @@ public function testTestValidSSHKey(bool $expected, string $key) $SITE = new UnitySite(); $this->assertEquals($expected, $SITE->testValidSSHKey($key)); } + + public static function providerTestGetGithubKeys() + { + return [ + # empty + ["", []], + # nonexistent user + ["asdfkljhasdflkjashdflkjashdflkasjd", []], + # user with no keys + ["sheldor1510", []], + # user with 1 key + //phpcs:disable + ["simonLeary42", ["ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDGRl6JWPj+Gq2Lz9GjYdUl4/unLoFOyfgeiII1CxutpabPByRJbeuonR0zTpn51tZeYuAUOJBOeKt+Lj4i4UDGl6igpdXXSwkBXl7jxfRPwJ6WuTkDx7Z8ynwnqlDV2q089q4OX/b/uuHgsIhIBwrouKsRQaZIqTbwNMfiqQ2zl14V0KMrTPzOiwR6Q+hqSaR5Z29WKE7ff/OWzSC3/0T6avCmcCbQaRPJdVM+QC17B0vl8FzPwRjorMngwZ0cImdQ/0Ww1d12YAL7UWp1c2egfnthKP3MuQZnNF8ixsAk1eIIwTRdiI87BOoorW8NXhxXmhyheRCsFwyP4LJBqyUVoZJ0UYyk0AO4G9EStnfpiz8YXGK+M1G4tUrWgzs1cdjlHtgCWUmITtgabnYCC4141m7n4GZTk2H/lSrJcvAs3JEiwLTj1lzeGgzeSsz/XKsnOJyzjEVr2Jp3iT+J9PbQpfS0SxTCIGgxMqllovv79pfsF/zc+vaxqSShyHW7oyn7hLMHM60LO/IIX1RWGL3rD9ecXx2pXXQ1RhIkVteIi13XkFt+KW00cstFlAd3EHCoY/XorShd2jeID7tpnYlmNfotYUs6IKefvpNC0PWkh5UXFEv3SUfw4Wd8O0DiHfhkrhxn1W/GajqSIlZ5DKgPzFg8EHexv8lSa7WJg0H3YQ=="]] + //phpcs:enable + ]; + } + + #[DataProvider("providerTestGetGithubKeys")] + public function testGetGithubKeys(string $username, array $expected) + { + $SITE = new UnitySite(); + $this->assertEquals($expected, $SITE->getGithubKeys($username)); + } } diff --git a/webroot/panel/account.php b/webroot/panel/account.php index c45602ca..867387ef 100644 --- a/webroot/panel/account.php +++ b/webroot/panel/account.php @@ -38,9 +38,6 @@ break; case "github": $gh_user = $_POST["gh_user"]; - if (empty($gh_user)) { - break; - } $keys = UnitySite::getGithubKeys($gh_user); foreach ($keys as $key) { if (UnitySite::testValidSSHKey($key)) {