C# “如何修复”;密码管理,空密码;?

C# “如何修复”;密码管理,空密码;?,c#,security,passwords,fortify,C#,Security,Passwords,Fortify,HP fortify警告我以下代码可能存在一些安全问题: string storedPassword = ""; string temp; if ((temp = ReadPassword(storedPassword)) != null) { storedPassword = temp; } if(Utils.VerifyPassword(storedPassword, userPassword)) // Access protected resources ...

HP fortify警告我以下代码可能存在一些安全问题:

string storedPassword = "";
string temp;

if ((temp = ReadPassword(storedPassword)) != null) {
    storedPassword = temp;
}

if(Utils.VerifyPassword(storedPassword, userPassword))
    // Access protected resources
    ...
}
如果readPassword()由于数据库错误或其他问题而无法检索存储的密码,则攻击者可以通过为userPassword提供空字符串来绕过密码检查


好的。。。我同意这个指示。。。但如何解决这个问题呢?代码处理密码管理的最佳实践是什么?

您只需使用
IsNullOrWhiteSpace
语句:

public bool VerifyPassword(string storedPassword, string userPassword)
{
    if(string.IsNullOrWhiteSpace(userPassword))
    {
        return false;
    }
}

您只需使用
IsNullOrWhiteSpace
语句:

public bool VerifyPassword(string storedPassword, string userPassword)
{
    if(string.IsNullOrWhiteSpace(userPassword))
    {
        return false;
    }
}

在获得有效的密码值之前,不要初始化密码(在本例中为空字符串)

string storedPassword;
//string storedPassword = null;
string temp;

if ((temp = ReadPassword(storedPassword)) != null) {
    storedPassword = temp;
}

if(Utils.VerifyPassword(storedPassword, userPassword))
    // Access protected resources
    ...
}

在获得有效的密码值之前,不要初始化密码(在本例中为空字符串)

string storedPassword;
//string storedPassword = null;
string temp;

if ((temp = ReadPassword(storedPassword)) != null) {
    storedPassword = temp;
}

if(Utils.VerifyPassword(storedPassword, userPassword))
    // Access protected resources
    ...
}

那么
temp
变量和无用的空检查有什么意义呢?为什么不干脆
string storedPassword=ReadPassword()
?然后您可以检查
storedPassword
是否为null,尽管我无法想象
ReadPassword
的任何合理实现在失败时返回null而不是抛出异常。它毕竟不是C。那么
temp
变量和无用的空检查有什么意义呢?为什么不干脆
string storedPassword=ReadPassword()
?然后您可以检查
storedPassword
是否为null,尽管我无法想象
ReadPassword
的任何合理实现在失败时返回null而不是抛出异常。它毕竟不是C。如果不初始化密码,它将被标记为“密码管理:空密码”。如果不初始化密码,它将被标记为“密码管理:空密码”