<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[1515] sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wcorg-json-api.php: WordCamp JSON API: Add to Meta repo.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/1515">1515</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/1515","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>iandunn</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-04-24 22:11:41 +0000 (Fri, 24 Apr 2015)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>WordCamp JSON API: Add to Meta repo.
It was removed from the private repo in [wordcamp2002].</pre>
<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentmupluginswcorgjsonapiphp">sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wcorg-json-api.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentmupluginswcorgjsonapiphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wcorg-json-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wcorg-json-api.php (rev 0)
+++ sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wcorg-json-api.php 2015-04-24 22:11:41 UTC (rev 1515)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,216 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/*
+ * Customizations to the JSON REST API
+ *
+ * WARNING: It's very important to make sure that only data intended to be public is disclosed by the API. This
+ * is particularly important when it comes to areas where we're customizing the output, like post meta.
+ *
+ * Before making any changes to this plugin or updating the JSON API to a new version, make sure you run the
+ * automated tests -- wp wcorg-json-api verify-data-is-scrubbed -- in your sandbox, and then again on production
+ * after you deploy any updates.
+ *
+ * You may also need to add new tests when making changes to this or other plugins, and/or update existing tests.
+ */
+
+
+/**
+ * Unhook any endpoints that aren't whitelisted
+ *
+ * @param array $endpoints
+ *
+ * @return array
+ */
+function wcorg_json_whitelist_endpoints( $endpoints ) {
+ $whitelisted_endpoints = array(
+ '/posts' => array( 'get_posts' ),
+ '/posts/(?P<id>\d+)' => array( 'get_post' ),
+ // todo Add /posts/types too, because it's useful for debugging and there's no harm. It has a different array structure than the current ones, though, so this will need some work.
+ );
+
+ foreach ( $endpoints as $endpoint => $endpoint_data ) {
+ /*
+ * Don't attempt to scan '/' because it has a different array structure than normal endpoints and is
+ * unlikely to expose anything sensitive.
+ */
+ if ( '/' == $endpoint ) {
+ continue;
+ }
+
+ if ( array_key_exists( $endpoint, $whitelisted_endpoints ) ) {
+ // Allow the endpoint, but remove any of its callbacks that aren't whitelisted and read-only
+
+ foreach ( $endpoint_data as $callback_index => $callback ) {
+ $callback_name = $callback[0][1];
+ $callback_permissions = $callback[1];
+
+ if ( ! in_array( $callback_name, $whitelisted_endpoints[ $endpoint ] ) || WP_JSON_Server::READABLE != $callback_permissions ) {
+ unset( $endpoints[ $endpoint ][ $callback_index ] );
+ }
+ }
+ } else {
+ // Remove endpoints that aren't whitelisted
+
+ unset( $endpoints[ $endpoint ] );
+ }
+ }
+
+ return $endpoints;
+}
+add_filter( 'json_endpoints', 'wcorg_json_whitelist_endpoints', 999 );
+
+/**
+ * Expose a whitelisted set of meta data to unauthenticated JSON API requests
+ *
+ * @param array $prepared_post
+ * @param array $raw_post
+ * @param string $context
+ *
+ * @return array
+ */
+function wcorg_json_expose_whitelisted_meta_data( $prepared_post, $raw_post, $context ) {
+ $whitelisted_post_meta = array(
+ 'wordcamp' => array(
+ 'Start Date (YYYY-mm-dd)', 'End Date (YYYY-mm-dd)', 'Location', 'URL', 'Twitter', 'WordCamp Hashtag',
+ 'Number of Anticipated Attendees', 'Organizer Name', 'WordPress.org Username', 'Venue Name',
+ 'Physical Address', 'Maximum Capacity', 'Available Rooms', 'Website URL', 'Exhibition Space Available',
+ // todo add Multi-Event Sponsor Region, but convert from ID to name so it will be meaningful without having to do extra lookup? Or convert to whitelisted object?
+ ),
+ );
+ $targeted_post_types = array_keys( $whitelisted_post_meta );
+
+ /*
+ * Wipe out any existing meta being exposed.
+ *
+ * It should already be empty unless $context == 'edit', but that may not be true in the future.
+ */
+ $prepared_post['post_meta'] = array();
+
+ if ( is_wp_error( $prepared_post ) || ! in_array( $prepared_post['type'], $targeted_post_types ) ) {
+ return $prepared_post;
+ }
+
+ $post_meta_endpoint = new WP_JSON_Meta_Posts( $GLOBALS['wp_json_server'] );
+ add_filter( 'json_check_post_edit_permission', '__return_true' ); // The API only exposes post meta to authenticated users, but we want to expose whitelisted items to everyone
+ add_filter( 'is_protected_meta', '__return_false' ); // We want to include whitelisted items, even if they're marked as protected
+ $post_meta = $post_meta_endpoint->get_all_meta( $raw_post['ID'] );
+ remove_filter( 'is_protected_meta', '__return_false' );
+ remove_filter( 'json_check_post_edit_permission', '__return_true' );
+
+ foreach( $post_meta as $meta_item ) {
+ if ( in_array( $meta_item['key'], $whitelisted_post_meta[ $prepared_post['type'] ] ) ) {
+ $prepared_post['post_meta'][] = $meta_item;
+ }
+ }
+
+ return $prepared_post;
+}
+add_filter( 'json_prepare_post', 'wcorg_json_expose_whitelisted_meta_data', 999, 3 );
+
+/**
+ * Avoid nested callback conflicts by de-registering WP_JSON_Media::add_thumbnail_data().
+ *
+ * There's a Core bug (#17817) where nested callbacks conflict with each other. If a post has a featured image,
+ * then `json_prepare_post` will be called recursively, and `wcorg_expose_whitelisted_json_api_meta_data()` will
+ * be short-circuited.
+ *
+ * Hopefully the fix for #17817 will land in 4.3, but until then we can avoid the issue by de-registering
+ * `add_thumbnail_data()`. This obviously has the downside of removing featured image data from the API responses,
+ * but we don't have a compelling use case for that data right now, and I don't see a better alternative. Ensuring
+ * that only whitelisted data is exposed is a privacy issue, and therefore trumps pretty much everything else.
+ *
+ * This is fragile, and will probably break when WP-API's `develop` branch is merged into `master`, so -- like
+ * everything else in this plugin -- it should be re-tested and updated when new versions of the API are
+ * released.
+ *
+ * See https://github.com/WP-API/WP-API/issues/1090
+ * See https://core.trac.wordpress.org/ticket/17817
+ */
+function wcorg_json_avoid_nested_callback_conflicts() {
+ /** @var $wp_json_media WP_JSON_Media */
+ global $wp_json_media;
+
+ remove_filter( 'json_prepare_post', array( $wp_json_media, 'add_thumbnail_data' ), 10, 3 );
+}
+add_action( 'wp_json_server_before_serve', 'wcorg_json_avoid_nested_callback_conflicts', 11 ); // after `json_api_default_filters()`
+
+/*
+ * WP-CLI Commands
+ */
+if ( defined( 'WP_CLI' ) && WP_CLI ) {
+ /**
+ * WordCamp.org JSON API
+ */
+ class WordCamp_JSON_API_Commands extends WP_CLI_Command {
+ /**
+ * Verify that no sensitive data is being exposed via the API.
+ *
+ * @subcommand verify-data-is-scrubbed
+ */
+ public function verify_data_is_scrubbed() {
+ $errors = false;
+ $start_timestamp = microtime( true );
+
+ // These calls are not formatted in a more compact way because we don't want to short-circuit any of them if one fails
+ if ( $this->post_meta_exposed() ) {
+ $errors = true;
+ }
+
+ WP_CLI::line();
+ WP_CLI::line( sprintf( 'Tests completed in %s seconds', number_format( microtime( true ) - $start_timestamp, 3 ) ) );
+
+ if ( $errors ) {
+ WP_CLI::error( 'Not all sensitive data has been scrubbed.' );
+ } else {
+ WP_CLI::success( 'All of the tests passed. If the tests are comprehensive and working properly, then all sensitive data has been properly scrubbed.' );
+ }
+ }
+
+ /**
+ * Check if any sensitive post meta is being exposed.
+ *
+ * If this were a proper test we'd insert the data into a test db during setup rather than relying on the
+ * existence of production data, but this is good enough for our current needs. Just make sure to double
+ * check that the meta where checking still exists, otherwise the tests could result in a false-negative.
+ *
+ * @return bool
+ */
+ protected function post_meta_exposed() {
+ $errors = false;
+
+ WP_CLI::line();
+ WP_CLI::line( 'Checking post meta.' );
+
+ // This is just a representative sample, not a complete list
+ $sensitive_post_meta = array(
+ 'http://central.wordcamp.org/wp-json/posts/3038288' => array( 'Email Address', 'Telephone', 'Mailing Address' ), // A wordcamp post on Central
+ 'http://central.wordcamp.org/wp-json/posts/2347409' => array( 'mes_email_address' ), // A Multi-Event Sponsor post on Central
+ 'http://europe.wordcamp.org/2014/wp-json/posts/216283' => array( '_wcb_speaker_email' ), // A Speaker post on a camp site
+ );
+
+ foreach ( $sensitive_post_meta as $request_url => $sensitive_meta_keys ) {
+ $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 sandbox
+ $response = json_decode( wp_remote_retrieve_body( wp_remote_get( esc_url_raw( $request_url ) ) ) );
+
+ if ( ! isset( $response->post_meta ) ) {
+ $errors = true;
+ WP_CLI::warning( "Unable to retrieve post meta from $request_url", false );
+ continue;
+ }
+
+ foreach ( $response->post_meta as $post_meta ) {
+ if ( in_array( $post_meta->key, $sensitive_meta_keys ) ) {
+ $errors = true;
+ WP_CLI::warning( "{$post_meta->key} is being exposed at $request_url" );
+ } else {
+ WP_CLI::line( "{$post_meta->key} is whitelisted." );
+ }
+ }
+ }
+
+ return $errors;
+ }
+ }
+
+ WP_CLI::add_command( 'wcorg-json-api', 'WordCamp_JSON_API_Commands' );
+}
</ins></span></pre>
</div>
</div>
</body>
</html>