Index: src/wp-includes/formatting.php
===================================================================
--- src/wp-includes/formatting.php	(revision 42623)
+++ src/wp-includes/formatting.php	(working copy)
@@ -2985,6 +2985,55 @@
 }
 
 /**
+ * Adds rel nofollow and noopener to all HTML A elements that have a target.
+ *
+ * @param string $text Content that may contain HTML A elements.
+ * @return string Converted content.
+ */
+function wp_targeted_link_rel( $text ) {
+	$text = preg_replace_callback( '|<a([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $text );
+	return $text;
+}
+
+/**
+ * Callback to add rel="nofollow noopener" string to HTML A element.
+ *
+ * Will remove already existing nofollow and noopener from the
+ * string to prevent from invalidating (X)HTML.
+ *
+ * @param array $matches Single Match
+ * @return string HTML A Element with rel nofollow and noopener if the target is set
+ */
+function wp_targeted_link_rel_callback( $matches ) {
+	$text = $matches[1];
+	$rel  = apply_filters( 'wp_targeted_link_rel', 'nofollow noopener' );
+	$rel_match = array();
+
+	// value with delimiters, spaces around = optional
+	$attr_regex  = '|rel\s*=\s*?(\\\\{0,1}["\'])(.*?)\\1|i';
+	preg_match( $attr_regex, $text, $rel_match );
+
+	if ( empty( $rel_match[0] ) ) {
+		// no delimters, try with a single value and spaces, because `rel =  va"lue` is totally fine...
+		$attr_regex  = '|rel\s*=(\s*)([^\s]*)|i';
+		preg_match( $attr_regex, $text, $rel_match );
+	}
+
+	if ( ! empty( $rel_match[0] ) ) {
+		$parts = preg_split( '|\s+|', strtolower( $rel_match[2] ) );
+		$parts = array_map( 'esc_attr', $parts );
+		$needed = explode( ' ', $rel );
+		$parts = array_unique( array_merge( $parts, $needed ) );
+		$delimiter = trim( $rel_match[1] ) ? $rel_match[1] : '"';
+		$rel = 'rel=' . $delimiter . trim( implode( ' ', $parts ) ) . $delimiter;
+		$text = str_replace( $rel_match[0], $rel, $text );
+	} else {
+		$text .= " rel=\"$rel\"";
+	}
+	return "<a$text>";
+}
+
+/**
  * Callback to add rel=nofollow string to HTML A element.
  *
  * Will remove already existing rel="nofollow" and rel='nofollow' from the
Index: src/wp-includes/kses.php
===================================================================
--- src/wp-includes/kses.php	(revision 42623)
+++ src/wp-includes/kses.php	(working copy)
@@ -1887,6 +1887,19 @@
 	add_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
 }
 
+function kses_init_phishing_filters() {
+	// Normal filtering
+	add_filter( 'title_save_pre', 'wp_targeted_link_rel', 0 );
+
+	// Comment filtering
+	add_filter( 'pre_comment_content', 'wp_targeted_link_rel', 0 );
+
+	// Post filtering
+	add_filter( 'content_save_pre', 'wp_targeted_link_rel', 0 );
+	add_filter( 'excerpt_save_pre', 'wp_targeted_link_rel', 0 );
+	add_filter( 'content_filtered_save_pre', 'wp_targeted_link_rel', 0 );
+}
+
 /**
  * Removes all Kses input form content filters.
  *
@@ -1932,6 +1945,9 @@
 	if ( ! current_user_can( 'unfiltered_html' ) ) {
 		kses_init_filters();
 	}
+
+	// Mandatory phishing filters
+	kses_init_phishing_filters();
 }
 
 /**
Index: tests/phpunit/tests/formatting/WPTargetedLinkRel.php
===================================================================
--- tests/phpunit/tests/formatting/WPTargetedLinkRel.php	(revision 0)
+++ tests/phpunit/tests/formatting/WPTargetedLinkRel.php	(working copy)
@@ -0,0 +1,67 @@
+<?php
+
+/**
+ * @group formatting
+ */
+class Tests_Targeted_Link_Rel extends WP_UnitTestCase {
+
+	public function test_add_no_follow_no_opener_to_links_with_target() {
+		$content  = '<p>Links: <a href="/" target="_blank">No rel</a></p>';
+		$expected = '<p>Links: <a href="/" target="_blank" rel="nofollow noopener">No rel</a></p>';
+		$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
+	}
+
+	public function test_target_as_first_attribute() {
+		$content  = '<p>Links: <a target="_blank" href="#">No rel</a></p>';
+		$expected = '<p>Links: <a target="_blank" href="#" rel="nofollow noopener">No rel</a></p>';
+		$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
+	}
+
+	public function test_add_no_follow_no_opener_to_existing_rel() {
+		$content  = '<p>Links: <a href="/" rel="existing values" target="_blank">Existing rel</a></p>';
+		$expected = '<p>Links: <a href="/" rel="existing values nofollow noopener" target="_blank">Existing rel</a></p>';
+		$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
+	}
+
+	public function test_no_duplicate_values_added() {
+		$content  = '<p>Links: <a href="/" rel="existing noopener values" target="_blank">Existing rel</a></p>';
+		$expected = '<p>Links: <a href="/" rel="existing noopener values nofollow" target="_blank">Existing rel</a></p>';
+		$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
+	}
+
+	public function test_rel_with_single_quote_delimiter() {
+		$content  = '<p>Links: <a href="/" rel=\'existing values\' target="_blank">Existing rel</a></p>';
+		$expected = '<p>Links: <a href="/" rel=\'existing values nofollow noopener\' target="_blank">Existing rel</a></p>';
+		$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
+	}
+
+	public function test_rel_with_no_delimiter() {
+		$content  = '<p>Links: <a href="/" rel=existing target="_blank">Existing rel</a></p>';
+		$expected = '<p>Links: <a href="/" rel="existing nofollow noopener" target="_blank">Existing rel</a></p>';
+		$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
+	}
+
+	public function test_rel_value_spaced_and_no_delimiter() {
+		$content  = '<p>Links: <a href="/" rel = existing target="_blank">Existing rel</a></p>';
+		$expected = '<p>Links: <a href="/" rel="existing nofollow noopener" target="_blank">Existing rel</a></p>';
+		$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
+	}
+
+	public function test_rel_value_spaced_and_no_delimiter_and_values_to_escape() {
+		$content  = '<p>Links: <a href="/" rel = existing"value target="_blank">Existing rel</a></p>';
+		$expected = '<p>Links: <a href="/" rel="existing&quot;value nofollow noopener" target="_blank">Existing rel</a></p>';
+		$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
+	}
+
+	public function test_escaped_quotes() {
+		$content  = '<p>Links: <a href=\"/\" rel=\"existing values\" target=\"_blank\">Existing rel</a></p>';
+		$expected = '<p>Links: <a href=\"/\" rel=\"existing values nofollow noopener\" target=\"_blank\">Existing rel</a></p>';
+		$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
+	}
+
+	public function test_ignore_links_with_no_target() {
+		$content  = '<p>Links: <a href="/" target="_blank">Change me</a> <a href="/">Do not change me</a></p>';
+		$expected = '<p>Links: <a href="/" target="_blank" rel="nofollow noopener">Change me</a> <a href="/">Do not change me</a></p>';
+		$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
+	}
+}

Property changes on: tests/phpunit/tests/formatting/WPTargetedLinkRel.php
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: tests/phpunit/tests/rest-api/rest-attachments-controller.php
===================================================================
--- tests/phpunit/tests/rest-api/rest-attachments-controller.php	(revision 42623)
+++ tests/phpunit/tests/rest-api/rest-attachments-controller.php	(working copy)
@@ -1015,12 +1015,12 @@
 						'rendered' => '<a href="#">link</a>',
 					),
 					'description' => array(
-						'raw'      => '<a href="#" target="_blank">link</a>',
-						'rendered' => '<p><a href="#" target="_blank">link</a></p>',
+						'raw'      => '<a href="#" target="_blank" rel="nofollow noopener">link</a>',
+						'rendered' => '<p><a href="#" target="_blank" rel="nofollow noopener">link</a></p>',
 					),
 					'caption'     => array(
-						'raw'      => '<a href="#" target="_blank">link</a>',
-						'rendered' => '<p><a href="#" target="_blank">link</a></p>',
+						'raw'      => '<a href="#" target="_blank" rel="nofollow noopener">link</a>',
+						'rendered' => '<p><a href="#" target="_blank" rel="nofollow noopener">link</a></p>',
 					),
 				),
 			),
Index: tests/phpunit/tests/rest-api/rest-posts-controller.php
===================================================================
--- tests/phpunit/tests/rest-api/rest-posts-controller.php	(revision 42623)
+++ tests/phpunit/tests/rest-api/rest-posts-controller.php	(working copy)
@@ -3185,12 +3185,12 @@
 						'rendered' => '<a href="#">link</a>',
 					),
 					'content' => array(
-						'raw'      => '<a href="#" target="_blank">link</a>',
-						'rendered' => '<p><a href="#" target="_blank">link</a></p>',
+						'raw'      => '<a href="#" target="_blank" rel="nofollow noopener">link</a>',
+						'rendered' => '<p><a href="#" target="_blank" rel="nofollow noopener">link</a></p>',
 					),
 					'excerpt' => array(
-						'raw'      => '<a href="#" target="_blank">link</a>',
-						'rendered' => '<p><a href="#" target="_blank">link</a></p>',
+						'raw'      => '<a href="#" target="_blank" rel="nofollow noopener">link</a>',
+						'rendered' => '<p><a href="#" target="_blank" rel="nofollow noopener">link</a></p>',
 					),
 				),
 			),
