2012-06-21 4 views
1

CakePHP는 SecurityComponent의 requireSecure 함수를 가지고 있습니다. 저는 신용 카드 번호 같은 민감한 정보를 전달할 때 SSL을 강제하기 위해 이것을 사용하고 있습니다.CakePHP의 requireCecure와 반대

질문 :

  • 는 requireNonSecure 기능이 있습니까?
  • requireNonSecure 함수가 없다면 원래 파일을 수정하지 않고도 CakePHP의 코어 파일에 함수를 추가/확장 할 수 있습니까?

일부 페이지에는 내 도메인 이름에서만 재생할 수있는 비디오가 포함되어 있기 때문에 requireNonSecure 기능이 필요합니다. SSL을 사용하면 비디오 호스팅 서비스가 도메인 이름을 인식하지 못해 비디오를 재생할 수 없습니다.

이것은 컨트롤러으로, beforeFilter의 코드 중 일부는 다음

function beforeFilter() { 
    parent::beforeFilter(); 

    $this->Security->validatePost = false; // disable CSRF protection 
    $this->Security->blackHoleCallback = 'forceSSL'; 
    $this->Security->requireSecure('pay', 'index'); 

    $this->Auth->allow('index'); 
} 

이이 용액에 추가 SecurityComponent app_controller.php

function forceSSL() { 
    $redirect = ''; 
    if (!empty($this->params['url']['redirect'])) { 
     $redirect = '?redirect=' . $this->params['url']['redirect']; 
    } 

    $this->redirect('https://' . rtrim(env('SERVER_NAME'), '/') . $this->here . $redirect); 
} 

답변

1

beforeFilter before :

app_controller.php에서

function beforeFilter() { 
    parent::beforeFilter(); 

    // Require non secure (http) for video action 
    $this->requireNonSecure('video'); 

    // ... other code here 

} 

:

function requireNonSecure() { 
    $requireNonSecure = array_map('strtolower', func_get_args()); 

    if (in_array(strtolower($this->action), $requireNonSecure) || $requireNonSecure == array('*')) { 
     if ($this->RequestHandler->isSSL()) { 
      $this->redirect('http://' . rtrim(env('SERVER_NAME'), '/') . $this->here); 
      return; 
     } 
    } 
} 
제어기에서

0

의 콜백이다. 작동해야하지만 requireSecure와 requireNonSecure가 모두 설정된 경우 리디렉션 루프의 위험이 있습니다.

SecurityPlusComponent :

class SecurityPlusComponent extends SecurityComponent { 

    /** 
    * List of actions that do not require an SSL-secured connection 
    * 
    * @var array 
    * @access public 
    * @see SecurityPlusComponent::requireNonSecure() 
    */ 
    var $requireSecure = array(); 

    /** 
    * Component startup. All security checking happens here. 
    * 
    * @param object $controller Instantiating controller 
    * @access public 
    */ 
     function startup(&$controller) { 
      $this->_action = strtolower($controller->action); 
      $this->_methodsRequired($controller); 
      $this->_secureRequired($controller); 
      $this->_nonSecureRequired($controller); 
      $this->_authRequired($controller); 
      $this->_loginRequired($controller); 

      $isPost = ($this->RequestHandler->isPost() || $this->RequestHandler->isPut()); 
      $isRequestAction = (
       !isset($controller->params['requested']) || 
       $controller->params['requested'] != 1 
      ); 

      if ($isPost && $isRequestAction && $this->validatePost) { 
       if ($this->_validatePost($controller) === false) { 
        if (!$this->blackHole($controller, 'auth')) { 
         return null; 
        } 
       } 
      } 
      $this->_generateToken($controller); 
     } 

    function requireNonSecure() { 
     $this->_requireMethod('NonSecure', func_get_args()); 
    } 

    /** 
    * Check if access requires non secure connection (http) 
    * 
    * @param object $controller Instantiating controller 
    * @return bool true if secure connection required 
    * @access protected 
    */ 
    function _nonSecureRequired(&$controller) { 
     if (is_array($this->requireNonSecure) && !empty($this->requireNonSecure)) { 
      $requireNonSecure = array_map('strtolower', $this->requireNonSecure); 

      if (in_array($this->_action, $requireNonSecure) || $this->requireNonSecure == array('*')) { 
       if ($this->RequestHandler->isSSL()) { 
        if (!$this->blackHole($controller, 'nonSecure')) { 
         return null; 
        } 
       } 
      } 
     } 
     return true; 
    } 
} 

수정 app_controller의 forceSSL 기능 :

function securityBlackhole($type) { 
    $redirect = ''; 
    if (!empty($this->params['url']['redirect'])) { 
     $redirect = '?redirect=' . $this->params['url']['redirect']; 
    } 

    // Force http (non-SSL) 
    if($type == 'nonSecure') { 
     $this->redirect('http://' . rtrim(env('SERVER_NAME'), '/') . $this->here . $redirect); 

    // Force https (SSL) 
    } else { 
     $this->redirect('https://' . rtrim(env('SERVER_NAME'), '/') . $this->here . $redirect); 
    } 
} 

는 컨트롤러 같이 호출 할 것이다 : 용액에 기능을 추가하는 것

function beforeFilter() { 
    parent::beforeFilter(); 

    $this->SecurityPlus->validatePost = false; // disable CSRF protection 
    $this->SecurityPlus->blackHoleCallback = 'securityBlackhole'; 
    $this->SecurityPlus->requireSecure('pay', 'index'); 
    $this->SecurityPlus->requireNonSecure('video'); 

    $this->Auth->allow('index'); 
}