Skip to content

Commit d631998

Browse files
authored
Merge commit from fork
Only allow valid cache filenames in `Measured::Cache::Json` initializer
2 parents 171f31f + 9414232 commit d631998

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/measured/cache/json.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class Json
55

66
def initialize(filename)
77
@filename = filename
8-
@path = Pathname.new(File.join(File.dirname(__FILE__), "../../../cache", @filename)).cleanpath
8+
raise ArgumentError, "Invalid cache file: #{filename}" unless %w[length.json weight.json volume.json test.json].include?(filename.to_s)
9+
@path = Pathname.new(File.join(File.dirname(__FILE__), "../../../cache", filename)).cleanpath
910
end
1011

1112
def exist?

test/cache/json_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ class Measured::Cache::JsonTest < ActiveSupport::TestCase
1414
refute_match "../", @cache.path.to_s
1515
end
1616

17+
test "#initialize accepts valid cache filenames for Measured::Measurable.subclasses, and test file" do
18+
valid_files = %w[length.json weight.json volume.json test.json]
19+
valid_files.each do |filename|
20+
assert_nothing_raised do
21+
Measured::Cache::Json.new(filename)
22+
end
23+
end
24+
end
25+
26+
test "#initialize rejects invalid cache filenames" do
27+
invalid_files = ["volum.json", "../volume.json", "other.txt"]
28+
invalid_files.each do |filename|
29+
assert_raises ArgumentError do
30+
Measured::Cache::Json.new(filename)
31+
end
32+
end
33+
end
34+
1735
test "#exist? returns false if the file does not exist" do
1836
File.expects(:exist?).with(@cache.path).returns(false)
1937
refute_predicate @cache, :exist?

0 commit comments

Comments
 (0)