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.
108 lines
4.2 KiB
PHP
108 lines
4.2 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
$directory = sys_get_temp_dir() . '/virt-cloud-mail-render-' . bin2hex(random_bytes(8));
|
|
if (!mkdir($directory, 0700)) {
|
|
throw new RuntimeException('Cannot create private mail-render directory');
|
|
}
|
|
$assert = static function ($condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$render = static function (string $template, array $variables) use ($directory): string {
|
|
$smarty = new Smarty();
|
|
$smarty->setTemplateDir(dirname(__DIR__) . '/resources/email');
|
|
$smarty->setCompileDir($directory);
|
|
$smarty->setCacheDir($directory);
|
|
$smarty->assign('config', [
|
|
'appName' => 'Virt <Cloud>',
|
|
'baseUrl' => 'https://cloud.example.test',
|
|
]);
|
|
foreach ($variables as $name => $value) {
|
|
$smarty->assign($name, $value);
|
|
}
|
|
return $smarty->fetch($template);
|
|
};
|
|
|
|
try {
|
|
$registration = $render('auth/verify.tpl', [
|
|
'language' => 'false',
|
|
'scene' => 'registration',
|
|
'code' => '12<345',
|
|
'expire' => '2026-09-28 18:00:00',
|
|
]);
|
|
$login = $render('auth/verify.tpl', [
|
|
'language' => 'true',
|
|
'scene' => 'login',
|
|
'code' => '654321',
|
|
'expire' => '2026-09-28 18:00:00',
|
|
]);
|
|
$reset = $render('password/reset.tpl', [
|
|
'language' => 'false',
|
|
'resetUrl' => 'https://cloud.example.test/password/token/a?next=<unsafe>&x=1',
|
|
'expire' => '2026-09-29 18:00:00',
|
|
]);
|
|
$renewal = $render('cloud/invoice.tpl', [
|
|
'language' => 'false',
|
|
'invoice_id' => '42',
|
|
'instance_name' => '<script>alert("renewal")</script>',
|
|
'amount' => '12.00',
|
|
'due_at' => '2026-10-05 18:00:00',
|
|
'invoice_url' => 'https://cloud.example.test/user/cloud/invoices/42?x=1&y=2',
|
|
]);
|
|
$provisioned = $render('cloud/provision-success.tpl', [
|
|
'language' => 'false',
|
|
'instance_name' => '<script>alert("ready")</script>',
|
|
'instance_id' => '43',
|
|
'region' => 'HongKong',
|
|
'public_ip' => '203.0.113.43',
|
|
'os_name' => 'AlmaLinux 10',
|
|
'login_name' => 'root',
|
|
'created_at' => '2026-09-28 18:00:00',
|
|
'expires_at' => '2026-10-28 18:00:00',
|
|
'instance_url' => 'https://cloud.example.test/user/cloud/instances/43?x=1&y=2',
|
|
]);
|
|
|
|
foreach ([$registration, $login, $reset, $renewal, $provisioned] as $html) {
|
|
$assert(str_contains(strtolower($html), '<!doctype html>')
|
|
&& str_contains($html, 'CONTROL PANEL')
|
|
&& str_contains(strtolower($html), '#101d2c')
|
|
&& !str_contains($html, '<script>'),
|
|
'A rendered system email lost its shared theme or emitted raw HTML.');
|
|
}
|
|
$assert(str_contains($registration, '验证您的注册邮箱')
|
|
&& str_contains($registration, '12<345')
|
|
&& str_contains($login, 'Confirm your sign-in'),
|
|
'Registration or login verification email rendered the wrong scene.');
|
|
$assert(str_contains($reset, '重置账户密码')
|
|
&& str_contains($reset, '<unsafe>')
|
|
&& str_contains($reset, '&x=1'),
|
|
'Password-reset email did not render or escape its secure URL.');
|
|
$assert(str_contains($renewal, '续费账单已生成')
|
|
&& str_contains($renewal, '<script>')
|
|
&& str_contains($renewal, '#42'),
|
|
'Renewal invoice email did not render its safe public details.');
|
|
$assert(str_contains($provisioned, '实例已开通')
|
|
&& str_contains($provisioned, '<script>')
|
|
&& str_contains($provisioned, '203.0.113.43')
|
|
&& !str_contains($provisioned, 'not-for-email'),
|
|
'Provision-success email did not render safe instance details.');
|
|
} finally {
|
|
$generatedFiles = new RecursiveIteratorIterator(
|
|
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS),
|
|
RecursiveIteratorIterator::CHILD_FIRST
|
|
);
|
|
foreach ($generatedFiles as $generated) {
|
|
if ($generated->isFile()) {
|
|
unlink($generated->getPathname());
|
|
} elseif ($generated->isDir()) {
|
|
rmdir($generated->getPathname());
|
|
}
|
|
}
|
|
rmdir($directory);
|
|
}
|
|
|
|
echo "Registration, reset, renewal and provisioning emails rendered safely.\n";
|