{"record":{"id":"c25ddbd350a99cad","repo":"puppetlabs/puppet","slug":"invalid-registry-key-name","errorCode":null,"errorMessage":"Invalid registry key '%{name}'","messagePattern":"Invalid registry key '%(.+?)'","errorType":"exception","errorClass":"Puppet::Error","httpStatus":null,"severity":"error","filePath":"lib/puppet/util/windows/registry.rb","lineNumber":25,"sourceCode":"    require 'ffi'\n    extend FFI::Library\n\n    # https://msdn.microsoft.com/en-us/library/windows/desktop/aa384129(v=vs.85).aspx\n    KEY64 = 0x100\n    KEY32 = 0x200\n\n    KEY_READ       = 0x20019\n    KEY_WRITE      = 0x20006\n    KEY_ALL_ACCESS = 0x2003f\n\n    ERROR_NO_MORE_ITEMS = 259\n\n    WCHAR_SIZE = FFI.type_size(:wchar)\n\n    def root(name)\n      Win32::Registry.const_get(name)\n    rescue NameError => e\n      raise Puppet::Error, _(\"Invalid registry key '%{name}'\") % { name: name }, e.backtrace\n    end\n\n    def open(name, path, mode = KEY_READ | KEY64, &block)\n      hkey = root(name)\n      begin\n        hkey.open(path, mode) do |subkey|\n          return yield subkey\n        end\n      rescue Win32::Registry::Error => error\n        raise Puppet::Util::Windows::Error.new(_(\"Failed to open registry key '%{key}\\\\%{path}'\") % { key: hkey.keyname, path: path }, error.code, error)\n      end\n    end\n\n    def keys(key)\n      keys = {}\n      each_key(key) { |subkey, filetime| keys[subkey] = filetime }\n      keys\n    end","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/util/windows/registry.rb#L7-L43","documentation":"Raised by Puppet::Util::Windows::Registry.root (lib/puppet/util/windows/registry.rb:25) when Win32::Registry.const_get(name) throws NameError — the name is not a hive constant — re-raised as Puppet::Error with the original backtrace chained. Valid names are the full, case-sensitive Win32::Registry constants (HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER, HKEY_CLASSES_ROOT, HKEY_USERS, HKEY_PERFORMANCE_DATA, HKEY_CURRENT_CONFIG, HKEY_DYN_DATA); abbreviations like HKLM and PowerShell-style 'HKLM:' paths fail.","triggerScenarios":"Passing 'HKLM' or 'HKCU' instead of the full constant name; a nil, misspelled or whitespace-padded root arriving from manifest data; names built by concatenation with wrong casing (const_get is case-sensitive).","commonSituations":"Providers converting user-friendly hive shortcuts into root names; config files using PowerShell hive syntax; copy-paste between reg.exe paths (HKLM\\Software\\...) and Puppet data.","solutions":["Use the full hive constant names expected by Win32::Registry, exactly cased.","Map abbreviations before calling root(): HKLM to HKEY_LOCAL_MACHINE, HKCU to HKEY_CURRENT_USER, HKCR to HKEY_CLASSES_ROOT, HKU to HKEY_USERS, HKCC to HKEY_CURRENT_CONFIG.","Upcase and strip input, then validate against the known list at the data boundary.","Rescue Puppet::Error to fail with a message that echoes the offending name."],"exampleFix":"# before\nPuppet::Util::Windows::Registry.root('HKLM')\n\n# after — normalize abbreviations first\nHIVES = { 'HKLM' => 'HKEY_LOCAL_MACHINE', 'HKCU' => 'HKEY_CURRENT_USER', 'HKCR' => 'HKEY_CLASSES_ROOT',\n          'HKU' => 'HKEY_USERS', 'HKCC' => 'HKEY_CURRENT_CONFIG' }\nname = HIVES.fetch(name.upcase, name)\nPuppet::Util::Windows::Registry.root(name)","handlingStrategy":"validation","validationCode":"VALID_HIVES = %w[HKEY_CLASSES_ROOT HKEY_CURRENT_USER HKEY_LOCAL_MACHINE HKEY_USERS\n               HKEY_PERFORMANCE_DATA HKEY_CURRENT_CONFIG HKEY_DYN_DATA].freeze\nALIASES = { 'HKLM' => 'HKEY_LOCAL_MACHINE', 'HKCU' => 'HKEY_CURRENT_USER', 'HKCR' => 'HKEY_CLASSES_ROOT',\n            'HKU' => 'HKEY_USERS', 'HKCC' => 'HKEY_CURRENT_CONFIG' }.freeze\nname = ALIASES.fetch(name.to_s.upcase, name.to_s)\nraise ArgumentError, \"invalid registry hive #{name.inspect}\" unless VALID_HIVES.include?(name)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Canonicalize hive names at the config boundary, not deep inside providers","Strip PowerShell-style prefixes and reg.exe abbreviations before use","Reject unknown hives with an explicit allow-list in the error message"],"tags":["windows","registry","constants","validation","puppet"],"backgroundTag":"invalid-registry-root-key","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}