Skip to content

Commit 6ca5d51

Browse files
committed
[feature]
removed code to show a GUI prompt for credentials as PSCore6 prompts in console
1 parent 5bd8dd3 commit 6ca5d51

File tree

2 files changed

+45
-130
lines changed

2 files changed

+45
-130
lines changed

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePrompt.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -315,31 +315,12 @@ out object convertedObj
315315
{
316316
WriteLineToConsole(WrapToCurrentWindowWidth(fieldPrompt));
317317
PSCredential credential = null;
318-
// the earlier implementation contained null
319-
// for caption and message in the call below
320-
// Passing null is a potential security risk
321-
// as any modifications made with security in
322-
// mind is lost. This can lead to a malicious
323-
// server prompting the user for a request
324-
// which can appear to come from locally.
325-
if (!PromptUsingConsole() && desc.ModifiedByRemotingProtocol)
326-
{
327-
credential =
328-
PromptForCredential(
329-
caption,
330-
message,
331-
null,
332-
string.Empty);
333-
}
334-
else
335-
{
336-
credential =
337-
PromptForCredential(
338-
null, // caption already written
339-
null, // message already written
340-
null,
341-
string.Empty);
342-
}
318+
credential =
319+
PromptForCredential(
320+
null, // caption already written
321+
null, // message already written
322+
null,
323+
string.Empty);
343324
convertedObj = credential;
344325
cancelInput = (convertedObj == null);
345326
if ((credential != null) && (credential.Password.Length == 0) && listInput)

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs

Lines changed: 39 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -78,127 +78,61 @@ public override PSCredential PromptForCredential(
7878
PSCredentialTypes allowedCredentialTypes,
7979
PSCredentialUIOptions options)
8080
{
81-
if (!PromptUsingConsole())
82-
{
83-
IntPtr mainWindowHandle = GetMainWindowHandle();
84-
return HostUtilities.CredUIPromptForCredential(caption, message, userName, targetName, allowedCredentialTypes, options, mainWindowHandle);
85-
}
86-
else
87-
{
88-
PSCredential cred = null;
89-
SecureString password = null;
90-
string userPrompt = null;
91-
string passwordPrompt = null;
92-
93-
if (!string.IsNullOrEmpty(caption))
94-
{
95-
// Should be a skin lookup
81+
PSCredential cred = null;
82+
SecureString password = null;
83+
string userPrompt = null;
84+
string passwordPrompt = null;
9685

97-
WriteLineToConsole();
98-
WriteToConsole(PromptColor, RawUI.BackgroundColor, WrapToCurrentWindowWidth(caption));
99-
WriteLineToConsole();
100-
}
101-
102-
if (!string.IsNullOrEmpty(message))
103-
{
104-
WriteLineToConsole(WrapToCurrentWindowWidth(message));
105-
}
86+
if (!string.IsNullOrEmpty(caption))
87+
{
88+
// Should be a skin lookup
10689

107-
if (string.IsNullOrEmpty(userName))
108-
{
109-
userPrompt = ConsoleHostUserInterfaceSecurityResources.PromptForCredential_User;
90+
WriteLineToConsole();
91+
WriteToConsole(PromptColor, RawUI.BackgroundColor, WrapToCurrentWindowWidth(caption));
92+
WriteLineToConsole();
93+
}
11094

111-
//
112-
// need to prompt for user name first
113-
//
114-
do
115-
{
116-
WriteToConsole(userPrompt, true);
117-
userName = ReadLine();
118-
if (userName == null)
119-
{
120-
return null;
121-
}
122-
}
123-
while (userName.Length == 0);
124-
}
95+
if (!string.IsNullOrEmpty(message))
96+
{
97+
WriteLineToConsole(WrapToCurrentWindowWidth(message));
98+
}
12599

126-
passwordPrompt = StringUtil.Format(ConsoleHostUserInterfaceSecurityResources.PromptForCredential_Password, userName
127-
);
100+
if (string.IsNullOrEmpty(userName))
101+
{
102+
userPrompt = ConsoleHostUserInterfaceSecurityResources.PromptForCredential_User;
128103

129104
//
130-
// now, prompt for the password
105+
// need to prompt for user name first
131106
//
132-
WriteToConsole(passwordPrompt, true);
133-
password = ReadLineAsSecureString();
134-
if (password == null)
107+
do
135108
{
136-
return null;
109+
WriteToConsole(userPrompt, true);
110+
userName = ReadLine();
111+
if (userName == null)
112+
{
113+
return null;
114+
}
137115
}
138-
WriteLineToConsole();
139-
140-
cred = new PSCredential(userName, password);
141-
142-
return cred;
116+
while (userName.Length == 0);
143117
}
144-
}
145118

146-
private IntPtr GetMainWindowHandle()
147-
{
148-
#if CORECLR // No System.Diagnostics.Process.MainWindowHandle on CoreCLR;
149-
// Returned WindowHandle is used only in 1 case - prompting for credential using GUI dialog, which is not used on Nano,
150-
// because on Nano we prompt for credential using console (different code path in 'PromptForCredential' function)
151-
return IntPtr.Zero;
152-
#else
153-
System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
154-
IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
119+
passwordPrompt = StringUtil.Format(ConsoleHostUserInterfaceSecurityResources.PromptForCredential_Password, userName
120+
);
155121

156-
while ((mainWindowHandle == IntPtr.Zero) && (currentProcess != null))
122+
//
123+
// now, prompt for the password
124+
//
125+
WriteToConsole(passwordPrompt, true);
126+
password = ReadLineAsSecureString();
127+
if (password == null)
157128
{
158-
currentProcess = PsUtils.GetParentProcess(currentProcess);
159-
if (currentProcess != null)
160-
{
161-
mainWindowHandle = currentProcess.MainWindowHandle;
162-
}
129+
return null;
163130
}
131+
WriteLineToConsole();
164132

165-
return mainWindowHandle;
166-
#endif
167-
}
168-
169-
// Determines whether we should prompt using the Console prompting
170-
// APIs
171-
private bool PromptUsingConsole()
172-
{
173-
#if CORECLR
174-
// on Nano there is no other way to prompt except by using console
175-
return true;
176-
#else
177-
bool promptUsingConsole = false;
178-
// Get the configuration setting
179-
try
180-
{
181-
promptUsingConsole = ConfigPropertyAccessor.Instance.GetConsolePrompting();
182-
}
183-
catch (System.Security.SecurityException e)
184-
{
185-
s_tracer.TraceError("Could not read CredUI registry key: " + e.Message);
186-
return promptUsingConsole;
187-
}
188-
catch (InvalidCastException e)
189-
{
190-
s_tracer.TraceError("Could not parse CredUI registry key: " + e.Message);
191-
return promptUsingConsole;
192-
}
193-
catch (FormatException e)
194-
{
195-
s_tracer.TraceError("Could not parse CredUI registry key: " + e.Message);
196-
return promptUsingConsole;
197-
}
133+
cred = new PSCredential(userName, password);
198134

199-
s_tracer.WriteLine("DetermineCredUIPolicy: policy == {0}", promptUsingConsole);
200-
return promptUsingConsole;
201-
#endif
135+
return cred;
202136
}
203137
}
204138
}

0 commit comments

Comments
 (0)