]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 1482447 - Add nofollow to all external links
authorDylan William Hardison <dylan@hardison.net>
Tue, 25 Jun 2019 18:13:40 +0000 (11:13 -0700)
committerdklawren <dklawren@users.noreply.github.com>
Tue, 25 Jun 2019 18:13:40 +0000 (13:13 -0500)
Bugzilla/Markdown.pm
t/markdown.t

index 0c528714407e3d312c86e7490bf6af1583c9c73a..fd0cf80b4e9cc23298769c5233d3d961017512ea 100644 (file)
@@ -79,6 +79,8 @@ sub render_html {
   my $dom = Mojo::DOM->new($html);
 
   $dom->find(join(', ', @bad_tags))->map('remove');
+
+  $dom->find("a[href]")->grep(\&_is_external_link)->map(attr => rel => 'nofollow');
   $dom->find(join ', ', @valid_text_parent_tags)->map(sub {
     my $node = shift;
     $node->descendant_nodes->map(sub {
@@ -98,5 +100,12 @@ sub render_html {
 
 }
 
+sub _is_external_link {
+  # the urlbase, without the trailing /
+  state $urlbase = substr(Bugzilla->localconfig->urlbase, 0, -1);
+
+  return index($_->attr('href'), $urlbase) != 0;
+}
+
 
 1;
index 35d7da4f458e9708b81e2dcb9838aabc57e1e6be..fbd737f1c9d306f9bc1a7d8d475e2b4eab5e803f 100644 (file)
@@ -10,6 +10,7 @@ use warnings;
 use lib qw( . lib local/lib/perl5 );
 
 use Bugzilla::Test::MockDB;
+use Bugzilla::Test::MockLocalconfig urlbase => 'http://bmo-web.vm/';
 use Bugzilla::Test::MockParams (password_complexity => 'no_constraints');
 use Mojo::DOM;
 use Bugzilla;
@@ -34,7 +35,7 @@ is(
 
 is(
   $parser->render_html('http://bmo-web.vm'),
-  "<p><a href=\"http://bmo-web.vm\">http://bmo-web.vm</a></p>\n",
+  "<p><a href=\"http://bmo-web.vm\" rel=\"nofollow\">http://bmo-web.vm</a></p>\n",
   'Autolink extension'
 );