diff --git a/api.wordpress.org/public_html/dotorg/slack/announce.php b/api.wordpress.org/public_html/dotorg/slack/announce.php index 04a7c8918b..b9bc0c5f02 100644 --- a/api.wordpress.org/public_html/dotorg/slack/announce.php +++ b/api.wordpress.org/public_html/dotorg/slack/announce.php @@ -1,38 +1,72 @@ get_var( $wpdb->prepare( - "SELECT user_id FROM slack_users WHERE slack_id = %s", - $slack_id - ) ); + $wp_user_id = $wpdb->get_var( + $wpdb->prepare( + 'SELECT user_id FROM slack_users WHERE slack_id = %s', + $slack_id + ) + ); + + if ( ! $wp_user_id ) { + return ''; + } + + $email = $wpdb->get_var( + $wpdb->prepare( + "SELECT user_email FROM $wpdb->users WHERE ID = %d", + $wp_user_id + ) + ); - $email = $wpdb->get_var( $wpdb->prepare( - "SELECT user_email FROM $wpdb->users WHERE ID = %d", - $wp_user_id - ) ); + if ( ! $email ) { + return ''; + } $hash = hash( 'sha256', strtolower( trim( $email ) ) ); - return sprintf( 'https://secure.gravatar.com/avatar/%s?s=96d=mm&r=G&%s', $hash, time() ); + return sprintf( 'https://secure.gravatar.com/avatar/%s?s=96&d=mm&r=G&%s', $hash, time() ); +} + +// Slack sends the token as POST data; anything else is not a webhook request. +if ( ! isset( $_POST['token'] ) || ! is_string( $_POST['token'] ) || '' === $_POST['token'] ) { + return; } $i = 0; // WEBHOOK_TOKEN_1, WEBHOOK_TOKEN_2, etc. -while ( defined( __NAMESPACE__ . '\\WEBHOOK_TOKEN_' . ++$i ) ) { +while ( defined( __NAMESPACE__ . '\\WEBHOOK_TOKEN_' . ( ++$i ) ) ) { if ( hash_equals( constant( __NAMESPACE__ . '\\WEBHOOK_TOKEN_' . $i ), $_POST['token'] ) ) { run( $_POST ); + break; } } - -} - diff --git a/api.wordpress.org/public_html/dotorg/slack/committers.php b/api.wordpress.org/public_html/dotorg/slack/committers.php index e89f6a820b..a4e11150f9 100644 --- a/api.wordpress.org/public_html/dotorg/slack/committers.php +++ b/api.wordpress.org/public_html/dotorg/slack/committers.php @@ -1,19 +1,39 @@ 'wordpressdotorg', - 'link_names' => 1, - 'text' => sprintf( '@%s: Use the `/committers` command.', $_POST['user_name'] ), -) ); +// The Slack user name of whoever triggered the webhook, echoed back in the JSON response below. +$user_name = (string) filter_var( $_POST['user_name'] ?? '', FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW ); + +// phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode -- No WP loaded. +echo json_encode( + array( + 'username' => 'wordpressdotorg', + 'link_names' => 1, + 'text' => sprintf( '@%s: Use the `/committers` command.', $user_name ), + ) +); exit;