dongpo0409 2013-11-15 19:38
浏览 189
已采纳

CSRF会话令牌无效

My session is not being set and I'm not sure why...

public static function generate( $key )
{
    $extra = self::$doOriginCheck ? sha1( $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT'] ) : '';
    $token = base64_encode( time() . $extra . self::randomString( 32 ) );
    $_SESSION[ 'csrf_' . $key ] = $token;
    return $token;
}

I use that to generate a key. On my log in form, after including my session file, I use:

$token = CSRF::generate("token"); // class name is CSRF

I then use it $token as a hidden value which is submitted along with the form.

Now to check it, I use a function called check() (this is the part where the exception is thrown:

public static function check( $key, $origin, $throwException=false, $timespan=null, $multiple=false )
    {
        if ( !isset( $_SESSION[ 'csrf_' . $key ] ) )
            if($throwException)
                throw new Exception( 'Missing session token.' );
            else
                return false;
                ....

I'm checking it as following:

CSRF::check($token, $_POST, true, 60*10, false);

($token is the token submitted). Why is is not saving the token in the session?

  • 写回答

1条回答 默认 最新

  • dozr162106 2013-11-15 19:41
    关注

    You're passing $token in as the first parameter for CSRF::check(). Surely, it should be:

    CSRF::check('token', $_POST, true, 60*10, false);
    

    instead of

    CSRF::check($token, $_POST, true, 60*10, false);
    

    Seeing as you're setting the key as 'token' in CSRF::generate? Otherwise:

    if ( !isset( $_SESSION[ 'csrf_' . $key ] ) )
    

    will be something like:

    if ( !isset( $_SESSION[ 'csrf_hed97988hdbnbnuihg07dede89723tg7yihoi3dh' ] ) )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样