Skip to content

Commit

Permalink
[Fix rubocop#4836] Make Rails/OutputSafety aware of safe navigation…
Browse files Browse the repository at this point in the history
… operator

The cop would not register an offense when `#html_safe` and
`#safe_concat` were sent using the safe navigation operator. This change
fixes that.
  • Loading branch information
Drenmi authored and bbatsov committed Oct 5, 2017
1 parent 4cb097e commit 2677293
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* [#4823](https://github.com/bbatsov/rubocop/issues/4823): Make `Lint/UnusedMethodArgument` and `Lint/UnusedBlockArgument` aware of overriding assignments. ([@akhramov][])
* [#4830](https://github.com/bbatsov/rubocop/issues/4830): Prevent `Lint/BooleanSymbol` from truncating symbol's value in the message when offense is located in the new syntax hash. ([@akhramov][])
* [#4747](https://github.com/bbatsov/rubocop/issues/4747): Fix `Rails/HasManyOrHasOneDependent` cop incorrectly flags `with_options` blocks. ([@koic][])
* [#4836](https://github.com/bbatsov/rubocop/issues/4836): Make `Rails/OutputSafety` aware of safe navigation operator. ([@drenmi][])

### Changes

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails/output_safety.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def on_send(node)

add_offense(node, location: :selector)
end
alias on_csend on_send

private

Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/rails/output_safety_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@
^^^^^^^^^^^ Tagging a string as html safe may be a security risk.
RUBY
end

it 'registers an offense when wrapped inside `#safe_join`' do
expect_offense(<<-RUBY.strip_indent)
safe_join([i18n_text.safe_concat(i18n_text)])
^^^^^^^^^^^ Tagging a string as html safe may be a security risk.
RUBY
end

context 'when using safe navigation operator', :ruby23 do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
foo&.safe_concat('bar')
^^^^^^^^^^^ Tagging a string as html safe may be a security risk.
RUBY
end
end
end

context 'when using `#html_safe`' do
Expand Down Expand Up @@ -56,6 +66,15 @@
^^^^^^^^^ Tagging a string as html safe may be a security risk.
RUBY
end

context 'when using safe navigation operator', :ruby23 do
it 'registers an offense for variable receiver and no argument' do
expect_offense(<<-RUBY.strip_indent)
foo&.html_safe
^^^^^^^^^ Tagging a string as html safe may be a security risk.
RUBY
end
end
end

context 'when using `#raw`' do
Expand Down

0 comments on commit 2677293

Please sign in to comment.