Skip to content

Commit 259a467

Browse files
authored
Merge pull request #1555 from koic/fix_false_positive_for_rails_output_safety
[Fix #1553] Fix false positives for `Rails/OutputSafety`
2 parents 2b228a8 + 92fcb33 commit 259a467

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1553](https://github.com/rubocop/rubocop-rails/issues/1553): Fix false positives for `Rails/OutputSafety` when using non-interpolated multiline heredoc. ([@koic][])

lib/rubocop/cop/rails/output_safety.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ def on_send(node)
8484
private
8585

8686
def non_interpolated_string?(node)
87-
node.receiver&.str_type? && !node.receiver.dstr_type?
87+
return false unless (receiver = node.receiver)
88+
89+
receiver.str_type? || (receiver.dstr_type? && receiver.children.all?(&:str_type?))
8890
end
8991

9092
def looks_like_rails_html_safe?(node)

spec/rubocop/cop/rails/output_safety_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,42 @@
4040
RUBY
4141
end
4242

43+
it 'does not register an offense for static single line heredoc receiver' do
44+
expect_no_offenses(<<~RUBY)
45+
<<~HTML.html_safe
46+
foo
47+
HTML
48+
RUBY
49+
end
50+
51+
it 'registers an offense for dynamic single line heredoc receiver' do
52+
expect_offense(<<~'RUBY')
53+
<<~HTML.html_safe
54+
^^^^^^^^^ Tagging a string as html safe may be a security risk.
55+
#{foo}
56+
HTML
57+
RUBY
58+
end
59+
60+
it 'does not register an offense for static multiline heredoc receiver' do
61+
expect_no_offenses(<<~RUBY)
62+
<<~HTML.html_safe
63+
foo
64+
bar
65+
HTML
66+
RUBY
67+
end
68+
69+
it 'registers an offense for dynamic multiline heredoc receiver' do
70+
expect_offense(<<~'RUBY')
71+
<<~HTML.html_safe
72+
^^^^^^^^^ Tagging a string as html safe may be a security risk.
73+
foo
74+
#{bar}
75+
HTML
76+
RUBY
77+
end
78+
4379
it 'registers an offense for variable receiver' do
4480
expect_offense(<<~RUBY)
4581
foo.html_safe

0 commit comments

Comments
 (0)