Skip to content

Commit

Permalink
Reject directory traversal in store report processor
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarlisle committed Jun 28, 2012
1 parent fe53647 commit 554eefc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/puppet/reports/store.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'puppet'

SEPARATOR = [Regexp.escape(File::SEPARATOR.to_s), Regexp.escape(File::ALT_SEPARATOR.to_s)].join

Puppet::Reports.register_report(:store) do
desc "Store the yaml report on disk. Each host sends its report as a YAML dump
and this just stores the file on disk, in the `reportdir` directory.
Expand All @@ -11,9 +13,11 @@
def process
# We don't want any tracking back in the fs. Unlikely, but there
# you go.
client = self.host.gsub("..",".")
if host =~ Regexp.union(/[#{SEPARATOR}]/, /\A\.\.?\Z/)
raise ArgumentError, "Invalid node name #{host.inspect}"
end

dir = File.join(Puppet[:reportdir], client)
dir = File.join(Puppet[:reportdir], host)

if ! FileTest.exists?(dir)
FileUtils.mkdir_p(dir)
Expand All @@ -35,7 +39,7 @@ def process
end
rescue => detail
puts detail.backtrace if Puppet[:trace]
Puppet.warning "Could not write report for #{client} at #{file}: #{detail}"
Puppet.warning "Could not write report for #{host} at #{file}: #{detail}"
end

# Only testing cares about the return value
Expand Down
14 changes: 14 additions & 0 deletions spec/unit/reports/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,19 @@

File.read(File.join(Puppet[:reportdir], @report.host, "201101061200.yaml")).should == @report.to_yaml
end

['..', 'hello/', '/hello', 'he/llo', 'hello/..', '.'].each do |node|
it "rejects #{node.inspect}" do
@report.host = node
expect { @report.process }.to raise_error(ArgumentError, /Invalid node/)
end
end

['.hello', 'hello.', '..hi', 'hi..'].each do |node|
it "accepts #{node.inspect}" do
@report.host = node
@report.process
end
end
end
end

0 comments on commit 554eefc

Please sign in to comment.