密码作为参数显示在堆栈跟踪中

问题描述 投票:0回答:3

我们记录代码中发生的所有异常以及关联的堆栈跟踪。

问题出在这个函数上:

public function Authenticate($user, $password)
    //Authenticate the user
}

当该函数抛出异常时,堆栈跟踪包含使用的参数:用户密码以纯文本形式显示。

我该如何应对?我应该重写身份验证函数以仅接受加密密码吗?我可以禁止在堆栈跟踪中显示此特定参数吗?

欢迎任何想法。

编辑

我使用 getTraceAsString 函数来记录跟踪。

php stack-trace
3个回答
2
投票

您可以使用 Exception::getTrace() 方法来收集信息,并编写自己的自定义

getTraceAsString()
,不包括参数。

请参阅此示例,来自 Exception::getTrace() 文档

  function MakePrettyException(Exception $e) {
    $trace = $e->getTrace();

    $result = 'Exception: "';
    $result .= $e->getMessage();
    $result .= '" @ ';
    if($trace[0]['class'] != '') {
      $result .= $trace[0]['class'];
      $result .= '->';
    }
    $result .= $trace[0]['function'];
    $result .= '();<br />';

    return $result;
  }

1
投票

我建议两件事:

  1. 堆栈跟踪不应在客户端可见(如果还没有)

  2. 身份验证应仅接受密码的哈希版本

这样,即使有人拥有哈希密码的副本,他们也无法使用它来登录,或使用它来反转密码。

理想的方法当然是使用Xdebug之类的东西,其中collect_params的默认设置是0,这意味着变量不会显示在堆栈跟踪上。


0
投票

从 PHP 8.2 开始,您可以使用 SensitiveParameter 属性。

从该页面上的示例...

function defaultBehavior(
    string $secret,
    string $normal
) {
    throw new Exception('Error!');
}

有错误会给出

异常:错误!在 example.php:7 堆栈跟踪中: #0 example.php(19): defaultBehavior('密码', '正常') #1 {主要}

将秘密标记为敏感...

function sensitiveParametersWithAttribute(
    #[\SensitiveParameter]
    string $secret,
    string $normal
) {
    throw new Exception('Error!');
}

给予

异常:错误!在 example.php:15 堆栈跟踪中: #0 example.php(25):sensitiveParametersWithAttribute(Object(SensitiveParameterValue), '普通的') #1 {主要}

© www.soinside.com 2019 - 2024. All rights reserved.