Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions includes/class-opentrust-admin-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,21 +318,29 @@ public function handle_import_preview(): void {
return;
}

// Park the upload between preview and apply. Path goes in a per-user transient.
$upload_dir = wp_upload_dir();
$stash_dir = rtrim((string) $upload_dir['basedir'], '/') . '/opentrust-tmp';
wp_mkdir_p($stash_dir);
if (!file_exists($stash_dir . '/.htaccess')) {
@file_put_contents($stash_dir . '/.htaccess', "Require all denied\n");
}
$stash_path = $stash_dir . '/import-' . wp_generate_password(12, false) . '.zip';
// wp_handle_upload() lives here; idempotent require for WP-CLI / alt call paths.
require_once ABSPATH . 'wp-admin/includes/file.php';

// test_form=false because our admin-post action is opentrust_import_preview,
// not the 'wp_handle_upload' token wp_handle_upload() checks for by default.
// mimes is narrowed to ZIP only — finfo magic-byte detection promotes Safari's
// application/x-zip-compressed to application/zip, so one entry covers all
// browsers without tripping the get_allowed_mime_types() global allowlist.
$upload = wp_handle_upload(
$_FILES['ot_import_file'],
[
'test_form' => false,
'mimes' => ['zip' => 'application/zip'],
]
);

// phpcs:ignore Generic.PHP.ForbiddenFunctions.Found -- Required for $_FILES handling. Higher-level WP wrappers impose a MIME allowlist that rejects file types we deliberately accept (admin-only, nonce + size cap + is_uploaded_file already gate this path).
if (!move_uploaded_file((string) $_FILES['ot_import_file']['tmp_name'], $stash_path)) {
$this->bounce_error(__('Could not store uploaded file.', 'opentrust'));
if (isset($upload['error'])) {
$this->bounce_error((string) $upload['error']);
return;
}

$stash_path = (string) $upload['file'];

try {
$read = OpenTrust_IO::read_zip($stash_path);
$manifest = $read['manifest'];
Expand Down
16 changes: 16 additions & 0 deletions uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,21 @@
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Bulk cleanup of plugin transients on uninstall, no user input
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_opentrust_%' OR option_name LIKE '_transient_timeout_opentrust_%'");

// Sweep legacy import-stash directory left behind by versions <1.1.0.
$ot_uploads = wp_upload_dir();
if (!empty($ot_uploads['basedir'])) {
$ot_stash = rtrim((string) $ot_uploads['basedir'], '/') . '/opentrust-tmp';
if (is_dir($ot_stash)) {
$ot_legacy = glob($ot_stash . '/{.,}*', GLOB_BRACE) ?: [];
foreach ($ot_legacy as $ot_file) {
if (is_file($ot_file)) {
wp_delete_file($ot_file);
}
}
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- one-shot legacy cleanup on uninstall; WP_Filesystem is disproportionate here.
@rmdir($ot_stash);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}

// Flush rewrite rules.
flush_rewrite_rules();
Loading