Skip to content

Commit a12743c

Browse files
(maint) Merge up 121a967 to main
Generated by CI * commit '121a9673877f84d96b950ffc8d47630af152f3fc': (PUP-10899) Add Windows account name sanitization for lookup
2 parents 8660b32 + 121a967 commit a12743c

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

lib/puppet/util/windows/principal.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def to_s
4444
ERROR_INVALID_PARAMETER = 87
4545
ERROR_INSUFFICIENT_BUFFER = 122
4646

47-
def self.lookup_account_name(system_name = nil, account_name)
47+
def self.lookup_account_name(system_name = nil, sanitize = true, account_name)
48+
account_name = sanitize_account_name(account_name) if sanitize
4849
system_name_ptr = FFI::Pointer::NULL
4950
begin
5051
if system_name
@@ -146,6 +147,13 @@ def self.lookup_account_sid(system_name = nil, sid_bytes)
146147
end
147148
end
148149

150+
# Sanitize the given account name for lookup to avoid known issues
151+
def self.sanitize_account_name(account_name)
152+
return account_name unless account_name.start_with?('APPLICATION PACKAGE AUTHORITY\\')
153+
account_name.split('\\').last
154+
end
155+
private_class_method :sanitize_account_name
156+
149157
ffi_convention :stdcall
150158

151159
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa379601(v=vs.85).aspx
@@ -191,4 +199,3 @@ def self.lookup_account_sid(system_name = nil, sid_bytes)
191199
[:lpcwstr, :pointer, :lpwstr, :lpdword, :lpwstr, :lpdword, :pointer], :win32_bool
192200
end
193201
end
194-

spec/integration/util/windows/principal_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
let (:system_bytes) { [1, 1, 0, 0, 0, 0, 0, 5, 18, 0, 0, 0] }
88
let (:null_sid_bytes) { [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }
99
let (:administrator_bytes) { [1, 2, 0, 0, 0, 0, 0, 5, 32, 0, 0, 0, 32, 2, 0, 0] }
10+
let (:all_application_packages_bytes) { [1, 2, 0, 0, 0, 0, 0, 15, 2, 0, 0, 0, 1, 0, 0, 0] }
1011
let (:computer_sid) { Puppet::Util::Windows::SID.name_to_principal(Puppet::Util::Windows::ADSI.computer_name) }
1112
# BUILTIN is localized on German Windows, but not French
1213
# looking this up like this dilutes the values of the tests as we're comparing two mechanisms
@@ -121,6 +122,26 @@
121122
expect(principal.to_s).to eq(builtin_localized)
122123
end
123124

125+
it "should always sanitize the account name first" do
126+
expect(Puppet::Util::Windows::SID::Principal).to receive(:sanitize_account_name).with('NT AUTHORITY\\SYSTEM').and_call_original
127+
Puppet::Util::Windows::SID::Principal.lookup_account_name('NT AUTHORITY\\SYSTEM')
128+
end
129+
130+
it "should be able to create an instance from an account name prefixed by APPLICATION PACKAGE AUTHORITY" do
131+
principal = Puppet::Util::Windows::SID::Principal.lookup_account_name('APPLICATION PACKAGE AUTHORITY\\ALL APPLICATION PACKAGES')
132+
expect(principal.account).to eq('ALL APPLICATION PACKAGES')
133+
expect(principal.sid_bytes).to eq(all_application_packages_bytes)
134+
expect(principal.sid).to eq('S-1-15-2-1')
135+
expect(principal.domain).to eq('APPLICATION PACKAGE AUTHORITY')
136+
expect(principal.domain_account).to eq('APPLICATION PACKAGE AUTHORITY\\ALL APPLICATION PACKAGES')
137+
expect(principal.account_type).to eq(:SidTypeWellKnownGroup)
138+
expect(principal.to_s).to eq('APPLICATION PACKAGE AUTHORITY\\ALL APPLICATION PACKAGES')
139+
end
140+
141+
it "should fail without proper account name sanitization when it is prefixed by APPLICATION PACKAGE AUTHORITY" do
142+
given_account_name = 'APPLICATION PACKAGE AUTHORITY\\ALL APPLICATION PACKAGES'
143+
expect { Puppet::Util::Windows::SID::Principal.lookup_account_name(nil, false, given_account_name) }.to raise_error(Puppet::Util::Windows::Error, /No mapping between account names and security IDs was done./)
144+
end
124145
end
125146

126147
describe ".lookup_account_sid" do

0 commit comments

Comments
 (0)