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

42 lines
2.1 KiB
PHP

<?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));
if (CloudSecrets::key('payload_key') !== str_repeat('k', 32)) {
throw new RuntimeException('CloudSecrets must decode the configured base64 key to raw bytes.');
}
// Keep the final four data bits while setting non-zero base64 pad bits.
$nonCanonical = substr(Config::$key, 0, -2) . 't=';
Config::$key = $nonCanonical;
try {
CloudSecrets::key('payload_key');
throw new RuntimeException('Non-canonical base64 key was accepted.');
} catch (CloudError $expected) {
// The Go Agent uses the same canonical base64 file contract.
}
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";
}