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

Follow a Ruby 3.3 warning for Security/Open #12572

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12572](https://github.com/rubocop/rubocop/pull/12572): Follow a Ruby 3.3 warning for `Security/Open` when `open` with a literal string starting with a pipe. ([@koic][])
4 changes: 2 additions & 2 deletions lib/rubocop/cop/security/open.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Security
# # bad
# open(something)
# open("| #{something}")
# open("| foo")
# URI.open(something)
#
# # good
Expand All @@ -32,15 +33,14 @@ module Security
#
# # good (literal strings)
# open("foo.text")
# open("| foo")
# URI.open("http://example.com")
class Open < Base
MSG = 'The use of `%<receiver>sopen` is a serious security risk.'
RESTRICT_ON_SEND = %i[open].freeze

# @!method open?(node)
def_node_matcher :open?, <<~PATTERN
(send ${nil? (const {nil? cbase} :URI)} :open $!str ...)
(send ${nil? (const {nil? cbase} :URI)} :open $_ ...)
PATTERN

def on_send(node)
Expand Down
11 changes: 7 additions & 4 deletions spec/rubocop/cop/security/open_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
RUBY
end

it 'registers an offense for open with a literal string starting with a pipe' do
expect_offense(<<~RUBY)
open('| foo')
^^^^ The use of `Kernel#open` is a serious security risk.
RUBY
end

it 'registers an offense for open with a block' do
expect_offense(<<~'RUBY')
open("#{foo}.txt") do |f|
Expand Down Expand Up @@ -88,8 +95,4 @@
it 'accepts open with a string that interpolates a literal' do
expect_no_offenses('open "foo#{2}.txt"')
end

it 'accepts open with a literal string starting with a pipe' do
expect_no_offenses('open "| foo"')
end
end