Files
deepDiverPaul 25030638a4 Implemented: editor uploads are constrained to safe image files inside
the upload tree, and public form submissions are
size-bounded and rate-limited
2026-11-30 16:51:15 +01:00

67 lines
2.8 KiB
PHP

<?php
use Plugi\Modules\GrapesJS\Upload\Uploader;
require_once __DIR__ . '/../src/Modules/GrapesJS/Upload/Uploader.php';
function assertUploadSecurity(bool $condition, string $message): void
{
if (!$condition) {
throw new RuntimeException($message);
}
}
$uploadRoot = sys_get_temp_dir() . '/plugi-upload-security-' . bin2hex(random_bytes(8));
assertUploadSecurity(mkdir($uploadRoot), 'The upload security test folder could not be created.');
$outsideFile = dirname($uploadRoot) . '/plugi-upload-escape-' . bin2hex(random_bytes(8)) . '.php';
$imagePath = __DIR__ . '/../dist/pagebuilder/images/image-placeholder.png';
$traversalUpload = new Uploader([
'name' => 'image.png',
'tmp_name' => $imagePath,
'size' => filesize($imagePath),
'error' => UPLOAD_ERR_OK,
], false);
$traversalUpload
->file_name('../' . basename($outsideFile))
->upload_to($uploadRoot . '/')
->run();
assertUploadSecurity(!$traversalUpload->was_uploaded, 'Path traversal destinations must be rejected.');
assertUploadSecurity(!file_exists($outsideFile), 'Path traversal must not create files outside the upload folder.');
$linkedDirectory = $uploadRoot . '/linked';
assertUploadSecurity(symlink(dirname($uploadRoot), $linkedDirectory), 'The upload symlink test could not be created.');
$symlinkFile = dirname($uploadRoot) . '/plugi-upload-symlink-' . bin2hex(random_bytes(8)) . '.png';
$symlinkUpload = new Uploader([
'name' => 'image.png',
'tmp_name' => $imagePath,
'size' => filesize($imagePath),
'error' => UPLOAD_ERR_OK,
], false);
$symlinkUpload
->file_name('linked/' . basename($symlinkFile))
->upload_to($uploadRoot . '/')
->run();
assertUploadSecurity(!$symlinkUpload->was_uploaded, 'Symlinked destination folders must be rejected.');
assertUploadSecurity(!file_exists($symlinkFile), 'Symlinked destinations must not create files outside the upload folder.');
assertUploadSecurity(unlink($linkedDirectory), 'The upload symlink test could not be removed.');
$scriptUpload = new Uploader([
'name' => 'payload.php',
'tmp_name' => __FILE__,
'size' => filesize(__FILE__),
'error' => UPLOAD_ERR_OK,
], false);
$scriptUpload->MIME_allowed = ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/bmp'];
$scriptUpload
->file_name('payload.jpg')
->upload_to($uploadRoot . '/')
->mime_check()
->run();
assertUploadSecurity(!$scriptUpload->was_uploaded, 'Non-image uploads must be rejected when image MIME validation is enabled.');
$uploadRules = file_get_contents(dirname(__DIR__, 2) . '/uploads/.htaccess');
assertUploadSecurity(strpos($uploadRules, 'Require all denied') !== false, 'Uploaded PHP scripts must be denied by the Apache upload rules.');
assertUploadSecurity(rmdir($uploadRoot), 'The upload security test folder could not be removed.');
echo "Upload security checks passed.\n";