Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions classes/Visualizer/Module/Abilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,10 @@ private function buildManualSource( $input ) {
return $errors;
}

// REST and MCP requests do not load the admin file helpers.
if ( ! function_exists( 'wp_tempnam' ) ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note The check tests only wp_tempnam(), but file.php declares about 30 global functions. A plugin that declares one of them, such as list_files(), without a guard and is loaded in the same request makes this include end the request with "Cannot redeclare". Is any such plugin loaded on REST ability requests? None means safe. One means the save fatals.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A plugin declaring list_files() or similar without a guard would already crash every wp-admin request (wp-admin/includes/admin.php:31 loads file.php unconditionally) and core's REST media upload. Visualizer's remote imports also already load it unconditionally (Remote/Fetch.php:105). This PR adds no new way for that to happen, and the bot's directory search found no copy that collides on REST requests.

require_once ABSPATH . 'wp-admin/includes/file.php';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note The require_once runs inside a method. The top-level $wp_file_descriptions array in file.php becomes a local variable, not a global. Later code in the same request that calls get_file_description() would read an empty global. Does anything after a manual-data save in a REST request read file descriptions? Nothing means safe. A reader means wrong file labels in that request.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reader is get_file_description(), called only from wp_print_theme_file_tree() (wp-admin/includes/misc.php:445). That only runs on wp-admin/theme-editor.php, a separate admin request where admin.php loads file.php at global scope. Nothing reads it after a REST save. Core itself does the same in-method include (class-wp-rest-attachments-controller.php:664), and so does this plugin already (Module.php:274, Remote/Fetch.php:105).

}
Comment on lines +1205 to +1208

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note The guard loads file.php only for wp_tempnam(). If code that the Pro add-on hooks into the upsert-chart save path calls another wp-admin/includes helper, a REST or MCP save still ends with an undefined-function error. The Pro add-on was not inspected. Does any Pro callback on this path call an admin-only helper without loading its file? None means safe. Any such call means the same fatal remains.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked against visualizer-pro origin/development. On the upsert path, Pro hooks only visualizer_pro_new_chart_defaults → createNewChart() (inc/addon.php:523-541, post meta writes only) and visualizer_pro_remove_schedule → deleteSchedule() (inc/addon.php:901-909, meta and option writes only). Neither calls a wp-admin/includes helper. Pro's only other admin-only call, get_editable_roles() (addon.php:799), runs from visualizer_pro_get_permissions_data, which only fires from Render/Layout.php:581,612 in the admin settings sidebar.

Comment on lines +1206 to +1208

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note Two directory plugins ship their own full copy of file.php: WP Time Capsule and Local Sync. A search found neither loading its copy on a normal REST request. If one of them loads its copy first, this guard skips the core file and runs their wp_tempnam(). Does either plugin load its bridge copy outside its own bridge entry point? No means safe. Yes means the save runs their copy.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bot's own search found that neither plugin loads its copy outside its own bridge entry point, so the trigger isn't reachable on a REST ability request. And if one were loaded first, core's admin.php:31 require_once would hit the same redeclare on any admin page, so those copies can't coexist with a normal request anyway.

$tmpfile = wp_tempnam( Visualizer_Plugin::NAME );
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
$handle = $tmpfile ? fopen( $tmpfile, 'w' ) : false;
Expand Down
11 changes: 11 additions & 0 deletions tests/e2e/config/mu-plugins/skip-sdk-promotions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Stop the SDK promotions from loading for requests carrying the test header.
*
* The promotions preload wp-admin/includes/file.php, which hides missing
* includes on sites that show no promotions.
*/

if ( defined( 'TI_E2E_TESTING' ) && isset( $_SERVER['HTTP_X_VISUALIZER_E2E_SKIP_PROMOTIONS'] ) ) {
add_filter( 'themeisle_sdk_ran_promos', '__return_true' );
}
72 changes: 72 additions & 0 deletions tests/e2e/specs/abilities.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

/**
* Internal dependencies
*/
const { deleteAllCharts } = require( '../utils/common' );

const UPSERT_PATH = '/wp-abilities/v1/abilities/visualizer/upsert-chart/run';

// Keeps the admin file helpers unloaded, as on sites without SDK promotions.
const HEADERS = { 'X-Visualizer-E2E-Skip-Promotions': '1' };

const CHART_INPUT = {
type: 'line',
columns: [
{ name: 'Month', type: 'string' },
{ name: 'Sales', type: 'number' },
],
rows: [
[ 'Jan', 10 ],
[ 'Feb', 20 ],
],
};

test.describe( 'Chart abilities over REST', () => {
test.beforeEach( async ( { requestUtils } ) => {
await deleteAllCharts( requestUtils );
} );

test.afterAll( async ( { requestUtils } ) => {
await deleteAllCharts( requestUtils );
} );

test( 'upsert-chart dry run returns the parsed preview from manual data', async ( {
requestUtils,
} ) => {
const result = await requestUtils.rest( {
method: 'POST',
path: UPSERT_PATH,
headers: HEADERS,
data: { input: { ...CHART_INPUT, dry_run: true } },
} );

expect( result.dry_run ).toBe( true );
expect( result.valid ).toBe( true );
expect( result.parsed_preview ).toBeTruthy();
expect( JSON.stringify( result.parsed_preview ) ).toContain( 'Feb' );
} );

test( 'upsert-chart creates a chart from manual data', async ( {
requestUtils,
} ) => {
const result = await requestUtils.rest( {
method: 'POST',
path: UPSERT_PATH,
headers: HEADERS,
data: { input: CHART_INPUT },
} );

expect( result.created ).toBe( true );
expect( result.id ).toBeGreaterThan( 0 );

const chart = await requestUtils.rest( {
path: `/wp/v2/visualizer/${ result.id }`,
params: { context: 'edit' },
} );
expect( chart.status ).toBe( 'publish' );
} );
} );
Loading