Problem/Motivation

Using target _blank is not safe. See more info here: https://web.dev/external-anchors-use-rel-noopener

Proposed resolution

Adding rel noopener when adding a target _blank.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

guardiola86 created an issue. See original summary.

guardiola86’s picture

anoopsingh92 made their first commit to this issue’s fork.

anoopsingh92’s picture

Assigned: Unassigned » anoopsingh92
Status: Active » Needs review

Hi @guardiola86, I am reviewing the #2 patch. Thank you

anoopsingh92’s picture

Assigned: anoopsingh92 » Unassigned

Hi @guardiola86, Getting this when I apply the #2 patch.

Lenovo@LAPTOP-PDE747K8 MINGW64 /c/xampp/htdocs/drupal-9/web/modules/contrib/link_attributes-3299750 (3299750-using-target-blank)
$ git apply -v using-target-blank-is-not-safe-3299750-2.patch
Checking patch src/Plugin/Field/FieldWidget/LinkWithAttributesWidget.php...
Applied patch src/Plugin/Field/FieldWidget/LinkWithAttributesWidget.php cleanly.
larowlan’s picture

+++ b/src/Plugin/Field/FieldWidget/LinkWithAttributesWidget.php
@@ -151,6 +151,10 @@ class LinkWithAttributesWidget extends LinkWidget implements ContainerFactoryPlu
+      if ($values[$delta]['options']['attributes']['target'] == '_blank') {
+        $values[$delta]['options']['attributes']['rel'] = 'noopener';

We should check there's not already a value set in case someone wants to override it manually

Anybody’s picture

Interesting learning! I didn't know this yet. Cool!

Agree with #6 and this should be done as MR, please.

@guardiola86 @anoopsingh92 would you do that?

anoopsingh92’s picture

I have done @Anybody, Please review the Merge request !6
Thanks

Anybody’s picture

Status: Needs review » Needs work

@anoopsingh92 thank you! Still needs the additional check from #6.

Grevil made their first commit to this issue’s fork.

Grevil’s picture

Status: Needs work » Needs review

Added an empty check, please review.

Anybody’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks @Grevil, looks good! I think to be 100% safe we should have a simple test here for both values to be present, if target="_blank" is selected.

If that goes green, RTBC from my side. Code LGTM already!

Grevil’s picture

@Anybody, I agree! Furthermore, we should allow testing inside issues, as the first patch applied here failed.

Grevil’s picture

Status: Needs work » Needs review

Done, please review, tests should be green now.

Anybody’s picture

FileSize
3.68 KB

Testing seems to work with patch files here manually, not with MR's. Let's try! :)

Anybody’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

Code LGTM and tests pass, thank you @Grevil! RTBC! Let's see what @larowlan says.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +DrupalSouth

This is looking good, can we add an extra test case for when target is blank, but a value of rel is already set?

Thanks!

Anybody’s picture

@larowlan totally makes sense! @Grevil could you add that one please? Shouldn't be too hard anymore, I hope.

Grevil’s picture

Status: Needs work » Needs review

Done, please review!

Anybody’s picture

Status: Needs review » Reviewed & tested by the community

Nice work @Grevil, this proves that the value is "nofollow" and even with target="_blank" is not "noopener"!
The comment is also important here. Thanks!

Anybody’s picture

Priority: Major » Minor

FYI: https://developer.chrome.com/docs/lighthouse/best-practices/external-anc...

As of Chromium version 88, anchors with target="_blank" automatically get noopener behavior by default. Explicit specification of rel="noopener" helps protect users of legacy browsers including Edge Legacy and Internet Explorer.

So changing this to minor. @larowlan should decide if we should do this anyway or keep it as it was before and let the browsers do their work.

Perhaps @guardiola86 could check this for the major browsers: Firefox, Safari, Edge?

Anybody’s picture

@larowlan as of #22 I'd vote to set this won't fix and leave it to the browsers.
If you'd like to add this anyway, it's RTBC'd and fine, I think.

larowlan’s picture

Status: Reviewed & tested by the community » Needs review

Yes, if we can confirm Firefox is the same, I'd prefer to close this as is and avoid the extra complexity

larowlan’s picture

Version: 8.x-1.x-dev » 2.x-dev
Grevil’s picture

Status: Needs review » Reviewed & tested by the community

Yes, I can confirm, that Firefox also has this feature implemented in their 79 release! See https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Releases/79#html

Setting target="_blank" on <a> and <area> elements implicitly provides the same behavior as also setting rel="noopener" [...]

I guess I will set this "RTBC" to confirm that this can be closed?

Anybody’s picture

Status: Reviewed & tested by the community » Closed (won't fix)

Thanks @Grevil, so I'll set it won't fix as of #24

larowlan’s picture

Thanks!