fix: handle invalid package uploads

This commit is contained in:
Keith Solomon
2026-04-26 20:45:18 -05:00
parent 90b56e13bb
commit cce40907d5
4 changed files with 98 additions and 2 deletions
+29 -2
View File
@@ -49,7 +49,23 @@ final class FileImportController {
throw new \RuntimeException( 'The package file could not be read.' );
}
$package = $this->transport->import( $contents );
try {
$package = $this->transport->import( $contents );
} catch ( \InvalidArgumentException $exception ) {
$this->logger->warning(
'Rejected imported content package.',
array(
'error' => $exception->getMessage(),
)
);
$this->redirectToDashboard(
array(
'wpcs_import_error' => $exception->getMessage(),
)
);
return;
}
$this->logger->info(
'Validated imported content package.',
@@ -59,9 +75,20 @@ final class FileImportController {
)
);
$this->redirectToDashboard(
array(
'wpcs_imported' => '1',
)
);
}
/**
* @param array<string, string> $args Redirect query args.
*/
private function redirectToDashboard( array $args ): void {
wp_safe_redirect(
add_query_arg(
array( 'wpcs_imported' => '1' ),
$args,
admin_url( 'admin.php?page=wp-content-sync' )
)
);
+14
View File
@@ -26,6 +26,20 @@ if ( ! defined( 'ABSPATH' ) ) {
</p>
</div>
<?php // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Displays redirect status only. ?>
<?php if ( isset( $_GET['wpcs_import_error'] ) ) : ?>
<div class="notice notice-error">
<p><?php echo esc_html( sanitize_text_field( wp_unslash( $_GET['wpcs_import_error'] ) ) ); ?></p>
</div>
<?php endif; ?>
<?php // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Displays redirect status only. ?>
<?php if ( isset( $_GET['wpcs_imported'] ) ) : ?>
<div class="notice notice-success">
<p><?php echo esc_html__( 'The package JSON file was validated successfully.', 'wp-content-sync' ); ?></p>
</div>
<?php endif; ?>
<h2><?php echo esc_html__( 'Current Defaults', 'wp-content-sync' ); ?></h2>
<table class="widefat striped">
<tbody>
@@ -0,0 +1,41 @@
<?php
namespace WPContentSync\Tests\Unit\Admin;
use PHPUnit\Framework\TestCase;
use WPContentSync\Settings\Settings;
class DashboardTemplateTest extends TestCase {
protected function tearDown(): void {
$_GET = array();
parent::tearDown();
}
public function test_it_renders_import_error_notices(): void {
$_GET['wpcs_import_error'] = 'The selected file is not valid JSON.';
$output = $this->renderDashboard();
self::assertStringContainsString( 'notice-error', $output );
self::assertStringContainsString( 'The selected file is not valid JSON.', $output );
}
public function test_it_renders_import_success_notices(): void {
$_GET['wpcs_imported'] = '1';
$output = $this->renderDashboard();
self::assertStringContainsString( 'notice-success', $output );
self::assertStringContainsString( 'The package JSON file was validated successfully.', $output );
}
private function renderDashboard(): string {
$settings = Settings::fromArray( array() );
ob_start();
include WPCS_PLUGIN_DIR . 'templates/admin/dashboard.php';
return (string) ob_get_clean();
}
}
@@ -82,6 +82,20 @@ class FileImportControllerTest extends TestCase {
self::assertStringContainsString( 'wpcs_imported=1', $GLOBALS['wpcs_redirect_location'] );
}
public function test_it_redirects_with_error_for_invalid_uploaded_packages(): void {
$file = $this->createTemporaryPackageFile( '{"schema_version":' );
$_FILES['wpcs_package_file'] = array(
'tmp_name' => $file,
'error' => UPLOAD_ERR_OK,
);
$this->controller()->handleImport();
self::assertStringContainsString( 'wpcs_import_error=', $GLOBALS['wpcs_redirect_location'] );
self::assertStringContainsString( 'not+valid+JSON', $GLOBALS['wpcs_redirect_location'] );
}
private function controller(): FileImportController {
return new FileImportController(
new JsonFileTransport( new PackageValidator() ),