Skip to content

Commit cc5501f

Browse files
authored
Fail if the key path is empty. (#57)
We should fail on input like `=foo` as the key path is ambiguous.
1 parent ad0c267 commit cc5501f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/protocol/http/url.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ def self.scan(string)
6565
end
6666

6767
def self.split(name)
68-
name.scan(/([^\[]+)|(?:\[(.*?)\])/).flatten!.compact!
68+
name.scan(/([^\[]+)|(?:\[(.*?)\])/)&.tap do |parts|
69+
parts.flatten!
70+
parts.compact!
71+
end
6972
end
7073

74+
# Assign a value to a nested hash.
7175
def self.assign(keys, value, parent)
7276
top, *middle = keys
7377

@@ -95,6 +99,10 @@ def self.decode(string, maximum = 8, symbolize_keys: false)
9599
self.scan(string) do |name, value|
96100
keys = self.split(name)
97101

102+
if keys.empty?
103+
raise ArgumentError, "Invalid key path: #{name.inspect}!"
104+
end
105+
98106
if keys.size > maximum
99107
raise ArgumentError, "Key length exceeded limit!"
100108
end

test/protocol/http/url.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@
7070
Protocol::HTTP::URL.decode("a[b][c][d][e][f][g][h][i]=10")
7171
end.to raise_exception(ArgumentError, message: be =~ /Key length exceeded/)
7272
end
73+
74+
it "fails with missing key" do
75+
expect do
76+
Protocol::HTTP::URL.decode("=foo")
77+
end.to raise_exception(ArgumentError, message: be =~ /Invalid key/)
78+
end
7379
end
7480

7581
with '.unescape' do

0 commit comments

Comments
 (0)