Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ repos:
entry: bash ./test/assert-utils-used.bash
language: system
files: \.php$
exclude: ^resources/lib/utils\.php$
exclude: |
(?x)^(
^resources/lib/utils\.php$|
^test/.*\.php$|
)$
- id: assert-exceptions-used
name: Assert exceptions are used
entry: bash ./test/assert-exceptions-used.bash
Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
This will throw an exception rather than returning `false`.
- No code should call `mb_detect_encoding()`, instead `\mbDetectEncoding()`.
This will enable strict mode and throw an exception rather than returning `false`.
- No code should call `intval()`, instead `\str2int()`.
This will enable strict mode and throw an exception rather than issuing a warning.
- `UnityHTTPD`'s user-facing error functionality (ex: `badRequest`) should only be called from `webroot/**/*.php`.
`resources/**/*.php` should throw exceptions instead.
- all pages under `webroot/admin/` must check for `$USER->isAdmin()` and call `UnityHTTPD::forbidden()` if not admin.
Expand Down
2 changes: 1 addition & 1 deletion resources/lib/UnityLDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private function getCustomIDMappings(): array
}
$output_map = [];
foreach ($output as [$uid, $uidNumber_str]) {
$output_map[$uid] = intval($uidNumber_str);
$output_map[$uid] = str2int($uidNumber_str);
}
return $output_map;
}
Expand Down
14 changes: 13 additions & 1 deletion resources/lib/utils.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use UnityWebPortal\lib\exceptions\ArrayKeyException;
use UnityWebPortal\lib\exceptions\EnsureException;
use UnityWebPortal\lib\exceptions\EncodingUnknownException;
use UnityWebPortal\lib\exceptions\EncodingConversionException;
Expand Down Expand Up @@ -83,3 +82,16 @@ function getHyperlink($text, ...$url_components)
$url = getURL(...$url_components);
return "<a href='$url'>$text</a>";
}

/**
* extra args (ex: base) are passed along to intval()
* @throws ValueError
*/
function str2int(string $x, ...$args): int
{
if (ctype_digit($x)) {
return intval($x, ...$args);
} else {
throw new ValueError("not digits: $x");
}
}
1 change: 1 addition & 0 deletions test/assert-utils-used.bash
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare -A utils=(
["json_encode"]="jsonEncode"
["mb_detect_encoding"]="mbDetectEncoding"
["mb_convert_encoding"]="mbConvertEncoding"
["intval"]="str2int"
)

rc=0
Expand Down
35 changes: 10 additions & 25 deletions test/functional/SSHKeyDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,30 @@ private function deleteKey(string $index): void
public static function getGarbageIndexArgs()
{
global $HTTP_HEADER_TEST_INPUTS;
return array_map(function ($x) {
return [$x];
}, $HTTP_HEADER_TEST_INPUTS);
$http_header_test_inputs_no_ints = array_filter(
$HTTP_HEADER_TEST_INPUTS,
fn($x) => !ctype_digit($x),
);
$http_header_test_inputs_no_ints_2d = array_map(
fn($x) => [$x],
$http_header_test_inputs_no_ints,
);
return array_merge([["-1"], ["0.5"]], $http_header_test_inputs_no_ints_2d);
}

#[DataProvider("getGarbageIndexArgs")]
public function testDeleteKeyGarbageInput(string $index)
{
global $USER;
try {
$this->expectException(ValueError::class);
$this->deleteKey($index);
$this->assertEquals(self::$initialKeys, $USER->getSSHKeys());
} finally {
$USER->setSSHKeys(self::$initialKeys);
}
}

public function testDeleteKeyNegativeIndex()
{
global $USER;
try {
$this->deleteKey("-1");
$this->assertEquals(self::$initialKeys, $USER->getSSHKeys());
} finally {
$USER->setSSHKeys(self::$initialKeys);
}
}

public function testDeleteKeyIndexTooLarge()
{
global $USER;
Expand All @@ -63,17 +59,6 @@ public function testDeleteKeyIndexTooLarge()
}
}

public function testDeleteKeyDecimal()
{
global $USER;
try {
$this->deleteKey("0.5");
$this->assertEquals(self::$initialKeys, $USER->getSSHKeys());
} finally {
$USER->setSSHKeys(self::$initialKeys);
}
}

public function testDeleteKey()
{
global $USER;
Expand Down
2 changes: 1 addition & 1 deletion webroot/api/content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

require_once __DIR__ . "/../../../resources/autoload.php";

$CHAR_WRAP = UnityHTTPD::getQueryParameter("line_wrap", false) ?? 80;
$CHAR_WRAP = str2int(UnityHTTPD::getQueryParameter("line_wrap", false) ?? "80");
$content_name = UnityHTTPD::getQueryParameter("content_name");
echo $SQL->getPage($content_name)["content"];
6 changes: 1 addition & 5 deletions webroot/panel/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@
break;
case "delKey":
$keys = $USER->getSSHKeys();
$indexStr = $_POST["delIndex"];
if (!preg_match("/^[0-9]+$/", $indexStr)) {
break;
}
$index = intval($indexStr);
$index = str2int(UnityHTTPD::getPostData("delIndex"));
if ($index >= count($keys)) {
break;
}
Expand Down
Loading