Add rel=nofollow parameter to links in comments

Spamming a Gerrit site is a real issue:

https://groups.google.com/d/msg/repo-discuss/Q9x23wnOQXg/aAtXSGqNAwAJ

Google recommends adding rel="nofollow"[1] to links on comments:

"This can discourage spammers from targeting your site, and will help
keep your site from inadvertently passing PageRank to bad neighborhoods
on the web".

[1] https://support.google.com/webmasters/answer/96569?hl=en

Cherry-pick from upstream:
   https://gerrit-review.googlesource.com/#/c/80320/

Change-Id: I17c2c6f009d31db1c91b39d37227edebf923373a
This commit is contained in:
Khai Do 2016-07-20 14:33:19 -07:00
parent cb9800e641
commit b50155fc80
3 changed files with 29 additions and 15 deletions

View File

@ -138,7 +138,7 @@ public abstract class SafeHtml
"(?:[(]" + part + "*" + "[)])*" +
part + "*" +
")",
"<a href=\"$1\" target=\"_blank\">$1</a>");
"<a href=\"$1\" target=\"_blank\" rel=\"nofollow\">$1</a>");
}
/**

View File

@ -25,7 +25,8 @@ public class SafeHtml_LinkifyTest {
final SafeHtml o = html("A http://go.here/ B");
final SafeHtml n = o.linkify();
assertNotSame(o, n);
assertEquals("A <a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a> B", n.asString());
assertEquals("A <a href=\"http://go.here/\" target=\"_blank\" rel=\"nofollow\""
+ ">http://go.here/</a> B", n.asString());
}
@Test
@ -33,7 +34,8 @@ public class SafeHtml_LinkifyTest {
final SafeHtml o = html("A https://go.here/ B");
final SafeHtml n = o.linkify();
assertNotSame(o, n);
assertEquals("A <a href=\"https://go.here/\" target=\"_blank\">https://go.here/</a> B", n.asString());
assertEquals("A <a href=\"https://go.here/\" target=\"_blank\" rel=\"nofollow\""
+ ">https://go.here/</a> B", n.asString());
}
@Test
@ -41,7 +43,8 @@ public class SafeHtml_LinkifyTest {
final SafeHtml o = html("A (http://go.here/) B");
final SafeHtml n = o.linkify();
assertNotSame(o, n);
assertEquals("A (<a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>) B", n.asString());
assertEquals("A (<a href=\"http://go.here/\" target=\"_blank\" rel=\"nofollow\""
+ ">http://go.here/</a>) B", n.asString());
}
@Test
@ -49,7 +52,8 @@ public class SafeHtml_LinkifyTest {
final SafeHtml o = html("A http://go.here/#m() B");
final SafeHtml n = o.linkify();
assertNotSame(o, n);
assertEquals("A <a href=\"http://go.here/#m()\" target=\"_blank\">http://go.here/#m()</a> B", n.asString());
assertEquals("A <a href=\"http://go.here/#m()\" target=\"_blank\" rel=\"nofollow\""
+ ">http://go.here/#m()</a> B", n.asString());
}
@Test
@ -57,7 +61,8 @@ public class SafeHtml_LinkifyTest {
final SafeHtml o = html("A <http://go.here/> B");
final SafeHtml n = o.linkify();
assertNotSame(o, n);
assertEquals("A &lt;<a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>&gt; B", n.asString());
assertEquals("A &lt;<a href=\"http://go.here/\" target=\"_blank\" rel=\"nofollow\""
+ ">http://go.here/</a>&gt; B", n.asString());
}
@Test
@ -65,7 +70,8 @@ public class SafeHtml_LinkifyTest {
final SafeHtml o = html("A http://go.here/foo B");
final SafeHtml n = o.linkify();
assertNotSame(o, n);
assertEquals("A <a href=\"http://go.here/foo\" target=\"_blank\">http://go.here/foo</a> B", n.asString());
assertEquals("A <a href=\"http://go.here/foo\" target=\"_blank\" rel=\"nofollow\""
+ ">http://go.here/foo</a> B", n.asString());
}
@Test
@ -73,7 +79,8 @@ public class SafeHtml_LinkifyTest {
final SafeHtml o = html("A http://go.here/. B");
final SafeHtml n = o.linkify();
assertNotSame(o, n);
assertEquals("A <a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>. B", n.asString());
assertEquals("A <a href=\"http://go.here/\" target=\"_blank\" rel=\"nofollow\""
+ ">http://go.here/</a>. B", n.asString());
}
@Test
@ -81,7 +88,8 @@ public class SafeHtml_LinkifyTest {
final SafeHtml o = html("A http://go.here/, B");
final SafeHtml n = o.linkify();
assertNotSame(o, n);
assertEquals("A <a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>, B", n.asString());
assertEquals("A <a href=\"http://go.here/\" target=\"_blank\" rel=\"nofollow\""
+ ">http://go.here/</a>, B", n.asString());
}
@Test
@ -89,7 +97,8 @@ public class SafeHtml_LinkifyTest {
final SafeHtml o = html("A http://go.here/.. B");
final SafeHtml n = o.linkify();
assertNotSame(o, n);
assertEquals("A <a href=\"http://go.here/.\" target=\"_blank\">http://go.here/.</a>. B", n.asString());
assertEquals("A <a href=\"http://go.here/.\" target=\"_blank\" rel=\"nofollow\""
+ ">http://go.here/.</a>. B", n.asString());
}
private static SafeHtml html(String text) {

View File

@ -65,7 +65,8 @@ public class SafeHtml_WikifyTest {
final SafeHtml o = html("A http://go.here/ B");
final SafeHtml n = o.wikify();
assertNotSame(o, n);
assertEquals("<p>A <a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a> B</p>", n.asString());
assertEquals("<p>A <a href=\"http://go.here/\" target=\"_blank\" rel=\"nofollow\""
+ ">http://go.here/</a> B</p>", n.asString());
}
@Test
@ -73,7 +74,8 @@ public class SafeHtml_WikifyTest {
final SafeHtml o = html("A https://go.here/ B");
final SafeHtml n = o.wikify();
assertNotSame(o, n);
assertEquals("<p>A <a href=\"https://go.here/\" target=\"_blank\">https://go.here/</a> B</p>", n.asString());
assertEquals("<p>A <a href=\"https://go.here/\" target=\"_blank\" rel=\"nofollow\""
+ ">https://go.here/</a> B</p>", n.asString());
}
@Test
@ -81,7 +83,8 @@ public class SafeHtml_WikifyTest {
final SafeHtml o = html("A (http://go.here/) B");
final SafeHtml n = o.wikify();
assertNotSame(o, n);
assertEquals("<p>A (<a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>) B</p>", n.asString());
assertEquals("<p>A (<a href=\"http://go.here/\" target=\"_blank\" rel=\"nofollow\""
+ ">http://go.here/</a>) B</p>", n.asString());
}
@Test
@ -89,7 +92,8 @@ public class SafeHtml_WikifyTest {
final SafeHtml o = html("A http://go.here/#m() B");
final SafeHtml n = o.wikify();
assertNotSame(o, n);
assertEquals("<p>A <a href=\"http://go.here/#m()\" target=\"_blank\">http://go.here/#m()</a> B</p>", n.asString());
assertEquals("<p>A <a href=\"http://go.here/#m()\" target=\"_blank\" rel=\"nofollow\""
+ ">http://go.here/#m()</a> B</p>", n.asString());
}
@Test
@ -97,7 +101,8 @@ public class SafeHtml_WikifyTest {
final SafeHtml o = html("A <http://go.here/> B");
final SafeHtml n = o.wikify();
assertNotSame(o, n);
assertEquals("<p>A &lt;<a href=\"http://go.here/\" target=\"_blank\">http://go.here/</a>&gt; B</p>", n.asString());
assertEquals("<p>A &lt;<a href=\"http://go.here/\" target=\"_blank\" rel=\"nofollow\""
+ ">http://go.here/</a>&gt; B</p>", n.asString());
}
private static SafeHtml html(String text) {