brew/Library/Homebrew/test/rubocops/io_read_spec.rb

75 lines
2.3 KiB
Ruby
Raw Normal View History

2021-04-14 16:08:37 +01:00
# frozen_string_literal: true
require "rubocops/io_read"
RSpec.describe RuboCop::Cop::Homebrew::IORead do
2021-04-14 16:08:37 +01:00
subject(:cop) { described_class.new }
it "reports an offense when `IO.read` is used with a pipe character" do
expect_offense(<<~RUBY)
IO.read("|echo test")
^^^^^^^^^^^^^^^^^^^^^ Homebrew/IORead: The use of `IO.read` is a security risk.
2021-04-14 16:08:37 +01:00
RUBY
end
it "does not report an offense when `IO.read` is used without a pipe character" do
expect_no_offenses(<<~RUBY)
IO.read("file.txt")
RUBY
end
it "reports an offense when `IO.read` is used with untrustworthy input" do
expect_offense(<<~RUBY)
input = "input value from an unknown source"
IO.read(input)
^^^^^^^^^^^^^^ Homebrew/IORead: The use of `IO.read` is a security risk.
2021-04-14 16:08:37 +01:00
RUBY
end
it "reports an offense when `IO.read` is used with a dynamic string starting with a pipe character" do
expect_offense(<<~'RUBY')
input = "test"
IO.read("|echo #{input}")
^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/IORead: The use of `IO.read` is a security risk.
2021-04-14 16:08:37 +01:00
RUBY
end
it "reports an offense when `IO.read` is used with a dynamic string at the start" do
expect_offense(<<~'RUBY')
input = "|echo test"
IO.read("#{input}.txt")
^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/IORead: The use of `IO.read` is a security risk.
2021-04-14 16:08:37 +01:00
RUBY
end
it "does not report an offense when `IO.read` is used with a dynamic string safely" do
expect_no_offenses(<<~'RUBY')
input = "test"
IO.read("somefile#{input}.txt")
RUBY
end
it "reports an offense when `IO.read` is used with a concatenated string starting with a pipe character" do
2023-02-10 08:59:51 +00:00
expect_offense(<<~RUBY)
2021-04-14 16:08:37 +01:00
input = "|echo test"
IO.read("|echo " + input)
^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/IORead: The use of `IO.read` is a security risk.
2021-04-14 16:08:37 +01:00
RUBY
end
it "reports an offense when `IO.read` is used with a concatenated string starting with untrustworthy input" do
2023-02-10 08:59:51 +00:00
expect_offense(<<~RUBY)
2021-04-14 16:08:37 +01:00
input = "|echo test"
IO.read(input + ".txt")
^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/IORead: The use of `IO.read` is a security risk.
2021-04-14 16:08:37 +01:00
RUBY
end
it "does not report an offense when `IO.read` is used with a concatenated string safely" do
2023-02-10 08:59:51 +00:00
expect_no_offenses(<<~RUBY)
2021-04-14 16:08:37 +01:00
input = "test"
IO.read("somefile" + input + ".txt")
RUBY
end
end