CheckMarx发布了PasswordBox的Heap_Inspection

时间:2017-11-08 16:28:32

标签: c# wpf mvvm passwordbox checkmarx

我对Checkmarx的问题感到震惊。

我的应用程序是用WPF编写的,带有C#编码。下面是PasswordBox的XAML

<PasswordBox x:Name="pwdPassword"
Controls:PasswordBoxHelper.CapsLockWarningToolTip="True" 
Controls:PasswordBoxHelper.CapsLockIcon="CAPSLOCK IS ON"
Controls:TextBoxHelper.Watermark="Password" 
HorizontalContentAlignment="Left" 
Controls:TextBoxHelper.ClearTextButton="True" HorizontalAlignment="Left" 
Margin="121,144,0,0" VerticalAlignment="Top" Width="253"/>

这用于针对Active Directory检查用户,我使用以下代码进行身份验证

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "DomainName"))
 {
bool isValid = pc.ValidateCredentials(txtUsername.Text, ADMIN.ConvertToUnsecureString(pwdPassword.SecurePassword), ContextOptions.Negotiate);
pwdPassword=null;

if (isValid)
 {
 //Open the application
}
else
{
//Display message and quit.
}

该应用程序工作正常,但当我使用Checkmarx扫描时,我得到了以下Heap_Inspection问题。

&GT;方法VerticalAlignment =&#34; Top&#34; /&gt;在\ Test1 \ MainWindow.xaml的第19行定义了pwdPassword,它被指定包含用户密码。但是,虽然稍后将明文密码分配给pwdPassword,但此变量永远不会从内存中清除。

我已经完成了将pwdPassword设置为null,但是我得到了错误。我还使用下面的finally块来收集垃圾

finally
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

有人可以帮助我如何从记忆中清除它。

非常感谢。

--------- *********************进一步进展*************** * -------------

我做了以下更改,并假设普通密码未保存到变量但未成功

using System.Security;
using System.Windows;
namespace Stack
{
    public static class PasswordBoxMVVMAttachedProperties
    {
        public static SecureString GetEncryptedPassword(DependencyObject obj)
        {
            return (SecureString)obj.GetValue(EncryptedPasswordProperty);
        }
        public static void SetEncryptedPassword(DependencyObject obj, SecureString value)
        {
            obj.SetValue(EncryptedPasswordProperty, value);
        }
        // Using a DependencyProperty as the backing store for EncryptedPassword. This enables animation, styling, binding, etc... 
        public static readonly DependencyProperty EncryptedPasswordProperty =
            DependencyProperty.RegisterAttached("EncryptedPassword", typeof(SecureString), typeof(PasswordBoxMVVMAttachedProperties));
    }
}

Xaml已更改为

<PasswordBox x:Name="pwdPassword" local:PasswordBoxMVVMAttachedProperties.EncryptedPassword="{Binding PasswordSecureString, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Controls:PasswordBoxHelper.CapsLockWarningToolTip="True" Controls:PasswordBoxHelper.CapsLockIcon="CAPSLOCK IS ON" Controls:TextBoxHelper.Watermark="Password" HorizontalContentAlignment="Left" Controls:TextBoxHelper.ClearTextButton="True" HorizontalAlignment="Left" Margin="121,144,0,0" VerticalAlignment="Top" Width="253" PasswordChanged="pwdPassword_PasswordChanged"/>

并且PasswordChanged背后的代码在

之下
private void pwdPassword_PasswordChanged(object sender, RoutedEventArgs e)
{
     pwdPassword = sender as PasswordBox;
Stack.PasswordBoxMVVMAttachedProperties.SetEncryptedPassword(pwdPassword, pwdPassword.SecurePassword);
    }

登录按钮具有以下代码

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "DOMAINNAME"))
     {
bool isValid = pc.ValidateCredentials(txtUsername.Text,
ADMIN.ConvertToUnsecureString(Stack.PasswordBoxMVVMAttachedProperties.GetEncryptedPassword(pwdPassword)), ContextOptions.Negotiate);
              //              pwdPassword = null;
if (isValid == true)
{
//Open Application
}
else
{
//Quit Application
}
}

尽管如此,我也得到了同样的错误。有人可以请仔细检查并帮我找到我错过的地方,如果我的话。

非常感谢任何帮助。

最新更新:07/12/2017

我已将问题归咎于它的根本原因。

我创建了一个只带有PasswordBox的WPF(后面没有代码),并在CheckMarx上扫描它并得到相同的错误(请查看附图。) Checkmarx Screenshot

0 个答案:

没有答案