Skip to content
Merged
4 changes: 4 additions & 0 deletions classes/Visualizer/Gutenberg/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
54 changes: 51 additions & 3 deletions classes/Visualizer/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment thread
pirate-bot marked this conversation as resolved.
Resolved

/**
* 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 ) );
}
Comment thread
pirate-bot marked this conversation as resolved.
Resolved

/**
* 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 );
}

Expand Down
29 changes: 13 additions & 16 deletions classes/Visualizer/Module/Abilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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',
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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'] ) ) {
Expand Down
34 changes: 33 additions & 1 deletion classes/Visualizer/Module/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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();

Expand All @@ -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<string, mixed> $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'] ) );
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
}

/**
* Updates the database with the correct post parameters for JSON-endpoint charts.
*
Expand All @@ -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();

Expand All @@ -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 );
Expand Down
33 changes: 33 additions & 0 deletions classes/Visualizer/Module/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -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<string> $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<int, mixed> $args The arguments of the check: the post ID, then the meta key.
* @return list<string>
*/
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.
*
Expand Down
2 changes: 1 addition & 1 deletion classes/Visualizer/Remote/Fetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand Down
24 changes: 16 additions & 8 deletions classes/Visualizer/Render/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
?>
<div id="visualizer-json-screen" style="display: none">
<div class="visualizer-json-form">
<h3 class="viz-step step1"><?php _e( 'STEP 1: Specify the JSON endpoint/URL', 'visualizer' ); ?></h3>
<div>
<form id="json-endpoint-form">
<input type="hidden" name="chart" value="<?php echo (int) $id; ?>">
<div class="json-wizard-hints">
<ul class="info">
<li>
Expand Down Expand Up @@ -247,15 +246,13 @@ class="visualizer-input json-form-element"
type="text"
id="vz-import-json-username"
name="username"
value="<?php echo esc_attr( $auth_username ); ?>"
placeholder="<?php esc_html_e( 'Username/Access Key', 'visualizer' ); ?>"
class="json-form-element">
&
<input
type="password"
id="vz-import-json-password"
name="password"
value="<?php echo esc_attr( $auth_password ); ?>"
placeholder="<?php esc_html_e( 'Password/Secret Key', 'visualizer' ); ?>"
class="json-form-element">
</div>
Expand All @@ -271,11 +268,22 @@ class="json-form-element">
type="text"
id="vz-import-json-auth"
name="auth"
value="<?php echo esc_attr( $auth_string ); ?>"
placeholder="<?php esc_html_e( 'e.g. SharedKey <AccountName>:<Signature>', 'visualizer' ); ?>"
class="visualizer-input json-form-element">
</div>
</div>
<?php if ( $has_saved_auth ) { ?>
<div class="json-wizard-header">
<div></div>
<div>
<p class="description"><?php esc_html_e( 'Credentials are saved for this endpoint and are not shown. Leave the fields empty to keep them, or enter new credentials to replace them.', 'visualizer' ); ?></p>
<label>
<input type="checkbox" name="clear_auth" value="1" class="json-form-element">
<?php esc_html_e( 'Remove the saved credentials', 'visualizer' ); ?>
</label>
</div>
</div>
<?php } ?>
<div class="json-wizard-header">
<div class="field-title"><?php esc_html_e( 'Additional headers', 'visualizer' ); ?></div>
<div>
Expand Down
29 changes: 23 additions & 6 deletions classes/Visualizer/Source/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ public function __construct( $params = null ) {
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON source initialized.', 'debug', __FILE__, __LINE__ );
}

/**
* Sets the credentials of the requests, for a source whose parameters bring none.
*
* @param array{username: string, password: string}|string|null $auth The credentials; null changes nothing.
* @return void
*/
public function setAuth( $auth ) {
if ( is_string( $auth ) ) {
$this->_headers['auth'] = $auth;
} elseif ( is_array( $auth ) ) {
$this->_headers['username'] = $auth['username'];
$this->_headers['password'] = $auth['password'];
}
}

/**
* Get the root elements for JSON-endpoint.
*
Expand Down Expand Up @@ -429,15 +444,17 @@ private function getJSON( $url = null ) {
private function connect( $url ) {
$args = array( 'method' => 'GET' );
if ( ! empty( $this->_headers ) ) {
$args = array( 'method' => strtoupper( $this->_headers['method'] ) );
if ( array_key_exists( 'auth', $this->_headers ) ) {
if ( ! empty( $this->_headers['auth'] ) ) {
$header_auth = explode( ':', $this->_headers['auth'] );
$args = array( 'method' => isset( $this->_headers['method'] ) ? strtoupper( $this->_headers['method'] ) : 'GET' );
// A next-page address comes from the response. The credentials go to the origin of the endpoint only.
$headers = Visualizer_Remote_Fetch::same_origin( $this->_url, $url ) ? $this->_headers : array();
if ( array_key_exists( 'auth', $headers ) ) {
if ( ! empty( $headers['auth'] ) ) {
$header_auth = explode( ':', $headers['auth'] );
$header_key = explode( ' ', reset( $header_auth ) );
$args['headers'] = array( 'Authorization' => reset( $header_key ) . ' ' . base64_encode( end( $header_key ) . ':' . end( $header_auth ) ) );
}
} elseif ( array_key_exists( 'username', $this->_headers ) && array_key_exists( 'password', $this->_headers ) && ! empty( $this->_headers['username'] ) && ! empty( $this->_headers['password'] ) ) {
$args['headers'] = array( 'Authorization' => 'Basic ' . base64_encode( $this->_headers['username'] . ':' . $this->_headers['password'] ) );
} elseif ( ! empty( $headers['username'] ) && ! empty( $headers['password'] ) ) {
$args['headers'] = array( 'Authorization' => 'Basic ' . base64_encode( $headers['username'] . ':' . $headers['password'] ) );
}
}

Expand Down
9 changes: 7 additions & 2 deletions js/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,9 +769,14 @@
return false;
}

// populate the form elements that are in the other tabs.
// populate the form elements that are in the other tabs. Drop the copies of an earlier submit first.
$('#json-conclude-form .json-form-copy').remove();
$('#json-conclude-form-helper .json-form-element, #json-endpoint-form .json-form-element, #json-root-form .json-form-element, #vz-import-json .json-form-element, #vz-import-woo-report .json-form-element:not(:disabled)').each(function(x, y){
$('#json-conclude-form').append('<input type="hidden" name="' + y.name + '" value="' + y.value + '">');
// An unchecked box is not part of the form.
if(y.type === 'checkbox' && !y.checked){
return;
}
$('#json-conclude-form').append($('<input type="hidden" class="json-form-copy">').attr('name', y.name).val(y.value));
});

$('body').trigger('visualizer:json:form:submit');
Expand Down
Loading
Loading