Skip to content

Commit

Permalink
Better validation and testing of key paths
Browse files Browse the repository at this point in the history
Previously, we allowed key paths to end in backslashes and we allowed key
names to be more than 255 characters, which are both wrong.

This commit uses a regex to perform better validation. It also adds
better tests, and to ensure that the capture of the repeating capture
group works (for more than a single level).
  • Loading branch information
joshcooper committed Apr 19, 2012
1 parent e9f00c1 commit d05d1e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
5 changes: 4 additions & 1 deletion lib/puppet/util/registry_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ def ascend(hkey, subkey, &block)
end

def key_split(path)
rootkey, subkey = path.split('\\', 2)
unless match = /^([^\\]*)((?:\\[^\\]{1,255})*)$/.match(path)
raise ArgumentError, "Invalid registry key: #{path}"
end

rootkey, subkey = match.captures
hkey =
case rootkey.downcase
when /hkey_local_machine/, /hklm/
Expand Down
30 changes: 13 additions & 17 deletions spec/unit/puppet/type/registry_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
require 'puppet/util/registry_base'

describe Puppet::Type.type(:registry_key) do
let (:path) { 'HKLM\Software\PuppetSpecTest' }
let (:key) { Puppet::Type.type(:registry_key).new(:path => path) }
let (:key) { Puppet::Type.type(:registry_key).new(:path => 'HKLM\Software') }

[:ensure].each do |property|
it "should have a #{property} property" do
Expand All @@ -21,26 +20,23 @@
Puppet::Type.type(:registry_key).attrtype(:path).should == :param
end

it 'should accept a fully qualified path' do
key[:path].should == path
end

Puppet::Util::RegistryBase::HKEYS.each do |hkey|
it "should accept #{hkey}\\Subkey" do
described_class.new(:path => "#{hkey}\\Subkey")
%w[hklm hklm\software hklm\software\vendor].each do |path|
it "should accept #{path}" do
key[:path] = path
end
end

it 'should reject unknown keys' do
expect { key[:path] = 'UNKNOWN\Subkey' }.should raise_error(Puppet::Error)
end

it 'should accept a valid root key' do
key[:path] = 'HKLM'
%w[unknown unknown\subkey HKEY_PERFORMANCE_DATA].each do |path|
it "should reject #{path} as unsupported" do
expect { key[:path] = path }.should raise_error(Puppet::Error, /Unsupported/)
end
end

it 'should reject an unknown root key' do
expect { key[:path] = 'UNKNOWN' }.should raise_error(Puppet::Error)
%[hklm\\ hklm\foo\\].each do |path|
it "should reject #{path} as invalid" do
path = "hklm\\" + 'a' * 256
expect { key[:path] = path }.should raise_error(Puppet::Error, /Invalid registry key/)
end
end

%w[HKLM HKEY_LOCAL_MACHINE hklm].each do |root|
Expand Down

0 comments on commit d05d1e6

Please sign in to comment.