diff --git a/classes/Visualizer/Gutenberg/Block.php b/classes/Visualizer/Gutenberg/Block.php index d8acb490..a9c5d667 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'], $json_headers['auth_url'] ); + } $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..4fae0c1c 100644 --- a/classes/Visualizer/Module.php +++ b/classes/Visualizer/Module.php @@ -924,20 +924,68 @@ function ( $matches ) { return $sanitize ? self::sanitize_chart_data( $altered ) : $altered; } + /** + * Returns the saved credentials of a JSON chart for a request to the endpoint they were saved for. + * + * A user who does not know the credentials can then not send them to another address, or read them + * back from an address that returns its request headers. A save writes that endpoint beside the + * credentials as `auth_url`, so a later change of the chart endpoint does not move them. Headers of an + * older version have no `auth_url`; their credentials belong to the chart endpoint. + * + * @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 = is_array( $headers ) && isset( $headers['auth_url'] ) ? $headers['auth_url'] : get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, true ); + $saved_url = is_string( $saved_url ) ? trim( $saved_url ) : ''; + if ( ! is_array( $headers ) || empty( $headers['auth'] ) || ! self::is_json_endpoint( $saved_url, $url ) ) { + return null; + } + $auth = $headers['auth']; + if ( is_string( $auth ) || ( is_array( $auth ) && isset( $auth['username'], $auth['password'] ) && is_string( $auth['username'] ) && is_string( $auth['password'] ) ) ) { + return $auth; + } + return null; + } + + /** + * Tells whether a request address is the saved JSON endpoint of a chart. + * + * The chart editor prints the saved endpoint with esc_url(), which encodes some characters, and the browser + * decodes the HTML entities. The form then sends that address, so it counts as the saved endpoint too. + * + * @param string $saved_url The saved JSON endpoint. + * @param string $url The address of the request. + * @return bool + */ + private static function is_json_endpoint( $saved_url, $url ) { + $url = trim( $url ); + return '' !== $saved_url && ( $url === $saved_url || $url === html_entity_decode( esc_url( $saved_url ), ENT_QUOTES ) ); + } + /** * Saves a chart's JSON endpoint, recording the user who changes it as the WooCommerce report user. * * A scheduled refresh of the endpoint then uses this user's report access, - * not the access of whoever authorized an earlier endpoint. + * not the access of whoever authorized an earlier endpoint. Saved credentials belong to the endpoint + * they were saved for, so a new endpoint removes them. * * @param int $chart_id The chart ID. - * @param string $url The JSON endpoint. + * @param string $url The JSON endpoint, with slashes. * @return void */ public static function update_json_url( $chart_id, $url ) { - if ( get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, true ) !== $url ) { + $saved_url = trim( (string) get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, true ) ); + if ( $saved_url !== $url ) { update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, get_current_user_id() ); } + $headers = get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_HEADERS, true ); + if ( is_array( $headers ) && isset( $headers['auth'] ) && ! self::is_json_endpoint( $saved_url, wp_unslash( $url ) ) ) { + unset( $headers['auth'], $headers['auth_url'] ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_HEADERS, wp_slash( $headers ) ); + } update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, $url ); } diff --git a/classes/Visualizer/Module/Abilities.php b/classes/Visualizer/Module/Abilities.php index 73861272..7952b1dc 100644 --- a/classes/Visualizer/Module/Abilities.php +++ b/classes/Visualizer/Module/Abilities.php @@ -968,22 +968,14 @@ private function setJsonSource( $chart, $input, $hours, $dry_run ) { $params['paging'] = sanitize_text_field( $input['json_paging'] ); } - // 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; - } - } + // Credentials saved from the chart editor are reused for the same endpoint only; they are never accepted or returned here. + // Each source gets them through setAuth(), because a source prints its parameters to the debug log. + $auth = self::get_saved_json_auth( $chart->ID, $url ); // First pass: discover the keys available under the chosen root. - $probe = $this->fetchSource( new Visualizer_Source_Json( $params ) ); + $probe_source = new Visualizer_Source_Json( $params ); + $probe_source->setAuth( $auth ); + $probe = $this->fetchSource( $probe_source ); if ( is_wp_error( $probe ) ) { return $probe; } @@ -997,7 +989,9 @@ private function setJsonSource( $chart, $input, $hours, $dry_run ) { if ( ! $mapping ) { if ( $dry_run ) { - $roots = ( new Visualizer_Source_Json( $params ) )->fetchRoots(); + $roots_source = new Visualizer_Source_Json( $params ); + $roots_source->setAuth( $auth ); + $roots = $roots_source->fetchRoots(); return array( 'id' => $chart->ID, 'source' => 'json', @@ -1025,6 +1019,7 @@ private function setJsonSource( $chart, $input, $hours, $dry_run ) { } $source = new Visualizer_Source_Json( $params ); + $source->setAuth( $auth ); try { $source->fetchFromEditableTable(); } catch ( Exception $e ) { @@ -1053,7 +1048,9 @@ private function setJsonSource( $chart, $input, $hours, $dry_run ) { $headers = array( 'method' => $params['method'] ); if ( $auth ) { - $headers['auth'] = $auth; + // add_post_meta() removes slashes, and the saved value is already without them. + $headers['auth'] = wp_slash( $auth ); + $headers['auth_url'] = wp_slash( $params['url'] ); } add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS, $headers ); if ( ! empty( $params['paging'] ) ) { diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 390400e8..5b8cd750 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -177,9 +177,10 @@ 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 = wp_parse_args( $_POST['params'] ); $source = new Visualizer_Source_Json( $params ); + $source->setAuth( self::saved_json_auth_for_editor( $params, isset( $params['chart'] ) ? absint( $params['chart'] ) : 0 ) ); $roots = $source->fetchRoots(); if ( empty( $roots ) ) { @@ -213,6 +214,7 @@ public function getJsonData() { } $source = new Visualizer_Source_Json( $params ); + $source->setAuth( self::saved_json_auth_for_editor( $params, absint( $chart_id ) ) ); $source->fetch(); $data = $source->getRawData(); @@ -224,6 +226,24 @@ public function getJsonData() { wp_send_json_success( array( 'table' => $data, 'root' => $params['root'], 'url' => $params['url'], 'paging' => $source->getPaginationElements() ) ); } + /** + * Returns the saved credentials that a JSON request from the chart editor uses. + * + * The chart editor does not show saved credentials, so its forms send empty credential fields for them. + * A request that brings credentials, or asks to remove the saved ones, does not use them. + * + * @param array $params JSON source parameters from the chart editor, with slashes. + * @param int $chart_id The chart ID. + * @return array{username: string, password: string}|string|null The credentials, or null when the request uses none. + */ + private static function saved_json_auth_for_editor( $params, $chart_id ) { + $has_auth_input = ! empty( $params['clear_auth'] ) || ! empty( $params['auth'] ) || ! empty( $params['username'] ) || ! empty( $params['password'] ); + if ( $has_auth_input || empty( $params['url'] ) || ! is_string( $params['url'] ) || ! self::can_edit_chart( $chart_id ) ) { + return null; + } + return self::get_saved_json_auth( $chart_id, wp_unslash( $params['url'] ) ); + } + /** * Updates the database with the correct post parameters for JSON-endpoint charts. * @@ -243,7 +263,11 @@ 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::saved_json_auth_for_editor( $params, $chart_id ); + $source = new Visualizer_Source_Json( $params ); + $source->setAuth( $saved_auth ); update_post_meta( $chart->ID, Visualizer_Plugin::CF_EDITABLE_TABLE, true ); $source->fetchFromEditableTable(); @@ -262,6 +286,14 @@ 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 is already without them. + $headers['auth'] = wp_slash( $saved_auth ); + } + + if ( isset( $headers['auth'] ) ) { + // The credentials belong to this endpoint. See get_saved_json_auth(). + $headers['auth_url'] = $params['url']; } add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS, $headers ); diff --git a/classes/Visualizer/Module/Setup.php b/classes/Visualizer/Module/Setup.php index cea683fd..e19292c3 100644 --- a/classes/Visualizer/Module/Setup.php +++ b/classes/Visualizer/Module/Setup.php @@ -69,6 +69,8 @@ public function __construct( Visualizer_Plugin $plugin ) { $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->_addFilter( 'map_meta_cap', 'deny_json_headers_meta_caps', 10, 4 ); + $this->_addFilter( 'wxr_export_skip_postmeta', 'skip_json_headers_in_export', 10, 2 ); $this->_addAction( 'admin_init', 'adminInit' ); $this->_addAction( 'init', 'setupCustomPostTypes' ); @@ -328,6 +330,37 @@ public function protect_chart_meta( $is_protected, $meta_key, $meta_type ) { return $is_protected || ( 'post' === $meta_type && 0 === strpos( (string) $meta_key, 'visualizer-' ) ); } + /** + * Refuses the meta capabilities for the JSON headers of a chart, which hold the saved credentials. + * + * A super administrator of a network passes the capability of every other protected key, and XML-RPC + * returns the custom fields that its user can edit. + * + * @param list $caps The primitive capabilities that the user needs. + * @param string $cap The capability that is checked. + * @param int $user_id The user ID. + * @param array $args The arguments of the check: the post ID, then the meta key. + * @return list + */ + public function deny_json_headers_meta_caps( $caps, $cap, $user_id, $args ) { + $is_meta_cap = in_array( $cap, array( 'edit_post_meta', 'add_post_meta', 'delete_post_meta' ), true ); + if ( $is_meta_cap && isset( $args[1] ) && Visualizer_Plugin::CF_JSON_HEADERS === $args[1] ) { + return array( 'do_not_allow' ); + } + return $caps; + } + + /** + * Leaves the JSON headers of a chart, which hold the saved credentials, out of a WordPress export. + * + * @param bool $skip Whether the export leaves the meta out. + * @param string $meta_key The meta key. + * @return bool + */ + public function skip_json_headers_in_export( $skip, $meta_key ) { + return $skip || Visualizer_Plugin::CF_JSON_HEADERS === $meta_key; + } + /** * Refresh the specific chart from the db. * diff --git a/classes/Visualizer/Remote/Fetch.php b/classes/Visualizer/Remote/Fetch.php index b1923cae..be2a1f3e 100644 --- a/classes/Visualizer/Remote/Fetch.php +++ b/classes/Visualizer/Remote/Fetch.php @@ -384,7 +384,7 @@ private static function ip_in_range( $ip, $cidr ) { * @param string $second Second URL. * @return bool */ - private static function same_origin( $first, $second ) { + public static function same_origin( $first, $second ) { $first = wp_parse_url( $first ); $second = wp_parse_url( $second ); if ( ! is_array( $first ) || ! is_array( $second ) || empty( $first['scheme'] ) || empty( $first['host'] ) || empty( $second['scheme'] ) || empty( $second['host'] ) ) { 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; ?>