Skip to content

Commit cf60e7a

Browse files
authored
improve ssh key validation (#163)
1 parent 0984e0b commit cf60e7a

File tree

7 files changed

+67
-13
lines changed

7 files changed

+67
-13
lines changed

Diff for: .github/workflows/phpunit.yml

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,14 @@ jobs:
77
runs-on: ubuntu-latest
88
steps:
99
- uses: actions/checkout@v3
10-
- uses: php-actions/composer@v6
11-
- uses: php-actions/phpunit@v3
10+
- name: setup PHP
11+
uses: shivammathur/setup-php@v2
12+
with:
13+
php-version: "8.3"
14+
# php extensions also listed in tools/docker-dev/web/Dockerfile
15+
extensions: curl,mysql,ldap,pdo,redis
16+
tools: composer:v2
17+
- name: Install dependencies
18+
run: composer install --prefer-dist --no-progress
19+
- name: Run PHPUnit tests
20+
run: vendor/bin/phpunit

Diff for: composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
"phpseclib/phpseclib": "3.0.43",
55
"phpmailer/phpmailer": "6.6.4",
66
"hakasapl/phpopenldaper": "1.0.5"
7+
},
8+
"require-dev": {
9+
"phpunit/phpunit": "<12.1"
710
}
811
}

Diff for: phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
<!-- restrictWarnings="true" -->
12
<phpunit
23
bootstrap="test/unit/bootstrap.php"
34
failOnWarning="true"
45
failOnDeprecation="true"
56
failOnNotice="true"
6-
restrictWarnings="true"
77
>
88
<testsuites>
99
<testsuite name="unit">

Diff for: resources/lib/UnitySite.php

+12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ public static function getGithubKeys($username)
5252

5353
public static function testValidSSHKey($key_str)
5454
{
55+
$key_str = trim($key_str);
56+
if ($key_str == "") {
57+
return false;
58+
}
59+
// PHP warning when key_str is digits: Attempt to read property "keys" on int
60+
if (preg_match("/^[0-9]+$/", $key_str)) {
61+
return false;
62+
}
63+
// PHP warning when key_str is JSON: Undefined property: stdClass::$keys
64+
if (!is_null(@json_decode($key_str))) {
65+
return false;
66+
}
5567
try {
5668
PublicKeyLoader::load($key_str);
5769
return true;

Diff for: test/unit/AjaxSshValidateTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace UnityWebPortal\lib;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
8+
class AjaxSshValidateTest extends TestCase
9+
{
10+
public static function providerTestSshValidate()
11+
{
12+
// sanity check only, see UnitySiteTest for more comprehensive test cases
13+
return [
14+
[false, "foobar"],
15+
// phpcs:disable
16+
[true, "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a"],
17+
// phpcs:enable
18+
];
19+
}
20+
21+
#[DataProvider("providerTestSshValidate")]
22+
public function testSshValidate(bool $is_valid, string $pubkey)
23+
{
24+
$_SERVER["REQUEST_METHOD"] = "POST";
25+
$_POST["key"] = $pubkey;
26+
ob_start();
27+
include __DIR__ . "/../../webroot/js/ajax/ssh_validate.php";
28+
$output = ob_get_clean();
29+
if ($is_valid) {
30+
$this->assertEquals("true", $output);
31+
} else {
32+
$this->assertEquals("false", $output);
33+
}
34+
}
35+
}

Diff for: tools/docker-dev/web/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ FROM ubuntu:20.04
22

33
# Web Server Setup
44
ARG DEBIAN_FRONTEND=noninteractive
5+
# php extensions also listed in .github/workflows/phpunit.yml
56
RUN apt-get update && apt-get install -y \
67
apache2 \
78
apache2-utils \
@@ -23,4 +24,4 @@ RUN sed -i '/display_errors/c\display_errors = on' /etc/php/7.4/apache2/php.ini
2324
# Start apache2 server
2425
EXPOSE 80
2526

26-
CMD ["apache2ctl", "-D", "FOREGROUND"]
27+
CMD ["apache2ctl", "-D", "FOREGROUND"]

Diff for: webroot/js/ajax/ssh_validate.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
<?php
22

3-
require "../../../resources/autoload.php";
3+
require_once __DIR__ . "/../../../resources/lib/UnitySite.php";
4+
use UnityWebPortal\lib\UnitySite;
45

5-
use phpseclib3\Crypt\PublicKeyLoader;
6-
7-
try {
8-
PublicKeyLoader::load($_POST['key'], $password = false);
9-
echo "true";
10-
} catch (Exception $e) {
11-
echo "false";
12-
}
6+
echo UnitySite::testValidSSHKey($_POST["key"]) ? "true" : "false";

0 commit comments

Comments
 (0)