mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-11-30 04:04:03 -05:00
web/vendor still shipped firebase/php-jwt 5.0.0 although composer.json requires ^7.0 and composer.lock pinned 7.0.0: the lock had been bumped without regenerating vendor. Run composer update firebase/php-jwt (7.0.0 -> 7.2.1) and commit the regenerated vendor tree. php-jwt 6 changed JWT::decode() to take a Key instead of a key and an array of allowed algorithms, so validateToken() moves to JWT::decode($token, new Key($key, 'HS256')). Without that every token would fail to decode. JWT::encode() keeps its signature. php-jwt 7 also refuses HMAC keys shorter than the hash, which the preceding ZM_AUTH_HASH_SECRET commits handle, and requires PHP 8.0; composer's platform_check.php now reports an older PHP at autoload. Tests: tests/php/test_jwt_vendor.php round trips a token the way HostController and validateToken() do, refuses a wrong secret, an expired token and alg none, and fails if any JWT::decode() call uses the v5 signature, in the web code or these tests. test_zmu_credentials.php moves to the Key form and a 32 byte secret. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
76 lines
3.0 KiB
PHP
76 lines
3.0 KiB
PHP
<?php
|
|
// Tests zmuCredentials() in web/includes/auth.php: what the web UI and the API
|
|
// put on zmu's command line to authenticate it.
|
|
//
|
|
// A command line is visible to every local user (ps, /proc/<pid>/cmdline) and
|
|
// lands in debug logs. zmu used to get the user's plaintext password there with
|
|
// ZM_AUTH_RELAY=plain, and with the default hashed relay an auth hash, which
|
|
// logs in to the web UI as auth= for ZM_AUTH_HASH_TTL hours. With a usable
|
|
// ZM_AUTH_HASH_SECRET it now gets an access token that lives a minute.
|
|
//
|
|
// Run: php tests/php/test_zmu_credentials.php
|
|
//
|
|
// auth.php can't be included without a database (see
|
|
// test_auth_no_include_side_effects.php), so the function is lifted out of the
|
|
// file and evaluated with stand-ins for what it calls.
|
|
|
|
require_once __DIR__.'/../../web/vendor/autoload.php';
|
|
|
|
$failures = 0;
|
|
$passes = 0;
|
|
|
|
function check($name, $got, $expected) {
|
|
global $failures, $passes;
|
|
if ($got === $expected) {
|
|
$passes++;
|
|
echo "ok - $name\n";
|
|
} else {
|
|
$failures++;
|
|
echo "not ok - $name\n got: ".var_export($got, true)."\n expected: ".var_export($expected, true)."\n";
|
|
}
|
|
}
|
|
|
|
$auth_src = file_get_contents(__DIR__.'/../../web/includes/auth.php');
|
|
if (!preg_match('/^function zmuCredentials\(.*?^}$/ms', $auth_src, $matches)) {
|
|
echo "not ok - zmuCredentials() not found in auth.php\n";
|
|
exit(1);
|
|
}
|
|
|
|
define('ZM_OPT_USE_AUTH', true);
|
|
define('ZM_AUTH_RELAY', 'plain');
|
|
// php-jwt 7 refuses an HS256 key shorter than 32 bytes.
|
|
define('ZM_AUTH_HASH_SECRET', 'zmu-credentials-test-secret-32-bytes-long');
|
|
|
|
$secretIsSet = true;
|
|
function authHashSecretIsSet() { global $secretIsSet; return $secretIsSet; }
|
|
function generateAuthHash($useRemoteAddr, $force = false) { return 'authhash'; }
|
|
|
|
class FakeUser {
|
|
public function Username() { return 'admin'; }
|
|
}
|
|
$user = new FakeUser();
|
|
$password = 'correct horse';
|
|
$_SESSION = array('username' => 'admin', 'password' => $password);
|
|
|
|
eval($matches[0]);
|
|
|
|
$args = zmuCredentials();
|
|
check('with a secret: a token and nothing else', preg_match("/^ -T '[^']+'$/", $args), 1);
|
|
check('with a secret: not the password, though the session has it', strpos($args, $password), false);
|
|
preg_match("/^ -T '([^']+)'$/", $args, $m);
|
|
$claims = (array)\Firebase\JWT\JWT::decode($m[1], new \Firebase\JWT\Key(ZM_AUTH_HASH_SECRET, 'HS256'));
|
|
check('an access token for the user', array($claims['user'], $claims['type']), array('admin', 'access'));
|
|
check('that lives a minute', $claims['exp'] - $claims['iat'], 60);
|
|
|
|
// zmu refuses tokens without a usable secret, so then it gets what it did before.
|
|
$secretIsSet = false;
|
|
check('without a secret, plain relay: user and password from the session', zmuCredentials(),
|
|
" -U 'admin' -P 'correct horse'");
|
|
check('without a secret, plain relay: the password the caller passes comes first', zmuCredentials('from request'),
|
|
" -U 'admin' -P 'from request'");
|
|
$secretIsSet = true;
|
|
check('with a secret, a password the caller passes is still not used', strpos(zmuCredentials('from request'), 'from request'), false);
|
|
|
|
echo "\n$passes passed, $failures failed\n";
|
|
exit($failures ? 1 : 0);
|