Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learn Security: XSRF #15

Open
drodata opened this issue Jul 21, 2015 · 0 comments
Open

Learn Security: XSRF #15

drodata opened this issue Jul 21, 2015 · 0 comments

Comments

@drodata
Copy link
Owner

drodata commented Jul 21, 2015

本节举了一个 CSRF 攻击的简单例子。我照例在 blog application 上模拟了一下,结果又中招(第一次是 XSS 攻击)了。我在其它应用下新建一个 a.php 页面,内容如下:

<img src="http://localhost/blog/pk/clip/delete?id=777" />

blog Session 有效的前提下,访问 a.php 后,blog 内 ID 为 777 的 Clip 已经被删除。

这种 bug 之所以会发生,是因为没有遵循 CSRF 攻击防范措施的第一条—— GET 请求只用于获取数据,不能用于修改数据。现在终于体会到为什么 Yii 内置的 blog demo 中删除 post (source code) 时要加上下面这样一个判断了:

// PostController.php
public function actionDelete()
{
    if (Yii::app()->request->isPostRequest)
    {
        // delete
    }
    else
        throw new CHttpException(400,"Invalid request.");
}

就是为了防止 CSRF 攻击。

Resolution

修改每一个 Controllers 中的 actionDelete() methods 未免太麻烦,我立刻想到 #5 中新建的 LoginFilter, 完全可以依葫芦画瓢,再创建一个判断是不是 POST request 的 filter 嘛。

不同于 LoginFilter 应用于 controller 内的所有 actions, PostRequestCheckFilter 仅针对 actionDelete, 我们需要在 filters() 内配置 filter 时,用到一些特殊符号:

public function filters()
{
    return array( 
        'accessControl',
        array('ext.filter.precheck.PostRequestCheckFilter + delete'), // only to 'delete' action
    );

}

这样我们就杜绝了通过 GET 请求修改数据的可能。

drodata added a commit that referenced this issue Jul 21, 2015
Create an filter to prevent delete action via GET way.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant