From 5a425f1d490ed8e5900311b0b1c7da568e0d30a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Thu, 26 Nov 2026 16:16:23 +0200 Subject: [PATCH 01/22] fix: escape the attributes of chart action buttons A saved action value is printed into HTML attributes. Escape each part and skip an action that the plugin does not define. Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module/Frontend.php | 5 ++- tests/test-frontend-actions.php | 48 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 tests/test-frontend-actions.php diff --git a/classes/Visualizer/Module/Frontend.php b/classes/Visualizer/Module/Frontend.php index 9d589da5..4dd63de8 100644 --- a/classes/Visualizer/Module/Frontend.php +++ b/classes/Visualizer/Module/Frontend.php @@ -475,9 +475,12 @@ public function renderChart( $atts ) { $key = $array[0]; $mime = end( $array ); } + if ( ! isset( $actions[ $key ] ) ) { + continue; + } $label = $actions[ $key ]['label']; $title = $actions[ $key ]['title']; - $actions_div .= sprintf( '_getDataAs( $atts['id'], 'csv' ); diff --git a/tests/test-frontend-actions.php b/tests/test-frontend-actions.php new file mode 100644 index 00000000..adf7e7fb --- /dev/null +++ b/tests/test-frontend-actions.php @@ -0,0 +1,48 @@ +post->create( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_content' => serialize( array( array( 'a', 1 ) ) ), + ) + ); + update_post_meta( $id, Visualizer_Plugin::CF_CHART_TYPE, 'pie' ); + update_post_meta( $id, Visualizer_Plugin::CF_SERIES, array( array( 'label' => 'x', 'type' => 'string' ), array( 'label' => 'y', 'type' => 'number' ) ) ); + update_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, array( 'actions' => $actions ) ); + + return do_shortcode( '[visualizer id="' . $id . '"]' ); + } + + /** + * The part after the action name stays inside its attribute. + */ + public function test_action_mime_cannot_add_an_attribute() { + $markup = $this->render_chart_with_actions( array( 'print;x" onfocus="alert(1)' ) ); + + $this->assertStringContainsString( 'data-visualizer-mime="x" onfocus="alert(1)"', $markup ); + } + + /** + * An action that the plugin does not define gets no button. + */ + public function test_unknown_action_gets_no_button() { + $markup = $this->render_chart_with_actions( array( 'x" onfocus="alert(1)', 'csv;application/csv' ) ); + + $this->assertStringNotContainsString( 'onfocus', $markup ); + $this->assertStringContainsString( 'data-visualizer-type="csv"', $markup ); + } +} From 94cbd144b457303ee51aa421a388909950a390bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Thu, 26 Nov 2026 16:24:02 +0200 Subject: [PATCH 02/22] fix: protect the chart meta keys Chart meta selects the data source of a chart. Mark the keys as protected, so that only the plugin writes them, and refuse file access in a stored query. Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module/Setup.php | 17 +++++ classes/Visualizer/Source/Query.php | 4 + tests/test-chart-meta-protection.php | 109 +++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 tests/test-chart-meta-protection.php diff --git a/classes/Visualizer/Module/Setup.php b/classes/Visualizer/Module/Setup.php index 3f530b35..cea683fd 100644 --- a/classes/Visualizer/Module/Setup.php +++ b/classes/Visualizer/Module/Setup.php @@ -68,6 +68,7 @@ public function __construct( Visualizer_Plugin $plugin ) { $this->_addAction( 'init', 'maybe_reschedule_refresh_db' ); $this->_addAction( 'action_scheduler_ensure_recurring_actions', 'ensure_refresh_db_action' ); $this->_addFilter( 'visualizer_schedule_refresh_chart', 'refresh_db_for_chart', 10, 3 ); + $this->_addFilter( 'is_protected_meta', 'protect_chart_meta', 10, 3 ); $this->_addAction( 'admin_init', 'adminInit' ); $this->_addAction( 'init', 'setupCustomPostTypes' ); @@ -311,6 +312,22 @@ public function adminInit() { } + /** + * Marks the chart meta as protected. + * + * Chart meta selects the data source and holds its query, URL and credentials. A protected key + * cannot be written through the Custom Fields panel, XML-RPC or the REST API, so only the plugin sets it. + * + * @param bool $is_protected Whether the key is protected. + * @param string $meta_key Meta key. + * @param string $meta_type Type of the object the meta belongs to. + * + * @return bool + */ + public function protect_chart_meta( $is_protected, $meta_key, $meta_type ) { + return $is_protected || ( 'post' === $meta_type && 0 === strpos( (string) $meta_key, 'visualizer-' ) ); + } + /** * Refresh the specific chart from the db. * diff --git a/classes/Visualizer/Source/Query.php b/classes/Visualizer/Source/Query.php index b72a142a..c1296c71 100644 --- a/classes/Visualizer/Source/Query.php +++ b/classes/Visualizer/Source/Query.php @@ -126,6 +126,10 @@ public function fetch( $as_html = false, $results_as_numeric_array = false, $raw 'SAVEPOINT', 'TRANSACTION', 'SET', + + 'INTO\s+OUTFILE', + 'INTO\s+DUMPFILE', + 'LOAD_FILE', ); $disallow_regex = implode( '|', diff --git a/tests/test-chart-meta-protection.php b/tests/test-chart-meta-protection.php new file mode 100644 index 00000000..789481e5 --- /dev/null +++ b/tests/test-chart-meta-protection.php @@ -0,0 +1,109 @@ + + */ + public function chart_meta_keys() { + return array( + 'source' => array( Visualizer_Plugin::CF_SOURCE ), + 'db query' => array( Visualizer_Plugin::CF_DB_QUERY ), + 'chart image' => array( Visualizer_Plugin::CF_CHART_IMAGE ), + 'remote db' => array( Visualizer_Plugin::CF_REMOTE_DB_PARAMS ), + 'report user' => array( Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER ), + 'd3 code' => array( Visualizer_Module_AIBuilder::CF_D3_CODE ), + ); + } + + /** + * The author of a chart cannot write chart meta through the WordPress meta capabilities. + * + * @dataProvider chart_meta_keys + * + * @param string $meta_key Meta key. + */ + public function test_chart_meta_is_not_writable_through_meta_capabilities( $meta_key ) { + $user_id = self::factory()->user->create( array( 'role' => 'contributor' ) ); + $chart_id = self::factory()->post->create( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_author' => $user_id, + 'post_status' => 'draft', + ) + ); + + $this->assertTrue( user_can( $user_id, 'edit_post', $chart_id ) ); + foreach ( array( 'add_post_meta', 'edit_post_meta', 'delete_post_meta' ) as $capability ) { + $this->assertFalse( user_can( $user_id, $capability, $chart_id, $meta_key ), $capability ); + } + $this->assertTrue( user_can( $user_id, 'add_post_meta', $chart_id, 'my-note' ) ); + } + + /** + * A chart created through XML-RPC does not get the data source that its custom fields ask for. + */ + public function test_xmlrpc_cannot_set_the_data_source_of_a_chart() { + $this->make_user_by_role( 'contributor' ); + + $chart_id = $this->myxmlrpcserver->wp_newPost( + array( + 1, + 'contributor', + 'contributor', + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => 'draft', + 'custom_fields' => array( + array( + 'key' => Visualizer_Plugin::CF_SOURCE, + 'value' => 'Visualizer_Source_Query', + ), + array( + 'key' => Visualizer_Plugin::CF_DB_QUERY, + 'value' => 'SELECT user_login FROM wp_users', + ), + ), + ), + ) + ); + + $this->assertNotIXRError( $chart_id ); + $this->assertSame( '', get_post_meta( (int) $chart_id, Visualizer_Plugin::CF_SOURCE, true ) ); + $this->assertSame( '', get_post_meta( (int) $chart_id, Visualizer_Plugin::CF_DB_QUERY, true ) ); + } + + /** + * Queries that read or write a file on the database server. + * + * @return array + */ + public function file_queries() { + return array( + 'LOAD_FILE' => array( "SELECT LOAD_FILE('/etc/passwd') AS a" ), + 'INTO OUTFILE' => array( "SELECT 'x' AS a INTO OUTFILE '/tmp/visualizer-test.txt'" ), + 'INTO DUMPFILE' => array( "SELECT 'x' AS a INTO DUMPFILE '/tmp/visualizer-test.txt'" ), + ); + } + + /** + * The database source refuses a query that reads or writes a file. + * + * @dataProvider file_queries + * + * @param string $query SQL query. + */ + public function test_database_source_refuses_file_access( $query ) { + $source = new Visualizer_Source_Query( $query ); + + $this->assertFalse( $source->fetch( false ) ); + $this->assertSame( 'Only SELECT queries are allowed', $source->get_error() ); + } + +} From 3944494b2502ec5715d5b3ecc55903499939cc20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Thu, 26 Nov 2026 16:24:57 +0200 Subject: [PATCH 03/22] fix: delete only the chart's own image when the image changes The old image was deleted by the ID in the chart meta, whatever that ID named. An image save also needs the editor nonce now. Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module/Chart.php | 11 ++--- tests/test-chart-image.php | 68 +++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 tests/test-chart-image.php diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 34d615ab..1d5e3446 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -651,7 +651,7 @@ public function renderChartPages() { $_POST['save_chart_image'] = isset( $_POST['save_chart_image'] ) && 'yes' === $_POST['save_chart_image'] ? true : false; $_POST['lazy_load_chart'] = isset( $_POST['lazy_load_chart'] ) && 'yes' === $_POST['lazy_load_chart'] ? true : false; - if ( isset( $_POST['chart-img'] ) && ! empty( $_POST['chart-img'] ) ) { + if ( ! empty( $_POST['chart-img'] ) && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) { $attachment_id = $this->save_chart_image( $_POST['chart-img'], $chart_id, $_POST['save_chart_image'] ); if ( $attachment_id ) { update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $attachment_id ); @@ -1752,9 +1752,11 @@ public function saveFilter() { * @return int Attachment ID, or 0 when no attachment was saved. */ public function save_chart_image( $base64_img, $chart_id, $save_attachment = true ) { - // Delete old chart image. - $old_attachment_id = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ); - if ( $old_attachment_id ) { + $filename = 'visualization-' . $chart_id . '.png'; + + // Delete the old chart image, but only a file that this method made for this chart. + $old_attachment_id = (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ); + if ( $old_attachment_id && basename( (string) get_attached_file( $old_attachment_id ) ) === $filename ) { wp_delete_attachment( $old_attachment_id, true ); } @@ -1773,7 +1775,6 @@ public function save_chart_image( $base64_img, $chart_id, $save_attachment = tru if ( false === $decoded || 0 !== strncmp( $decoded, "\x89PNG\r\n\x1a\n", 8 ) ) { return 0; } - $filename = 'visualization-' . $chart_id . '.png'; $file_type = 'image/png'; $hashed_filename = $filename; diff --git a/tests/test-chart-image.php b/tests/test-chart-image.php new file mode 100644 index 00000000..6b088bbf --- /dev/null +++ b/tests/test-chart-image.php @@ -0,0 +1,68 @@ +_setRole( 'contributor' ); + + return self::factory()->post->create( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_author' => get_current_user_id(), + 'post_status' => 'draft', + ) + ); + } + + /** + * An attachment that the plugin did not make for the chart is not deleted when the chart image changes. + */ + public function test_image_save_keeps_an_attachment_of_another_owner() { + $attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg' ); + $chart_id = $this->create_own_chart(); + update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $attachment_id ); + + $module = new Visualizer_Module_Chart( Visualizer_Plugin::instance() ); + $module->save_chart_image( self::PNG, $chart_id, false ); + + $this->assertInstanceOf( 'WP_Post', get_post( $attachment_id ) ); + } + + /** + * A request without the editor nonce does not save a chart image. + */ + public function test_image_save_needs_the_editor_nonce() { + $chart_id = $this->create_own_chart(); + $_GET = array( 'chart' => $chart_id ); + $_POST = array( + 'chart-img' => self::PNG, + 'save_chart_image' => 'yes', + ); + + ob_start(); + try { + $this->_handleAjax( 'visualizer-edit-chart' ); + } catch ( WPAjaxDieContinueException $e ) { + // The handler ends the request. + } catch ( WPAjaxDieStopException $ee ) { + // The handler ends the request. + } + ob_end_clean(); + + $this->assertSame( '', get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ) ); + } +} From 34d8ed6c395a24abcf441f6439b40e11e4e8b46c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 26 Nov 2026 20:01:50 +0200 Subject: [PATCH 04/22] chore(deps-dev): bump phpstan/phpstan from 2.3.6 to 2.3.7 (#1420) Bumps [phpstan/phpstan](https://github.com/phpstan/phpstan-phar-composer-source) from 2.3.6 to 2.3.7. - [Commits](https://github.com/phpstan/phpstan-phar-composer-source/commits) --- updated-dependencies: - dependency-name: phpstan/phpstan dependency-version: 2.3.7 dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 43581a00..390a0a64 100644 --- a/composer.lock +++ b/composer.lock @@ -1066,11 +1066,11 @@ }, { "name": "phpstan/phpstan", - "version": "2.3.6", + "version": "2.3.7", "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/00cedebb76cc82c1592414fa4f616cefdccda16f", - "reference": "00cedebb76cc82c1592414fa4f616cefdccda16f", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/90c91c2498849ab8cc4e873864bbf1cb2bf6ea12", + "reference": "90c91c2498849ab8cc4e873864bbf1cb2bf6ea12", "shasum": "" }, "require": { @@ -1126,7 +1126,7 @@ "type": "github" } ], - "time": "2026-11-15T19:58:26+00:00" + "time": "2026-11-19T10:31:11+00:00" }, { "name": "phpunit/php-code-coverage", From 9c38d233e2c414dfb9e1e2e271a9d1a12ec25b7c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 27 Nov 2026 14:03:47 +0200 Subject: [PATCH 05/22] chore(deps): bump codeinwp/themeisle-sdk from 3.3.73 to 3.3.74 (#1423) Bumps [codeinwp/themeisle-sdk](https://github.com/Codeinwp/themeisle-sdk) from 3.3.73 to 3.3.74. - [Release notes](https://github.com/Codeinwp/themeisle-sdk/releases) - [Commits](https://github.com/Codeinwp/themeisle-sdk/compare/v3.3.73...v3.3.74) --- updated-dependencies: - dependency-name: codeinwp/themeisle-sdk dependency-version: 3.3.74 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- composer.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.lock b/composer.lock index 390a0a64..678f9ef8 100644 --- a/composer.lock +++ b/composer.lock @@ -8,16 +8,16 @@ "packages": [ { "name": "codeinwp/themeisle-sdk", - "version": "3.3.73", + "version": "3.3.74", "source": { "type": "git", "url": "https://github.com/Codeinwp/themeisle-sdk.git", - "reference": "f242bafc4d216bcb1957d800bf9bde679514d137" + "reference": "3131818293a5c08105d443fa7c476cfe339b0299" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Codeinwp/themeisle-sdk/zipball/f242bafc4d216bcb1957d800bf9bde679514d137", - "reference": "f242bafc4d216bcb1957d800bf9bde679514d137", + "url": "https://api.github.com/repos/Codeinwp/themeisle-sdk/zipball/3131818293a5c08105d443fa7c476cfe339b0299", + "reference": "3131818293a5c08105d443fa7c476cfe339b0299", "shasum": "" }, "require-dev": { @@ -43,9 +43,9 @@ ], "support": { "issues": "https://github.com/Codeinwp/themeisle-sdk/issues", - "source": "https://github.com/Codeinwp/themeisle-sdk/tree/v3.3.73" + "source": "https://github.com/Codeinwp/themeisle-sdk/tree/v3.3.74" }, - "time": "2026-11-25T13:44:47+00:00" + "time": "2026-11-27T08:23:44+00:00" }, { "name": "neitanod/forceutf8", From b9dbb6102eef2b66f34f14258e387fac761ef680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Fri, 27 Nov 2026 17:06:05 +0200 Subject: [PATCH 06/22] test: cover the key and title of a filtered action Co-Authored-By: Claude Mythos 5.2 --- tests/test-frontend-actions.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test-frontend-actions.php b/tests/test-frontend-actions.php index adf7e7fb..34d49f51 100644 --- a/tests/test-frontend-actions.php +++ b/tests/test-frontend-actions.php @@ -45,4 +45,25 @@ public function test_unknown_action_gets_no_button() { $this->assertStringNotContainsString( 'onfocus', $markup ); $this->assertStringContainsString( 'data-visualizer-type="csv"', $markup ); } + + /** + * The key and the title of an action that a filter adds stay inside their attributes. + */ + public function test_filtered_action_key_and_title_cannot_add_an_attribute() { + add_filter( + 'visualizer_action_buttons', + function ( $actions ) { + $actions['share" data-x="1'] = array( + 'label' => 'Share', + 'title' => 'Share" onfocus="alert(1)', + ); + return $actions; + } + ); + + $markup = $this->render_chart_with_actions( array( 'share" data-x="1' ) ); + + $this->assertStringContainsString( 'data-visualizer-type="share" data-x="1"', $markup ); + $this->assertStringContainsString( 'title="Share" onfocus="alert(1)"', $markup ); + } } From 02e5897d00988afcb7c45d7838d2fab5e90aca26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Sat, 28 Nov 2026 22:50:51 +0200 Subject: [PATCH 07/22] fix: clear the chart image reference when image saving is off Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module/Chart.php | 4 +++- tests/test-chart-image.php | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 1d5e3446..882ca351 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -1754,13 +1754,15 @@ public function saveFilter() { public function save_chart_image( $base64_img, $chart_id, $save_attachment = true ) { $filename = 'visualization-' . $chart_id . '.png'; - // Delete the old chart image, but only a file that this method made for this chart. + // Delete the old chart image, but only a file with the name that this method gives to the image of this chart. $old_attachment_id = (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ); if ( $old_attachment_id && basename( (string) get_attached_file( $old_attachment_id ) ) === $filename ) { wp_delete_attachment( $old_attachment_id, true ); } if ( ! $save_attachment ) { + // The chart has no image now. A copied chart can still point to the image of its source chart. + delete_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE ); return 0; } diff --git a/tests/test-chart-image.php b/tests/test-chart-image.php index 6b088bbf..9c3d7e9c 100644 --- a/tests/test-chart-image.php +++ b/tests/test-chart-image.php @@ -30,6 +30,7 @@ private function create_own_chart() { /** * An attachment that the plugin did not make for the chart is not deleted when the chart image changes. + * With image saving off, the chart does not point to that attachment after the save. */ public function test_image_save_keeps_an_attachment_of_another_owner() { $attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg' ); @@ -40,6 +41,7 @@ public function test_image_save_keeps_an_attachment_of_another_owner() { $module->save_chart_image( self::PNG, $chart_id, false ); $this->assertInstanceOf( 'WP_Post', get_post( $attachment_id ) ); + $this->assertSame( '', get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ) ); } /** From 1ae1c4c94dc07e40d8627cbbcdb92dd47ae423fa Mon Sep 17 00:00:00 2001 From: Vytis Bulkevicius Date: Tue, 1 Dec 2026 12:15:37 +0200 Subject: [PATCH 08/22] fix: a copied chart does not take the image of its source chart Both copy handlers copied the chart image reference, so the copy pointed to the source chart's attachment, which is deleted when the source image changes. --- classes/Visualizer/Module/Chart.php | 10 ++-- tests/test-chart-image.php | 77 +++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 882ca351..390400e8 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -631,8 +631,8 @@ public function renderChartPages() { $post_meta = get_post_meta( $parent_chart_id ); $chart_id = $new_chart_id; foreach ( $post_meta as $key => $value ) { - // A copy refreshes reports with its own author's access. - if ( strpos( $key, 'visualizer-' ) !== false && Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER !== $key ) { + // A copy refreshes reports with its own author's access, and it has no image until it is saved. + if ( strpos( $key, 'visualizer-' ) !== false && ! in_array( $key, array( Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, Visualizer_Plugin::CF_CHART_IMAGE ), true ) ) { add_post_meta( $new_chart_id, $key, self::maybe_decode_content( $value[0] ) ); } } @@ -1476,8 +1476,8 @@ public function cloneChart() { } else { $post_meta = get_post_meta( $chart_id ); foreach ( $post_meta as $key => $value ) { - // A copy refreshes reports with its own author's access. - if ( strpos( $key, 'visualizer-' ) !== false && Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER !== $key ) { + // A copy refreshes reports with its own author's access, and it has no image until it is saved. + if ( strpos( $key, 'visualizer-' ) !== false && ! in_array( $key, array( Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, Visualizer_Plugin::CF_CHART_IMAGE ), true ) ) { add_post_meta( $new_chart_id, $key, self::maybe_decode_content( $value[0] ) ); } } @@ -1761,7 +1761,7 @@ public function save_chart_image( $base64_img, $chart_id, $save_attachment = tru } if ( ! $save_attachment ) { - // The chart has no image now. A copied chart can still point to the image of its source chart. + // The chart has no image now. A chart copied by an older version can still point to the image of its source chart. delete_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE ); return 0; } diff --git a/tests/test-chart-image.php b/tests/test-chart-image.php index 9c3d7e9c..14e79629 100644 --- a/tests/test-chart-image.php +++ b/tests/test-chart-image.php @@ -55,16 +55,87 @@ public function test_image_save_needs_the_editor_nonce() { 'save_chart_image' => 'yes', ); + $this->handle( Visualizer_Plugin::ACTION_EDIT_CHART ); + + $this->assertSame( '', get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ) ); + } + + /** + * A copy of a chart does not point to the image of the source chart. + */ + public function test_copy_does_not_take_the_chart_image() { + $chart_id = $this->create_chart_with_image(); + $_GET = array( + 'nonce' => wp_create_nonce( Visualizer_Plugin::ACTION_CLONE_CHART ), + 'chart' => $chart_id, + ); + + $this->handle( Visualizer_Plugin::ACTION_CLONE_CHART ); + + $this->assertCopyHasNoImage( $chart_id ); + } + + /** + * A translated copy of a chart does not point to the image of the source chart. + */ + public function test_translation_copy_does_not_take_the_chart_image() { + $chart_id = $this->create_chart_with_image(); + $_GET = array( + 'lang' => 'de', + 'parent_chart_id' => $chart_id, + ); + + $this->handle( Visualizer_Plugin::ACTION_CREATE_CHART ); + + $this->assertCopyHasNoImage( $chart_id ); + } + + /** + * Create a chart of the current user that has a saved image. + * + * @return int + */ + private function create_chart_with_image() { + $chart_id = $this->create_own_chart(); + update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, 'line' ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, 123 ); + + return $chart_id; + } + + /** + * Assert that the one copy of a chart has the chart meta but no image. + * + * @param int $chart_id Source chart ID. + */ + private function assertCopyHasNoImage( $chart_id ) { + $copies = get_posts( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => 'any', + 'exclude' => array( $chart_id ), + 'fields' => 'ids', + ) + ); + $this->assertCount( 1, $copies ); + $this->assertSame( 'line', get_post_meta( $copies[0], Visualizer_Plugin::CF_CHART_TYPE, true ) ); + $this->assertSame( '', get_post_meta( $copies[0], Visualizer_Plugin::CF_CHART_IMAGE, true ) ); + } + + /** + * Run an AJAX action that ends the request. + * + * @param string $action AJAX action. + */ + private function handle( $action ) { ob_start(); try { - $this->_handleAjax( 'visualizer-edit-chart' ); + $this->_handleAjax( $action ); } catch ( WPAjaxDieContinueException $e ) { // The handler ends the request. } catch ( WPAjaxDieStopException $ee ) { // The handler ends the request. } ob_end_clean(); - - $this->assertSame( '', get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ) ); } } From 9708978d1293ca57ec18de9b4690793ccf706855 Mon Sep 17 00:00:00 2001 From: Vytis Bulkevicius Date: Tue, 1 Dec 2026 12:18:17 +0200 Subject: [PATCH 09/22] test: cover deleting the chart's own image and keeping another chart's image --- tests/test-chart-image.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/test-chart-image.php b/tests/test-chart-image.php index 14e79629..c413be2d 100644 --- a/tests/test-chart-image.php +++ b/tests/test-chart-image.php @@ -44,6 +44,37 @@ public function test_image_save_keeps_an_attachment_of_another_owner() { $this->assertSame( '', get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true ) ); } + /** + * A new image of a chart replaces the old image of that chart. + */ + public function test_image_save_deletes_the_old_image_of_the_chart() { + $chart_id = $this->create_own_chart(); + $module = new Visualizer_Module_Chart( Visualizer_Plugin::instance() ); + $old_id = $module->save_chart_image( self::PNG, $chart_id ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $old_id ); + + $new_id = $module->save_chart_image( self::PNG, $chart_id ); + + $this->assertNull( get_post( $old_id ) ); + $this->assertInstanceOf( 'WP_Post', get_post( $new_id ) ); + } + + /** + * A chart that points to the image of another chart does not delete that image. + */ + public function test_image_save_keeps_the_image_of_another_chart() { + $module = new Visualizer_Module_Chart( Visualizer_Plugin::instance() ); + $source_id = $this->create_own_chart(); + $image_id = $module->save_chart_image( self::PNG, $source_id ); + $copy_id = $this->create_own_chart(); + update_post_meta( $copy_id, Visualizer_Plugin::CF_CHART_IMAGE, $image_id ); + + $module->save_chart_image( self::PNG, $copy_id ); + + $this->assertInstanceOf( 'WP_Post', get_post( $image_id ) ); + $this->assertFileExists( get_attached_file( $image_id ) ); + } + /** * A request without the editor nonce does not save a chart image. */ From 672790fd8e30819f24c1c4f070b5e83898298fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Tue, 1 Dec 2026 22:03:32 +0200 Subject: [PATCH 10/22] fix: keep the saved JSON source credentials on the server The chart editor and the chart_data REST field no longer return saved credentials. A save or a fetch with empty credential fields uses the saved credentials for the scheme and host they were saved for. Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Gutenberg/Block.php | 4 + classes/Visualizer/Module.php | 23 ++ classes/Visualizer/Module/Abilities.php | 17 +- classes/Visualizer/Module/Chart.php | 44 +++- classes/Visualizer/Render/Layout.php | 24 +- js/frame.js | 4 + tests/test-ajax.php | 14 +- tests/test-json-credentials.php | 293 ++++++++++++++++++++++++ tests/test-json-headers-xss.php | 129 ++++------- 9 files changed, 436 insertions(+), 116 deletions(-) create mode 100644 tests/test-json-credentials.php diff --git a/classes/Visualizer/Gutenberg/Block.php b/classes/Visualizer/Gutenberg/Block.php index d8acb490..2fc7fab4 100644 --- a/classes/Visualizer/Gutenberg/Block.php +++ b/classes/Visualizer/Gutenberg/Block.php @@ -427,6 +427,10 @@ public function get_visualizer_data( $post ) { if ( ! empty( $json_url ) ) { $data['visualizer-json-schedule'] = $json_schedule; $data['visualizer-json-url'] = $json_url; + // The saved credentials stay on the server. + if ( is_array( $json_headers ) ) { + unset( $json_headers['auth'] ); + } $data['visualizer-json-headers'] = $json_headers; $data['visualizer-json-root'] = $json_root; diff --git a/classes/Visualizer/Module.php b/classes/Visualizer/Module.php index d00b2517..2818fcd2 100644 --- a/classes/Visualizer/Module.php +++ b/classes/Visualizer/Module.php @@ -924,6 +924,29 @@ function ( $matches ) { return $sanitize ? self::sanitize_chart_data( $altered ) : $altered; } + /** + * Returns the saved credentials of a JSON chart for a request to the scheme and host they were saved for. + * + * @param int $chart_id The chart ID. + * @param string $url The JSON endpoint of the request. + * @return array{username: string, password: string}|string|null The credentials, or null when the chart has none for that endpoint. + */ + public static function get_saved_json_auth( $chart_id, $url ) { + $headers = get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_HEADERS, true ); + $saved_url = (string) get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, true ); + if ( ! is_array( $headers ) || empty( $headers['auth'] ) || '' === $saved_url ) { + return null; + } + if ( wp_parse_url( $saved_url, PHP_URL_HOST ) !== wp_parse_url( $url, PHP_URL_HOST ) || wp_parse_url( $saved_url, PHP_URL_SCHEME ) !== wp_parse_url( $url, PHP_URL_SCHEME ) ) { + return null; + } + $auth = $headers['auth']; + if ( is_string( $auth ) || ( is_array( $auth ) && isset( $auth['username'], $auth['password'] ) ) ) { + return $auth; + } + return null; + } + /** * Saves a chart's JSON endpoint, recording the user who changes it as the WooCommerce report user. * diff --git a/classes/Visualizer/Module/Abilities.php b/classes/Visualizer/Module/Abilities.php index 73861272..794026ff 100644 --- a/classes/Visualizer/Module/Abilities.php +++ b/classes/Visualizer/Module/Abilities.php @@ -969,17 +969,12 @@ private function setJsonSource( $chart, $input, $hours, $dry_run ) { } // Credentials saved from the chart editor are reused for the same host only; they are never accepted or returned here. - $saved_headers = get_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS, true ); - $saved_url = (string) get_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_URL, true ); - $auth = array(); - if ( is_array( $saved_headers ) && ! empty( $saved_headers['auth'] ) && '' !== $saved_url && wp_parse_url( $saved_url, PHP_URL_HOST ) === wp_parse_url( $url, PHP_URL_HOST ) && wp_parse_url( $saved_url, PHP_URL_SCHEME ) === wp_parse_url( $url, PHP_URL_SCHEME ) ) { - $auth = $saved_headers['auth']; - if ( is_array( $auth ) && isset( $auth['username'], $auth['password'] ) ) { - $params['username'] = $auth['username']; - $params['password'] = $auth['password']; - } elseif ( is_string( $auth ) ) { - $params['auth'] = $auth; - } + $auth = self::get_saved_json_auth( $chart->ID, $url ); + if ( is_array( $auth ) ) { + $params['username'] = $auth['username']; + $params['password'] = $auth['password']; + } elseif ( is_string( $auth ) ) { + $params['auth'] = $auth; } // First pass: discover the keys available under the chosen root. diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 390400e8..30f7ce46 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -177,7 +177,7 @@ public function getJsonRoots() { wp_send_json_error( array( 'msg' => esc_html__( 'You do not have permission to perform this action.', 'visualizer' ) ) ); } - $params = wp_parse_args( $_POST['params'] ); + $params = self::add_saved_json_auth( wp_parse_args( $_POST['params'] ) ); $source = new Visualizer_Source_Json( $params ); @@ -212,7 +212,7 @@ public function getJsonData() { wp_die(); } - $source = new Visualizer_Source_Json( $params ); + $source = new Visualizer_Source_Json( self::add_saved_json_auth( $params ) ); $source->fetch(); $data = $source->getRawData(); @@ -224,6 +224,40 @@ public function getJsonData() { wp_send_json_success( array( 'table' => $data, 'root' => $params['root'], 'url' => $params['url'], 'paging' => $source->getPaginationElements() ) ); } + /** + * Adds the saved credentials of a chart to JSON request parameters that have none. + * + * The chart editor does not show saved credentials, so its forms send empty credential fields for them. + * + * @param array $params JSON source parameters from the chart editor; `chart` is the chart ID. + * @return array + */ + private static function add_saved_json_auth( $params ) { + $chart_id = isset( $params['chart'] ) ? absint( $params['chart'] ) : 0; + if ( self::has_json_auth_input( $params ) || empty( $params['url'] ) || ! self::can_edit_chart( $chart_id ) ) { + return $params; + } + + $auth = self::get_saved_json_auth( $chart_id, $params['url'] ); + if ( is_string( $auth ) ) { + $params['auth'] = $auth; + } elseif ( is_array( $auth ) ) { + $params['username'] = $auth['username']; + $params['password'] = $auth['password']; + } + return $params; + } + + /** + * Tells whether JSON source parameters from the chart editor bring credentials or ask to remove the saved ones. + * + * @param array $params JSON source parameters from the chart editor. + * @return bool + */ + private static function has_json_auth_input( $params ) { + return ! empty( $params['clear_auth'] ) || ! empty( $params['auth'] ) || ! empty( $params['username'] ) || ! empty( $params['password'] ); + } + /** * Updates the database with the correct post parameters for JSON-endpoint charts. * @@ -243,6 +277,9 @@ public function setJsonData() { $chart = get_post( $chart_id ); + // Read the saved credentials before the new endpoint replaces the one they belong to. + $saved_auth = self::has_json_auth_input( $params ) ? null : self::get_saved_json_auth( $chart_id, wp_unslash( $params['url'] ) ); + $source = new Visualizer_Source_Json( $params ); update_post_meta( $chart->ID, Visualizer_Plugin::CF_EDITABLE_TABLE, true ); $source->fetchFromEditableTable(); @@ -262,6 +299,9 @@ public function setJsonData() { $headers['auth'] = $params['auth']; } elseif ( ! empty( $params['username'] ) && ! empty( $params['password'] ) ) { $headers['auth'] = array( 'username' => $params['username'], 'password' => $params['password'] ); + } elseif ( null !== $saved_auth ) { + // add_post_meta() removes slashes, and the saved value has none. + $headers['auth'] = wp_slash( $saved_auth ); } add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS, $headers ); diff --git a/classes/Visualizer/Render/Layout.php b/classes/Visualizer/Render/Layout.php index ed9b5b21..6c19872a 100644 --- a/classes/Visualizer/Render/Layout.php +++ b/classes/Visualizer/Render/Layout.php @@ -188,19 +188,18 @@ public static function _renderJsonScreen( $args ) { } $methods = apply_filters( 'visualizer_json_request_methods', array( 'GET', 'POST' ) ); - $auth = isset( $headers['auth'] ) ? $headers['auth'] : ''; - $auth_username = is_array( $auth ) && isset( $auth['username'] ) ? $auth['username'] : ''; - $auth_password = is_array( $auth ) && isset( $auth['password'] ) ? $auth['password'] : ''; - $auth_string = is_string( $auth ) ? $auth : ''; + // Saved credentials are never printed. The save keeps them when the credential fields come back empty. + $has_saved_auth = ! empty( $headers['auth'] ); // open the headers by default? - $headers_open = ! empty( $auth_username ) || ! empty( $auth_string ); + $headers_open = $has_saved_auth; ?>