Skip to content

Commit a955422

Browse files
authored
Merge pull request #976 from koic/fix_a_false_positive_for_rails_output_safety
[Fix #501] Fix a false positive for `Rails/OutputSafety`
2 parents 1d59a6d + 97329f6 commit a955422

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#501](https://github.com/rubocop/rubocop-rails/issues/501): Fix a false positive for `Rails/OutputSafety` when using `html_safe` for `I18n` methods. ([@koic][])

lib/rubocop/cop/rails/output_safety.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ class OutputSafety < Base
6666
MSG = 'Tagging a string as html safe may be a security risk.'
6767
RESTRICT_ON_SEND = %i[html_safe raw safe_concat].freeze
6868

69+
def_node_search :i18n_method?, <<~PATTERN
70+
(send {nil? (const {nil? cbase} :I18n)} {:t :translate :l :localize} ...)
71+
PATTERN
72+
6973
def on_send(node)
70-
return if non_interpolated_string?(node)
74+
return if non_interpolated_string?(node) || i18n_method?(node)
7175

7276
return unless looks_like_rails_html_safe?(node) ||
7377
looks_like_rails_raw?(node) ||

spec/rubocop/cop/rails/output_safety_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,57 @@
130130
^^^ Tagging a string as html safe may be a security risk.
131131
RUBY
132132
end
133+
134+
it 'does not register an offense when using `html_safe` for `I18n.t` method' do
135+
expect_no_offenses(<<~RUBY)
136+
I18n.t('foo.bar.baz', scope: [:x, :y, :z]).html_safe
137+
::I18n.t('foo.bar.baz', scope: [:x, :y, :z]).html_safe
138+
RUBY
139+
end
140+
141+
it 'does not register an offense when using `html_safe` for `I18n.translate` method' do
142+
expect_no_offenses(<<~RUBY)
143+
I18n.translate('foo.bar.baz', scope: [:x, :y, :z]).html_safe
144+
::I18n.translate('foo.bar.baz', scope: [:x, :y, :z]).html_safe
145+
RUBY
146+
end
147+
148+
it 'does not register an offense when using `html_safe` for `t` method' do
149+
expect_no_offenses(<<~RUBY)
150+
t('foo.bar.baz').html_safe
151+
RUBY
152+
end
153+
154+
it 'does not register an offense when using `html_safe` for `translate` method' do
155+
expect_no_offenses(<<~RUBY)
156+
translate('foo.bar.baz').html_safe
157+
RUBY
158+
end
159+
160+
it 'does not register an offense when using `html_safe` for `I18n.l` method' do
161+
expect_no_offenses(<<~RUBY)
162+
I18n.l(Time.now, locale: :de).html_safe
163+
::I18n.l(Time.now, locale: :de).html_safe
164+
RUBY
165+
end
166+
167+
it 'does not register an offense when using `html_safe` for `I18n.localize` method' do
168+
expect_no_offenses(<<~RUBY)
169+
I18n.localize(Time.now, locale: :de).html_safe
170+
::I18n.localize(Time.now, locale: :de).html_safe
171+
RUBY
172+
end
173+
174+
it 'does not register an offense when using `html_safe` for `l` method' do
175+
expect_no_offenses(<<~RUBY)
176+
l(Time.now).html_safe
177+
RUBY
178+
end
179+
180+
it 'does not register an offense when using `html_safe` for `localize` method' do
181+
expect_no_offenses(<<~RUBY)
182+
localize(Time.now).html_safe
183+
RUBY
184+
end
133185
end
134186
end

0 commit comments

Comments
 (0)