2017-01-09 6 views
0

얼마 전에 Slim 2를 사용하여 앱을 만들었으며 각도를 추가하려고합니다. 지금까지 잘 진행되었지만, Angular가 모든 게시물 요청을 처리하고 있기 때문에 더 이상 CSRF 보호 기능을 사용할 수 없습니다. 아래는 내가 일하는 미들웨어입니다.Slim 앱에 Angular XSRF 추가하기 -이게 뭡니까?

<?php 

namespace Cache\Middleware; 

use Exception; 
use Slim\Middleware; 

class CsrfMiddleware extends Middleware { 
protected $key; 

public function call() { 

    $this->key = $this->app->config->get('csrf.key'); 

    $this->app->hook('slim.before', [$this, 'check']); 

    $this->next->call(); 
} 

public function check() { 
    if (!isset($_SESSION[$this->key])) { 
     $_SESSION[$this->key] = $this->app->hash->hash($this->app->randomlib->generateString(128)); 
    } 

    $token = $_SESSION[$this->key]; 

    if (in_array($this->app->request()->getMethod(), ['POST', 'PUT', 'DELETE'])) { 
     $submittedToken = $this->app->request()->post($this->key) ?: ''; 

     if (!$this->app->hash->hashCheck($token, $submittedToken)) { 
      throw new Exception('CSRF token mismatch'); 
     } 
    } 

    $this->app->view()->appendData([ 
     'csrf_key' => $this->key, 
     'csrf_token' => $token 
    ]); 
} 

}

나는 각도가 자동으로 XSRF 토큰 이름 토큰을 찾고 X-XSRF 토큰과 같은 헤더에 추가 것을 알고있다. 아래의 미들웨어를 수정하여 올바른 값을 쓰고 읽고 비교할 수있는 방법은 무엇입니까?

편집

이에

$submittedToken = $this->app->request()->post($this->key) ?: ''; 

: 내가 옳다 경우

$submittedToken = $this->app->request->headers->get('X-XSRF-TOKEN') ?: ''; 

다시보고와 슬림 한 문서를 확인한 후, 나는 선을 변경 , $ submittedToken에 헤더에 X-XSRF-TOKEN으로 전달 된 값을 할당합니다. 미들웨어 "CSRF token mismatch"의 메시지와 함께 예외가 발생합니다. 이것은 진행 상황을 느낍니다. PHP 코드는 이제 의미하는 곳 바로 아래

app.controller('itemsCtrl', ['$scope', '$http', function($scope, $http) { 

    // Initailize object when the page first loads 
    $scope.getAll = function() { 
     $http.post('/domain.com/admin/getNames').success(function(data) { 
      $scope.names = data; 
     }); 
    } 

편집 : 아래 관련 코너입니다. 나는 이것이 효과가 있다고 생각한다. 양식을 제출하기 전에 쿠키를 제거하거나 $ 토큰의 값을 변경하면 예상되는 CSRF 오류가 발생했습니다. 나는 여러 명의 사용자가있을 때 어떤 일이 생길지 약간 걱정하고 있습니다. 나는 아직 그것을 테스트하지 않았다. 이 개정에 따라 보호 기능이 정상적으로 작동합니까? Angular docs for $http Cross Site Request Forgery (XSRF) Protection에서

<?php 

namespace Cache\Middleware; 

use Exception; 
use Slim\Middleware; 

class CsrfMiddleware extends Middleware { 
protected $key; 

public function call() { 

    $this->key = $this->app->config->get('csrf.key'); 

    $this->app->hook('slim.before', [$this, 'check']); 

    $this->next->call(); 
} 

public function check() { 
    // if (!isset($_SESSION[$this->key])) { 
    if (!isset($_SESSION[$this->key])) { 
     // $_SESSION[$this->key] = $this->app->hash->hash($this->app->randomlib->generateString(128)); 
     $this->app->setcookie($this->key, $this->app->hash->hash($this->app->randomlib->generateString(128))); 
    } 

    // $token = $_SESSION[$this->key]; 
    if(isset($_COOKIE[$this->key])) { 
     $token = $_COOKIE[$this->key]; 
    } 

    if (in_array($this->app->request()->getMethod(), ['POST', 'PUT', 'DELETE'])) { 
     // $submittedToken = $this->app->request()->post($this->key) ?: ''; 
     $submittedToken = $this->app->request->headers->get('X-XSRF-TOKEN') ?: ''; 

     if (!$this->app->hash->hashCheck($token, $submittedToken)) { 
      throw new Exception('CSRF token mismatch'); 
     } 
    } 
} 
} 

답변

0

:

실행시 중 $ httpProvider.defaults 설정 시간에, $ http.defaults의 xsrfHeaderName 및 xsrfCookieName 속성을 사용하여 지정할 수 있습니다 헤더의 이름, 또는 요청 별 구성 객체.

다른 쿠키 이름/헤더 이름을 사용하도록 쿠키를 변경하려면 해당 값을 변경하십시오.

+0

다른 곳에서도 구현 된 것을 보았습니다. 지금은 csrf.key 이름을 XSRF-TOKEN으로 변경하여 Angular가 선택하도록했습니다. 그것이 작동하는지 테스트하는 방법을 모르겠습니다. 또한 헤더 이름을 읽는 미들웨어를 얻는 방법을 모르십니까? – user2530671