Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Rails/LinkToBlank cop #6580

Merged
merged 4 commits into from Dec 22, 2018
Merged

Conversation

Intrepidd
Copy link

This cop checks for calls to link_to that contain a target: '_blank' but no rel: 'noopener'. This can be a security risk as the loaded page will have control over the previous page and could change its location for phishing purposes.

Demo resource for reference : https://mathiasbynens.github.io/rel-noopener/

def on_send(node)
return unless node.method?(:link_to)

option_nodes = [node.children.last, node.children[3]].compact
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the [3] here, any better idea ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably go with something like:

node.arguments.each_child_node(:hash)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip ! I managed to make it work with node.each_child_node(:hash)


option_nodes = [node.children.last, node.children[3]].compact

option_nodes.map(&:children).each do |options|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can go with #each_pair here, instead of map(&:children).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately at this point option_nodes is an Enumerator of Hash nodes

@Intrepidd
Copy link
Author

Hi @Drenmi ! Can I do anything to help get this PR merged ? Thanks a lot !

Copy link
Collaborator

@Drenmi Drenmi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

Ping @bbatsov.

CHANGELOG.md Outdated
@@ -1,6 +1,7 @@
# Change log

## master (unreleased)
* New cop `Rails/LinkToBlank` checks for `link_to` calls with `target: '_blank'` and no `rel: 'noopener'`. ([@Intrepidd][])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to put this under "New features".

module Rails
# This cop checks for calls to `link_to` that contain a
# `target: '_blank'` but no `rel: 'noopener'`. This can be a security
# risk as the loaded page will have control over the previous page
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line seems misaligned.

@Intrepidd
Copy link
Author

Thanks @bbatsov ! I applied your comments an rebased

@bbatsov bbatsov merged commit be2fcff into rubocop:master Dec 22, 2018
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 22, 2018

Thanks!

@koic
Copy link
Member

koic commented Dec 25, 2018

FYI, although it has not been merged yet, there is a PR opened in rails/rails that adds rel="noopener" default to link_to when particular conditions are met 😉
rails/rails#28035

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants