-
Notifications
You must be signed in to change notification settings - Fork 166
Fix HTML escaping for partner email sharing #11491
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RuboCop | ||
| module Cop | ||
| module IdentityIdp | ||
| # This linter checks to ensure that strings which include HTML are rendered using Rails `t` | ||
| # view helper, rather than through the I18n class. Only the Rails view helper will mark the | ||
| # content as HTML-safe. | ||
| # | ||
| # @see https://guides.rubyonrails.org/i18n.html#using-safe-html-translations | ||
| # | ||
| # @example | ||
| # # bad | ||
| # I18n.t('errors.message_html') | ||
| # | ||
| # # good | ||
| # t('errors.message_html') | ||
| # | ||
| class I18nHelperHtmlLinter < RuboCop::Cop::Base | ||
| MSG = 'Use the Rails `t` view helper for HTML-safe strings' | ||
|
|
||
| RESTRICT_ON_SEND = [:t].freeze | ||
|
|
||
| def_node_matcher :i18n_class_send?, <<~PATTERN | ||
| (send (const nil? :I18n) :t $...) | ||
| PATTERN | ||
|
|
||
| def on_send(node) | ||
| return if !i18n_class_send?(node) || !i18n_key(node)&.end_with?('_html') | ||
| add_offense(node) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def i18n_key(node) | ||
| first_argument = node.arguments.first | ||
| return if first_argument.nil? | ||
| return if !first_argument.respond_to?(:value) | ||
| first_argument.value.to_s | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| require 'rubocop' | ||
| require 'rubocop/rspec/cop_helper' | ||
| require 'rubocop/rspec/expect_offense' | ||
|
|
||
| require_relative '../../../lib/linters/i18n_helper_html_linter' | ||
|
|
||
| RSpec.describe RuboCop::Cop::IdentityIdp::I18nHelperHtmlLinter do | ||
| include CopHelper | ||
| include RuboCop::RSpec::ExpectOffense | ||
|
|
||
| let(:config) { RuboCop::Config.new } | ||
| let(:cop) { RuboCop::Cop::IdentityIdp::I18nHelperHtmlLinter.new(config) } | ||
|
|
||
| it 'registers offense when calling `t` from i18n class with key suffixed by "_html"' do | ||
| expect_offense(<<~RUBY) | ||
| I18n.t('errors.message_html') | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ IdentityIdp/I18nHelperHtmlLinter: Use the Rails `t` view helper for HTML-safe strings | ||
| RUBY | ||
| end | ||
|
|
||
| it 'registers offense when calling `t` from i18n class with symbol key suffixed by "_html"' do | ||
| expect_offense(<<~RUBY) | ||
| I18n.t(:message_html) | ||
| ^^^^^^^^^^^^^^^^^^^^^ IdentityIdp/I18nHelperHtmlLinter: Use the Rails `t` view helper for HTML-safe strings | ||
| RUBY | ||
| end | ||
|
|
||
| it 'gracefully handles `I18n.t` without arguments' do | ||
| expect_no_offenses(<<~RUBY) | ||
| I18n.t | ||
| RUBY | ||
| end | ||
|
|
||
| it 'gracefully handles `I18n.t` with variable key' do | ||
| expect_no_offenses(<<~RUBY) | ||
| I18n.t(key) | ||
| RUBY | ||
| end | ||
|
|
||
| it 'registers no offense when calling `t` from i18n class with key not suffixed by "_html"' do | ||
| expect_no_offenses(<<~RUBY) | ||
| I18n.t('errors.message') | ||
| RUBY | ||
| end | ||
|
|
||
| it 'registers no offense when calling `t` from Rails view helper' do | ||
| expect_no_offenses(<<~RUBY) | ||
| t('errors.message_html') | ||
| RUBY | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.