doumianfeng5065 2014-09-22 15:16 采纳率: 0%
浏览 250

如果令牌不匹配,则阻止表单提交?

I'm currently working to protect a form I created in PHP/HTML/Javascript from CSRF attacks. I have a class that does two things - one function that generates and returns a random token, and another function that checks if the token exists within the current session and if the tokens are equal. The first one I'll leave out because all it does is generate a token.

    public static function checkToken($token) {
        if(!empty($_SESSION['token']) && $token === $_SESSION['token'])) {
            unset($_SESSION['token']);
            return true;
        } 
    return false;
    }

On the main page where the form is - I start a session and check if the name of my submit button is equal to its value (which it gains upon submission):

    if($_POST['submit_app'] == "Submit" && $_POST['token']) {
        if(Token::checkToken($_POST['token'])) {
            echo 'Token OK';
    } else {
        die();
        }
    }

I have purposely altered the token in the Web Development Tools in Google Chrome(F12), yet the form still seems to proceed to my processing page (written in php). Would using die(); not prevent the other processing form from.. processing? How can I stop the data from being submitted if the tokens do not match?

I know PHP is a server-side language so I may have to do something else to prevent submission, but I'm trying to think of a way I can kill the script before it ever process the data on the form if the tokens aren't the same.

  • 写回答

1条回答 默认 最新

  • douhuang3833 2014-09-22 18:11
    关注

    As you said yourself PHP is a server-side script which can't prevent submitting a form. A form is submitted client-side so, the only way to prevent this is with a client-side script, like javascript.

    A CSRF attack is done by embedding a external form in an iframe. What you can do server-side is prevent this behaviour. You can accomplish this with the .htaccess X-Frame-Options. To prevent CSRF attack you can use:

    Header set X-Frame-Options SAMEORIGIN
    

    Just add it to your .htaccess file. SAMEORIGIN setting allows pages to be served in a frame of a page on the same website. If an external site attempts to load the page in a frame the request will be denied. You can also use the DENY option here, which should be used if you never intend for your pages to be used inside of a frame.

    Note that this won't work for old browsers. Browser compatibility:

    • Internet Explorer 8+
    • Opera 10.50+
    • Safari 4+
    • Chrome 4.1.249.1042+
    • Firefox 3.6.9+ (or earlier with NoScript)
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP