Skip to content

Commit a091e39

Browse files
committed
Add many more adversarial test cases to ensure we don't have any ReDoS regressions
1 parent 463a819 commit a091e39

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

spec/addressable/template_spec.rb

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,126 @@ def self.match(name)
10131013
end.not_to raise_error
10141014
end
10151015
end
1016+
context "# operator with explode modifier" do
1017+
subject { Addressable::Template.new("{#foo*}") }
1018+
it "should match in linear time against a non-matching payload" do
1019+
expect do
1020+
Timeout.timeout(10) do
1021+
expect(subject.match(("#" + "a," * 1000) + " ")).to be_nil
1022+
end
1023+
end.not_to raise_error
1024+
end
1025+
end
1026+
context "+ operator with explode modifier and surrounding literals" do
1027+
subject { Addressable::Template.new("x{+foo*}y") }
1028+
it "should match in linear time against a non-matching payload" do
1029+
expect do
1030+
Timeout.timeout(10) do
1031+
expect(subject.match("x" + ("a," * 1000) + " ")).to be_nil
1032+
end
1033+
end.not_to raise_error
1034+
end
1035+
end
1036+
context "+ operator with multiple explode-modified variables" do
1037+
subject { Addressable::Template.new("{+foo*,bar*}") }
1038+
it "should match in linear time against a non-matching payload" do
1039+
expect do
1040+
Timeout.timeout(10) do
1041+
expect(subject.match(("a," * 1000) + " ")).to be_nil
1042+
end
1043+
end.not_to raise_error
1044+
end
1045+
end
1046+
context "default operator with explode modifier" do
1047+
subject { Addressable::Template.new("{foo*}") }
1048+
it "should match in linear time against a non-matching payload" do
1049+
expect do
1050+
Timeout.timeout(10) do
1051+
expect(subject.match(("a," * 1000) + "!")).to be_nil
1052+
end
1053+
end.not_to raise_error
1054+
end
1055+
end
1056+
context "/ operator with explode modifier" do
1057+
subject { Addressable::Template.new("{/foo*}") }
1058+
it "should match in linear time against a non-matching payload" do
1059+
expect do
1060+
Timeout.timeout(10) do
1061+
expect(subject.match(("a/" * 1000) + "!")).to be_nil
1062+
end
1063+
end.not_to raise_error
1064+
end
1065+
end
1066+
context ". operator with explode modifier" do
1067+
subject { Addressable::Template.new("{.foo*}") }
1068+
it "should match in linear time against a non-matching payload" do
1069+
expect do
1070+
Timeout.timeout(10) do
1071+
expect(subject.match(("a." * 1000) + "!")).to be_nil
1072+
end
1073+
end.not_to raise_error
1074+
end
1075+
end
1076+
context "; operator with explode modifier" do
1077+
subject { Addressable::Template.new("{;foo*}") }
1078+
it "should match in linear time against a non-matching payload" do
1079+
expect do
1080+
Timeout.timeout(10) do
1081+
expect(subject.match(("a=1;" * 1000) + "!")).to be_nil
1082+
end
1083+
end.not_to raise_error
1084+
end
1085+
end
1086+
context "? operator with explode modifier" do
1087+
subject { Addressable::Template.new("{?foo*}") }
1088+
it "should match in linear time against a non-matching payload" do
1089+
expect do
1090+
Timeout.timeout(10) do
1091+
expect(subject.match("?" + ("a=1&" * 1000) + "!")).to be_nil
1092+
end
1093+
end.not_to raise_error
1094+
end
1095+
end
1096+
context "& operator with explode modifier" do
1097+
subject { Addressable::Template.new("{&foo*}") }
1098+
it "should match in linear time against a non-matching payload" do
1099+
expect do
1100+
Timeout.timeout(10) do
1101+
expect(subject.match(("&a=1" * 1000) + "!")).to be_nil
1102+
end
1103+
end.not_to raise_error
1104+
end
1105+
end
1106+
context "many variables in a single + expression" do
1107+
subject { Addressable::Template.new("{+v1,v2,v3,v4,v5,v6,v7,v8,v9,v10}") }
1108+
it "should match in linear time against a non-matching payload" do
1109+
expect do
1110+
Timeout.timeout(10) do
1111+
expect(subject.match(("a," * 1000) + " ")).to be_nil
1112+
end
1113+
end.not_to raise_error
1114+
end
1115+
end
1116+
context "consecutive + expressions without separators" do
1117+
subject { Addressable::Template.new("{+a}{+b}{+c}") }
1118+
it "should match in linear time against a non-matching payload" do
1119+
expect do
1120+
Timeout.timeout(10) do
1121+
expect(subject.match(("a" * 1000) + " ")).to be_nil
1122+
end
1123+
end.not_to raise_error
1124+
end
1125+
end
1126+
context "percent-encoded payload with + explode modifier" do
1127+
subject { Addressable::Template.new("{+foo*}") }
1128+
it "should match in linear time against a non-matching percent-encoded payload" do
1129+
expect do
1130+
Timeout.timeout(10) do
1131+
expect(subject.match(("%2F" * 1000) + " ")).to be_nil
1132+
end
1133+
end.not_to raise_error
1134+
end
1135+
end
10161136
context ". operator" do
10171137
subject { Addressable::Template.new("foo{.foo,bar}baz") }
10181138
it "can match" do

0 commit comments

Comments
 (0)