You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudSecretsTest.php

29 lines
1.5 KiB
PHTML

<?php
namespace App\Services {
final class Config {
public static string $key = '';
public static function get($name, $default = null) { return $name === 'cloud_payload_key' ? self::$key : $default; }
}
}
namespace {
require dirname(__DIR__) . '/vendor/autoload.php';
if (!function_exists('sodium_crypto_secretbox')) {
echo "SKIPPED CloudSecretsTest: runtime lacks sodium; production requires sodium.\n";
exit(0);
}
use App\Services\Config;
use App\Services\Cloud\CloudSecrets;
use App\Services\Cloud\CloudError;
Config::$key = base64_encode(str_repeat('k', 32));
$value = ['password' => 'test-fixture-password'];
$first = CloudSecrets::encrypt($value); $second = CloudSecrets::encrypt($value);
if ($first === $second || str_contains(base64_decode($first), $value['password']) || CloudSecrets::decrypt($first) !== $value) {
throw new RuntimeException('Encryption, random nonce or roundtrip failed.');
}
$tampered = base64_decode($first); $tampered[strlen($tampered) - 1] = chr(ord($tampered[strlen($tampered) - 1]) ^ 1);
try { CloudSecrets::decrypt(base64_encode($tampered)); throw new RuntimeException('Tampered ciphertext accepted.'); } catch (CloudError $expected) {}
Config::$key = base64_encode(str_repeat('x', 32));
try { CloudSecrets::decrypt($first); throw new RuntimeException('Wrong key accepted.'); } catch (CloudError $expected) {}
echo "Cloud sodium nonce, ciphertext authentication and key isolation checks passed.\n";
}