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 1/7] 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; ?>