CodeQL 文档

会话未放弃

ID: cs/session-reuse
Kind: problem
Security severity: 8.8
Severity: error
Precision: high
Tags:
   - security
   - external/cwe/cwe-384
Query suites:
   - csharp-code-scanning.qls
   - csharp-security-extended.qls
   - csharp-security-and-quality.qls

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

重复使用会话可能会允许攻击者获得对另一个帐户的未授权访问。始终确保在用户登录或注销时放弃当前会话,以便可以启动新会话。

建议

始终调用 HttpSessionState.Abandon() 以确保新用户不会使用之前的会话。

示例

以下示例显示了在身份验证后使用之前的会话。这将允许之前的用户使用新用户的帐户。

public void Login(HttpContext ctx, string username, string password)
{
    if (FormsAuthentication.Authenticate(username, password)
    {
        // BAD: Reusing the previous session
        ctx.Session["Mode"] = GetModeForUser(username);
    }
}

此代码示例通过不重复使用会话来解决问题,而是调用 Abandon() 以确保不会重复使用会话。

public void Login(HttpContext ctx, string username, string password)
{
    if (FormsAuthentication.Authenticate(username, password)
    {
        // GOOD: Abandon the session first.
        ctx.Session.Abandon();
    }
}

参考资料

  • ©GitHub 公司
  • 条款
  • 隐私