diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 5481a007..e2807431 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -7,5 +7,14 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - uses: php-actions/composer@v6 - - uses: php-actions/phpunit@v3 + - name: setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "8.3" + # php extensions also listed in tools/docker-dev/web/Dockerfile + extensions: curl,mysql,ldap,pdo,redis + tools: composer:v2 + - name: Install dependencies + run: composer install --prefer-dist --no-progress + - name: Run PHPUnit tests + run: vendor/bin/phpunit diff --git a/composer.json b/composer.json index 606629ac..006bcf7f 100644 --- a/composer.json +++ b/composer.json @@ -4,5 +4,8 @@ "phpseclib/phpseclib": "3.0.43", "phpmailer/phpmailer": "6.6.4", "hakasapl/phpopenldaper": "1.0.5" + }, + "require-dev": { + "phpunit/phpunit": "<12.1" } } diff --git a/phpunit.xml b/phpunit.xml index b3ac75ad..e16564c3 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,9 +1,9 @@ + diff --git a/resources/lib/UnitySite.php b/resources/lib/UnitySite.php index 7ad484ff..50151e57 100644 --- a/resources/lib/UnitySite.php +++ b/resources/lib/UnitySite.php @@ -52,6 +52,18 @@ public static function getGithubKeys($username) public static function testValidSSHKey($key_str) { + $key_str = trim($key_str); + if ($key_str == "") { + return false; + } + // PHP warning when key_str is digits: Attempt to read property "keys" on int + if (preg_match("/^[0-9]+$/", $key_str)) { + return false; + } + // PHP warning when key_str is JSON: Undefined property: stdClass::$keys + if (!is_null(@json_decode($key_str))) { + return false; + } try { PublicKeyLoader::load($key_str); return true; diff --git a/test/unit/AjaxSshValidateTest.php b/test/unit/AjaxSshValidateTest.php new file mode 100644 index 00000000..6f670a43 --- /dev/null +++ b/test/unit/AjaxSshValidateTest.php @@ -0,0 +1,35 @@ +assertEquals("true", $output); + } else { + $this->assertEquals("false", $output); + } + } +} diff --git a/tools/docker-dev/web/Dockerfile b/tools/docker-dev/web/Dockerfile index db322179..5f171085 100644 --- a/tools/docker-dev/web/Dockerfile +++ b/tools/docker-dev/web/Dockerfile @@ -2,6 +2,7 @@ FROM ubuntu:20.04 # Web Server Setup ARG DEBIAN_FRONTEND=noninteractive +# php extensions also listed in .github/workflows/phpunit.yml RUN apt-get update && apt-get install -y \ apache2 \ apache2-utils \ @@ -23,4 +24,4 @@ RUN sed -i '/display_errors/c\display_errors = on' /etc/php/7.4/apache2/php.ini # Start apache2 server EXPOSE 80 -CMD ["apache2ctl", "-D", "FOREGROUND"] \ No newline at end of file +CMD ["apache2ctl", "-D", "FOREGROUND"] diff --git a/webroot/js/ajax/ssh_validate.php b/webroot/js/ajax/ssh_validate.php index 7180defb..a62809cb 100644 --- a/webroot/js/ajax/ssh_validate.php +++ b/webroot/js/ajax/ssh_validate.php @@ -1,12 +1,6 @@