From: Dylan William Hardison Date: Tue, 25 Jun 2019 18:13:40 +0000 (-0700) Subject: Bug 1482447 - Add nofollow to all external links X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e881714b22ed60d832ef9452dfb860edd196f775;p=thirdparty%2Fbugzilla.git Bug 1482447 - Add nofollow to all external links --- diff --git a/Bugzilla/Markdown.pm b/Bugzilla/Markdown.pm index 0c5287144..fd0cf80b4 100644 --- a/Bugzilla/Markdown.pm +++ b/Bugzilla/Markdown.pm @@ -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; diff --git a/t/markdown.t b/t/markdown.t index 35d7da4f4..fbd737f1c 100644 --- a/t/markdown.t +++ b/t/markdown.t @@ -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'), - "

http://bmo-web.vm

\n", + "

http://bmo-web.vm

\n", 'Autolink extension' );