CodeQL 文档

敏感信息以明文形式存储

ID: cs/cleartext-storage-of-sensitive-information
Kind: path-problem
Security severity: 7.5
Severity: error
Precision: high
Tags:
   - security
   - external/cwe/cwe-312
   - external/cwe/cwe-315
   - external/cwe/cwe-359
Query suites:
   - csharp-code-scanning.qls
   - csharp-security-extended.qls
   - csharp-security-and-quality.qls

点击查看 CodeQL 代码库中的查询

未加密存储的敏感信息可供获得存储访问权限的攻击者访问。这对于存储在最终用户计算机上的 Cookie 尤为重要。

建议

确保在存储敏感信息之前始终对其进行加密。对于 ASP.NET 应用程序,可以使用 System.Web.Security.MachineKey 类对敏感信息进行编码。

如果可能,请避免将敏感信息完全存储在 Cookie 中。而是建议在 Cookie 中存储一个密钥,该密钥可用于查找敏感信息。

通常,仅在需要以明文形式使用敏感信息时才对其进行解密。

示例

以下示例显示了在 Cookie 中存储用户凭据的两种方法。在“错误”情况下,凭据仅以明文形式存储。在“正确”情况下,在存储凭据之前使用 MachineKey.Protect(封装在实用工具方法中)对其进行保护。

using System.Text;
using System.Web;
using System.Web.Security;

public class CleartextStorageHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext ctx)
    {
        string accountName = ctx.Request.QueryString["AccountName"];
        // BAD: Setting a cookie value with cleartext sensitive data.
        ctx.Response.Cookies["AccountName"].Value = accountName;
        // GOOD: Encoding the value before setting it.
        ctx.Response.Cookies["AccountName"].Value = Protect(accountName, "Account name");
    }

    /// <summary>
    /// Protect the cleartext value, using the given type.
    /// </summary>
    /// <value>
    /// The protected value, which is no longer cleartext.
    /// </value>
    public string Protect(string value, string type)
    {
        return Encoding.UTF8.GetString(MachineKey.Protect(Encoding.UTF8.GetBytes(value), type));
    }
}

参考资料

  • M. Dowd、J. McDonald 和 J. Schuhm 合著的《软件安全评估的艺术》(第 1 版),第 2 章 -“加密的常见漏洞”,第 43 页。Addison Wesley 出版社,2006 年。

  • M. Howard 和 D. LeBlanc 合著的《编写安全代码》(第 2 版),第 9 章 -“保护机密数据”,第 299 页。微软出版社,2002 年。

  • 常见弱点枚举:CWE-312

  • 常见弱点枚举:CWE-315

  • 常见弱点枚举:CWE-359

  • ©GitHub 公司
  • 条款
  • 隐私