duanbaque4230 2015-08-26 20:04
浏览 31
已采纳

我通过jquery字符串发送Cross站点伪造令牌,但在下一页我生成一个新令牌然后他们的令牌将永远不会匹配

Okay, so I am storing their tokes in a session:

Session::get('token', 'randomtokenstringhere');

On each form, whether it's successful or not I generate a new token, and update their session token, now say for example user 1 is on a update profile page, and I serialize a form, with a token on page load echo the token variable) and they submit the form, which runs an ajax, and either completes or there's an error, my system generate a new token, but now on the edit profile page, they're still using the old token, so now their tokens will never match.

How can I fix this for extra security?

Here's an example of my ajax:

<!-- accept, reject, add and cancel friend requests -->
$('.add_friend').click(function(event) { // bind function to submit event of form
    event.preventDefault();
    $.ajax({
        url: $(this).attr('href'),
        success: function(responseText) {
            if($(event.target).text() == 'Add as friend'){
                $(event.target).attr("href", "<?php echo Config::get('URL'); ?>friends/cancel_request/" + $(event.target).attr('id') + "/<?php echo System::escape(Session::get('token')); ?>");
                $(event.target).text('Cancel Request');
                $(event.target).removeClass("add_friend btn btn-success").addClass("cancel_request btn btn-info");                  
            }else{
                $(event.target).attr("href", "<?php echo Config::get('URL'); ?>friends/addfriend/" + $(event.target).attr('id') + "/<?php echo System::escape(Session::get('token'));?>");
                $(event.target).text('Add as friend');  
                $(event.target).removeClass("cancel_request btn btn-info").addClass("add_friend btn btn-success");                  
            }
        }
    });
    return false;
});
<!-- End Like a status -->

which ofcourse run the following link:

<a class="add_friend btn btn-success" id="<?php echo System::escape($likes->timeline_likes_id); ?>" href="<?php echo Config::get('URL'); ?>friends/addfriend/<?php echo System::escape($likes->user_id).'/'.System::escape(Session::get('token')); ?>"><?php echo System::translate("Add as friend"); ?></a>

Their token is: System::escape(Session::get('token')); ?> which will only load on page refresh which defauts my reason for using ajax

  • 写回答

2条回答 默认 最新

  • duancongjue9202 2015-08-26 22:39
    关注

    To update the value in your javascript, you'll want to modify your js somewhat.

    Something like this should get you on the right track

    <!-- accept, reject, add and cancel friend requests -->
    $('.add_friend').click(function(event) { // bind function to submit event of form
        event.preventDefault();
        var token = <?php echo System::escape(Session::get('token'));?>;
        $.ajax({
            url: $(this).attr('href'),
            success: function(responseText) {
                if($(event.target).text() == 'Add as friend'){
                    $(event.target).attr("href", "<?php echo Config::get('URL'); ?>friends/cancel_request/" + $(event.target).attr('id') + "/" + token);
                    $(event.target).text('Cancel Request');
                    $(event.target).removeClass("add_friend btn btn-success").addClass("cancel_request btn btn-info");                  
                }else{
                    $(event.target).attr("href", "<?php echo Config::get('URL'); ?>friends/addfriend/" + $(event.target).attr('id') + "/" + token);
                    $(event.target).text('Add as friend');  
                    $(event.target).removeClass("cancel_request btn btn-info").addClass("add_friend btn btn-success");                  
                }
                token = responseText;
            }
        });
        return false;
    });
    <!-- End Like a status -->
    

    Now all you have to do is modify the form handler at $(this).attr('href') to echo the new session token. The JS will set the token value to the new token and use that in subsequent requests.

    What this is doing is placing your token in a variable that can be updated after each call so that you are able to change it after a successful request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line