【问题标题】:Filter CSRF token to secure against CSRF过滤 CSRF 令牌以防止 CSRF
【发布时间】:2015-01-27 02:49:57
【问题描述】:

我正在关注这篇文章http://blog.neoxia.com/laravel4-and-angularjs/ 来设置和过滤 CSRF 令牌。 我能够让它在本地服务器上运行,但是在我现场部署并测试它之后,我不断收到“状态代码 418”。有什么想法吗?

下面是我的代码:

Route.php:

// Route to filter CSRF
Route::filter('serviceCSRF',function(){
if (Session::token() != Request::header('csrf_token')) {
    return Response::json([
        'message' => 'Security token doesn\'t match, possible CSRF attack.'
    ], 418);
}
});

// Route for authentication
Route::group(['prefix' => 'api/auth', 'after' => 'allowOrigin'], function() {

Route::get('check', [
    'as'=>'check_auth_path',
    'uses'=>'SessionsController@check'
]);

Route::post('login', [
    'as'=>'login_path',
    'uses'=>'SessionsController@login'
]);

Route::get('sentryLogout', [
    'as'=>'logout_path',
    'uses'=>'SessionsController@logout'
]);
});

会话控制器:

class SessionsController extends \BaseController {

public function __construct() {
    $this->beforeFilter('serviceCSRF');
}
...

app.js:

var xhReq = new XMLHttpRequest();
xhReq.open("GET", "//" + window.location.hostname + "/api/csrf", false);
xhReq.send(null);

app.constant("CSRF_TOKEN", xhReq.responseText);

app.run(function ($window, $couchPotato, $rootScope, $state, $stateParams, $http, CSRF_TOKEN) {
    app.lazy = $couchPotato;
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
    // editableOptions.theme = 'bs3';

    $http.defaults.headers.common['csrf_token'] = CSRF_TOKEN;

    // watch for location path change
    $rootScope.$on("$locationChangeStart",function() {

    ....

【问题讨论】:

  • 伙计们,为什么所有的反对票都投了?
  • 状态码是个愚人节玩笑...
  • 我知道。问题是为什么它适用于本地而不适用于生产?
  • 你被否决了,因为你没有提供你的代码,没有想要的结果,并且向我们提供了一个虚假的错误......
  • @Richard:对不起,刚刚编辑并添加了我的代码。

标签: php angularjs laravel-4 csrf csrf-protection


【解决方案1】:

解决了。原因是 Request::header();无法用下划线(csrf_token)解释参数。所以在我改成之后

$http.defaults.headers.common['csrfToken'] = CSRF_TOKEN;

它有效!

感谢来自 (https://github.com/laravel/framework/issues/1655#issuecomment-20595277) 的 rvs1977

【讨论】:

    猜你喜欢
    • 2013-02-25
    • 2019-02-23
    • 2015-09-28
    • 1970-01-01
    • 2014-01-02
    • 2011-08-16
    • 2022-12-19
    • 2015-03-12
    • 1970-01-01
    相关资源
    最近更新 更多