Changeset 2165
- Timestamp:
- 12/05/2015 05:33:32 PM (11 years ago)
- Location:
- sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins
- Files:
-
- 4 added
- 2 edited
-
wcorg-json-api.php (modified) (2 diffs)
-
wcorg-misc.php (modified) (2 diffs)
-
wp-cli-commands (added)
-
wp-cli-commands.php (added)
-
wp-cli-commands/rest-api.php (added)
-
wp-cli-commands/rewrite-rules.php (added)
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wcorg-json-api.php
r1553 r2165 8 8 * 9 9 * Before making any changes to this plugin or updating the JSON API to a new version, make sure you run the 10 * automated tests -- wp wc org-json-apiverify-data-is-scrubbed -- in your sandbox, and then again on production10 * automated tests -- wp wc-rest verify-data-is-scrubbed -- in your sandbox, and then again on production 11 11 * after you deploy any updates. 12 12 * … … 304 304 } 305 305 add_action( 'wp_json_server_before_serve', 'wcorg_json_avoid_nested_callback_conflicts', 11 ); // after the default endpoints are added in `json_api_default_filters()` 306 307 308 /*309 * WP-CLI Commands310 */311 if ( defined( 'WP_CLI' ) && WP_CLI ) {312 /**313 * WordCamp.org JSON API314 */315 class WordCamp_JSON_API_Commands extends WP_CLI_Command {316 /**317 * Verify that no sensitive data is being exposed via the API.318 *319 * @subcommand verify-data-is-scrubbed320 */321 public function verify_data_is_scrubbed() {322 $errors = false;323 $start_timestamp = microtime( true );324 325 // These calls are not formatted in a more compact way because we don't want to short-circuit any of them if one fails326 if ( $this->post_types_exposed() ) {327 $errors = true;328 }329 330 if ( $this->post_meta_exposed() ) {331 $errors = true;332 }333 334 WP_CLI::line();335 WP_CLI::line( sprintf( 'Tests completed in %s seconds', number_format( microtime( true ) - $start_timestamp, 3 ) ) );336 337 if ( $errors ) {338 WP_CLI::error( 'Not all sensitive data has been scrubbed.' );339 } else {340 WP_CLI::success( 'All of the tests passed. If the tests are comprehensive and working properly, then all sensitive data has been properly scrubbed.' );341 }342 }343 344 /**345 * Check if any sensitive post types are being exposed.346 *347 * See note in post_meta_exposed() about test data.348 *349 * @return bool350 */351 protected function post_types_exposed() {352 $errors = false;353 354 WP_CLI::line();355 WP_CLI::line( 'Checking post types.' );356 357 // Check Central and a normal site, because they can have different types loaded358 $post_types_endpoints = array(359 'http://central.wordcamp.org/wp-json/posts/types',360 'http://europe.wordcamp.org/2014/wp-json/posts/types',361 );362 363 $whitelisted_post_types = array(364 'post', 'page', 'attachment', 'revision', 'wcb_speaker', 'wcb_session', 'wcb_sponsor', 'mes',365 'mes-sponsor-level', 'wordcamp'366 );367 368 foreach ( $post_types_endpoints as $request_url ) {369 $request_url = apply_filters( 'wcorg_json_api_verify_data_scrubbed_url', $request_url ); // Use this filter to override the URLs with corresponding endpoints on your sandbox370 $response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_url ) ) );371 372 if ( empty( $response->post->slug ) ) {373 $errors = true;374 WP_CLI::warning( "Unable to retrieve post types from $request_url", false );375 continue;376 }377 378 foreach ( $response as $post_type ) {379 if ( in_array( $post_type->slug, $whitelisted_post_types ) ) {380 WP_CLI::line( "{$post_type->slug} is whitelisted." );381 } else {382 $errors = true;383 WP_CLI::warning( "{$post_type->slug} is being exposed at $request_url" );384 }385 }386 }387 388 return $errors;389 }390 391 /**392 * Check if any sensitive post meta is being exposed.393 *394 * If this were a proper test we'd insert the data into a test db during setup rather than relying on the395 * existence of production data, but this is good enough for our current needs. Just make sure to double396 * check that the meta where checking still exists, otherwise the tests could result in a false-negative.397 *398 * @return bool399 */400 protected function post_meta_exposed() {401 $errors = false;402 403 WP_CLI::line();404 WP_CLI::line( 'Checking post meta.' );405 406 // This is just a representative sample, not a complete list407 $sensitive_post_meta = array(408 'http://central.wordcamp.org/wp-json/posts/3038288' => array( 'Email Address', 'Telephone', 'Mailing Address' ), // A wordcamp post on Central409 'http://central.wordcamp.org/wp-json/posts/2347409' => array( 'mes_email_address' ), // A Multi-Event Sponsor post on Central410 'http://europe.wordcamp.org/2014/wp-json/posts/216283' => array( '_wcb_speaker_email' ), // A Speaker post on a camp site411 );412 413 foreach ( $sensitive_post_meta as $request_url => $sensitive_meta_keys ) {414 $request_url = apply_filters( 'wcorg_json_api_verify_data_scrubbed_url', $request_url ); // Use this filter to override the URLs with corresponding posts on your sandbox415 $response = json_decode( wp_remote_retrieve_body( wp_remote_get( esc_url_raw( $request_url ) ) ) );416 417 if ( ! isset( $response->post_meta ) ) {418 $errors = true;419 WP_CLI::warning( "Unable to retrieve post meta from $request_url", false );420 continue;421 }422 423 foreach ( $response->post_meta as $post_meta ) {424 if ( in_array( $post_meta->key, $sensitive_meta_keys ) ) {425 $errors = true;426 WP_CLI::warning( "{$post_meta->key} is being exposed at $request_url" );427 } else {428 WP_CLI::line( "{$post_meta->key} is whitelisted." );429 }430 }431 }432 433 return $errors;434 }435 }436 437 WP_CLI::add_command( 'wcorg-json-api', 'WordCamp_JSON_API_Commands' );438 } -
sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wcorg-misc.php
r2079 r2165 179 179 * Flush the rewrite rules on the current site. 180 180 * 181 * See WordCamp_ Miscellaneous_Commands::flush_rewrite_rules_everywhere() for an explanation.181 * See WordCamp_CLI_Rewrite_Rules::flush() for an explanation. 182 182 * 183 183 * Requires authentication because flush_rewrite_rules() is expensive and could be used as a DoS vector. … … 193 193 add_action( 'wp_ajax_wcorg_flush_rewrite_rules_everywhere', 'wcorg_flush_rewrite_rules' ); // This isn't used by the wp-cli command, but is useful for manual testing 194 194 add_action( 'wp_ajax_nopriv_wcorg_flush_rewrite_rules_everywhere', 'wcorg_flush_rewrite_rules' ); 195 196 197 /*198 * WP-CLI Commands199 */200 if ( defined( 'WP_CLI' ) && WP_CLI ) {201 /**202 * WordCamp.org Miscellaneous Commands203 */204 class WordCamp_Miscellaneous_Commands extends WP_CLI_Command {205 /**206 * Flush rewrite rules on all sites.207 *208 * Periodically they break for various reasons and need to be reset on all sites. If we209 * just called flush_rewrite_rules() inside a switch_to_blog() loop then each site's210 * plugins wouldn't be loaded and the rewrite rules wouldn't be correct.211 *212 * So instead, this issues an HTTP request to wcorg_flush_rewrite_rules() on each site so213 * that flush_rewrite_rules() will run in the context of the loaded site.214 *215 * @subcommand flush-rewrite-rules-everywhere216 */217 public function flush_rewrite_rules_everywhere() {218 $start_timestamp = microtime( true );219 $error = '';220 $sites = wp_get_sites( array( 'limit' => false ) );221 222 WP_CLI::line();223 224 foreach ( $sites as $site ) {225 $ajax_url = sprintf( 'http://%s%swp-admin/admin-ajax.php', $site['domain'], $site['path'] );226 $display_url = $site['domain'] . rtrim( $site['path'], '/' );227 $nonce = wp_create_nonce( 'flush-rewrite-rules-everywhere-' . $site['blog_id'] );228 229 $response = wp_remote_get( esc_url_raw( add_query_arg(230 array(231 'action' => 'wcorg_flush_rewrite_rules_everywhere',232 'nonce' => $nonce,233 ),234 $ajax_url235 ) ) );236 237 if ( is_wp_error( $response ) ) {238 $success = false;239 $error = $response->get_error_message();240 } else {241 $response = json_decode( wp_remote_retrieve_body( $response ) );242 243 if ( isset( $response->success ) && $response->success ) {244 $success = true;245 } else {246 $success = false;247 $error = isset( $response->data ) ? $response->data : 'Unknown error';248 }249 }250 251 if ( $success ) {252 WP_CLI::line( sprintf( '%s: Flushed', $display_url ) );253 } else {254 WP_CLI::warning( sprintf( '%s: Failed with error: %s', $display_url, $error ) );255 }256 }257 $execution_time = microtime( true ) - $start_timestamp;258 259 WP_CLI::line();260 WP_CLI::line( sprintf(261 'Flushed all rewrite rules in %d minute(s) and %d second(s).',262 floor( $execution_time / 60 ),263 $execution_time % 60264 ) );265 }266 }267 268 WP_CLI::add_command( 'wcorg-misc', 'WordCamp_Miscellaneous_Commands' );269 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)