Skip to content

Commit c872619

Browse files
committed
Reject file requests containing ..
1 parent c3c7462 commit c872619

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

lib/puppet/file_serving/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def split_path(request)
7171
mount_name, path = request.key.split(File::Separator, 2)
7272

7373
raise(ArgumentError, "Cannot find file: Invalid mount '#{mount_name}'") unless mount_name =~ %r{^[-\w]+$}
74+
raise(ArgumentError, "Cannot find file: Invalid relative path '#{path}'") if path and path.split('/').include?('..')
7475

7576
return nil unless mount = find_mount(mount_name, request.environment)
7677
if mount.name == "modules" and mount_name != "modules"

spec/unit/file_serving/configuration_spec.rb

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -170,80 +170,88 @@
170170
end
171171
end
172172

173-
describe "when finding the mount name and relative path in a request key" do
174-
before do
175-
@config = Puppet::FileServing::Configuration.create
176-
@config.stubs(:find_mount)
173+
describe "#split_path" do
174+
let(:config) { Puppet::FileServing::Configuration.create }
175+
let(:request) { stub 'request', :key => "foo/bar/baz", :options => {}, :node => nil, :environment => mock("env") }
177176

178-
@request = stub 'request', :key => "foo/bar/baz", :options => {}, :node => nil, :environment => mock("env")
177+
before do
178+
config.stubs(:find_mount)
179179
end
180180

181181
it "should reread the configuration" do
182-
@config.expects(:readconfig)
182+
config.expects(:readconfig)
183183

184-
@config.split_path(@request)
184+
config.split_path(request)
185185
end
186186

187187
it "should treat the first field of the URI path as the mount name" do
188-
@config.expects(:find_mount).with { |name, node| name == "foo" }
188+
config.expects(:find_mount).with { |name, node| name == "foo" }
189189

190-
@config.split_path(@request)
190+
config.split_path(request)
191191
end
192192

193193
it "should fail if the mount name is not alpha-numeric" do
194-
@request.expects(:key).returns "foo&bar/asdf"
194+
request.expects(:key).returns "foo&bar/asdf"
195195

196-
lambda { @config.split_path(@request) }.should raise_error(ArgumentError)
196+
lambda { config.split_path(request) }.should raise_error(ArgumentError)
197197
end
198198

199199
it "should support dashes in the mount name" do
200-
@request.expects(:key).returns "foo-bar/asdf"
200+
request.expects(:key).returns "foo-bar/asdf"
201201

202-
lambda { @config.split_path(@request) }.should_not raise_error(ArgumentError)
202+
lambda { config.split_path(request) }.should_not raise_error(ArgumentError)
203203
end
204204

205205
it "should use the mount name and environment to find the mount" do
206-
@config.expects(:find_mount).with { |name, env| name == "foo" and env == @request.environment }
207-
@request.stubs(:node).returns("mynode")
206+
config.expects(:find_mount).with { |name, env| name == "foo" and env == request.environment }
207+
request.stubs(:node).returns("mynode")
208208

209-
@config.split_path(@request)
209+
config.split_path(request)
210210
end
211211

212212
it "should return nil if the mount cannot be found" do
213-
@config.expects(:find_mount).returns nil
213+
config.expects(:find_mount).returns nil
214214

215-
@config.split_path(@request).should be_nil
215+
config.split_path(request).should be_nil
216216
end
217217

218218
it "should return the mount and the relative path if the mount is found" do
219219
mount = stub 'mount', :name => "foo"
220-
@config.expects(:find_mount).returns mount
220+
config.expects(:find_mount).returns mount
221221

222-
@config.split_path(@request).should == [mount, "bar/baz"]
222+
config.split_path(request).should == [mount, "bar/baz"]
223223
end
224224

225225
it "should remove any double slashes" do
226-
@request.stubs(:key).returns "foo/bar//baz"
226+
request.stubs(:key).returns "foo/bar//baz"
227227
mount = stub 'mount', :name => "foo"
228-
@config.expects(:find_mount).returns mount
228+
config.expects(:find_mount).returns mount
229+
230+
config.split_path(request).should == [mount, "bar/baz"]
231+
end
232+
233+
it "should fail if the path contains .." do
234+
request.stubs(:key).returns 'module/foo/../../bar'
229235

230-
@config.split_path(@request).should == [mount, "bar/baz"]
236+
expect do
237+
config.split_path(request)
238+
end.to raise_error(ArgumentError, /Invalid relative path/)
231239
end
232240

233241
it "should return the relative path as nil if it is an empty string" do
234-
@request.expects(:key).returns "foo"
242+
request.expects(:key).returns "foo"
235243
mount = stub 'mount', :name => "foo"
236-
@config.expects(:find_mount).returns mount
244+
config.expects(:find_mount).returns mount
237245

238-
@config.split_path(@request).should == [mount, nil]
246+
config.split_path(request).should == [mount, nil]
239247
end
240248

241249
it "should add 'modules/' to the relative path if the modules mount is used but not specified, for backward compatibility" do
242-
@request.expects(:key).returns "foo/bar"
250+
request.expects(:key).returns "foo/bar"
243251
mount = stub 'mount', :name => "modules"
244-
@config.expects(:find_mount).returns mount
252+
config.expects(:find_mount).returns mount
245253

246-
@config.split_path(@request).should == [mount, "foo/bar"]
254+
config.split_path(request).should == [mount, "foo/bar"]
247255
end
248256
end
249257
end

0 commit comments

Comments
 (0)