2012-01-08 89 views
3

免责声明:我是网络开发新手。CodeIgniter应用程序中的URL安全

场景:我已经构建了一个使用CodeIgniter的应用程序,最好将其描述为事件日历。应用程序中有一个共享功能,可让您与其他人共享您的活动日历。登录后,用户可以前往共享页面,并从已与其共享活动日历的用户列表中进行选择。目前,当用户选择谁分享了他们的活动日历与他们的人的名字,下面的URI产生:

http://example.com/folder/controller/method/id 

id部分是在用户的数据库谁共享了该owner_id他们与个人日历。

问题:很容易将URL的id部分更改为数据库中另一个用户的owner_id。这允许任何人访问未授权分享其活动日历的个人的活动日历。

问题:有什么方法可以解决这个安全漏洞?请让我知道是否还有其他需要提供的内容,或者以更清晰的方式解释。预先感谢您的时间和精力。

型号

class Shares_model extends crud_model { 

    public function __construct() 
    { 
     parent::__construct(); 

     $this->pk = 'id'; 
     $this->table_name = 'shares'; 
    } 

    public function get($shared_to_user_id) 
    { 
     $this->db->where('shared_to_id', $shared_to_user_id); 
     $ids = parent::get_all(); 

     $users = array(); 

     foreach ($ids as $id) 
     { 
      $users[$id->owner_id]['owner_id'] = $id->owner_id; 
      $users[$id->owner_id]['owner_first_name'] = $id->owner_first_name; 
      $users[$id->owner_id]['owner_last_name'] = $id->owner_last_name; 
     } 

     return $users; 
    } 
} 

查看

<div class="panel"> 
    <h4>Shared Planners</h4> 
     <ol> 
      <?php foreach($sharers as $s): ?> 
      <li><a href="<?php echo base_url('user/shared/view/'.$s['owner_id']) ?>"><strong><?php echo $s['owner_first_name']." ".$s['owner_last_name'] ?></strong></a></li> 
      <?php endforeach; ?> 
     </ol> 
</div> 

控制器

class Shared extends Common_Auth_Controller { 

    private $end_user; 

    public function __construct() 
    { 
     parent::__construct(); 

     $this->end_user = $this->ion_auth->user()->row(); 
     $data['end_user'] = $this->end_user; 
     $this->load->vars($data); 

     $this->load->model('events_model', 'events'); 
    } 

    public function index() 
    { 
     $title['title'] = 'Shared'; 

     $this->load->model('shares_model','shares'); 

     $data['sharers'] = $this->shares->get($this->end_user->id); 

     $this->load->view('public/head_view', $title); 
     $this->load->view('user/header_view'); 
     $this->load->view('user/shared_view', $data); 
     $this->load->view('user/footer_view'); 
    } 
+0

为什么要为您的活动/日历授权表...? – Peter 2012-01-20 09:02:09

回答

5

使用下面的逻辑

<?php 
      // Check if user is logged in 
      if (!$this->ion_auth->logged_in()) 
      { 
       //Not logged in , so redirect them to login page 
       redirect('account/login', 'refresh'); 
      } 

      else{ 
      // So the user is logged in 
      // Get the id of the currently logged in user (The user who is trying to view the page) 
      $current_user = $this->ion_auth->get_user(); 
      $current_userid = $current_user->id; 


      // you need an array of users who have been invited to that event by the event creator 
      // As you mentioned you are storing the users who have been invited in db, get the ids to an array 

      $invited_users = getIdsOfusers(); 

      if (in_array($current_userid, $invited_users)) { 
       // Yes, The user who is trying to view the page has access 
       // you can show him the respective view 
      } 
      else { 
       // No, The user who is trying to view the page Doesn't have access 
       show_error('You dont have access !' ,500); 
      } 
    } 

    ?>  
1

可能您保持一个表跟踪用户可以访问的所有日历。然后,当用户尝试访问日历时,您要做的第一件事就是检查是否允许他们查看该日历。

+0

数据库已将谁拥有日历以及谁有权查看该日历。我需要的代码可以验证此过程,或者保护URL以防止访问其他用户的日历。 – imlouisrussell 2012-01-08 18:57:42

4

这是访问权限进入的地方。基本上在加载页面之前,请从表格中检查用户(owner_id)是否实际上与正在浏览的用户共享。如果没有,只显示一条错误消息或者正常显示。

1

如果你希望登录的用户是唯一能看到他的日历的人,那么你不需要通过url传递id。只需从会话中获取用户标识,然后使用该标识获取日历数据。

如果你想让任何授权/登录的用户看到任何用户的日历,你可以简单地检查他/她是否登录。如果未登录,则使用redirect()重定向到错误页面。

而且受邀用户检查,只是从数据库中读取邀请用户的ID,并使用in_array()检查用户的特定记录的邀请与否,然后重定向,只要你想......

1

有确保在安全系统中正确访问页面的两个阶段。

  1. 身份验证 - 您必须确定用户是他们说他们是谁。这通常使用cookie来确保它们发送的内容与登录用户的会话相匹配。这个cookie将是您第一次登录时生成的一个cookie,并将该令牌的副本存储在数据库中。每次用户请求页面时,都要进行检查以确保当前的PHP SESSION属于已登录的用户,并且他们仍然登录。
  2. 授权 - 确定AUTHENTICATED用户实际上允许查看它们是什么试图查看。知道这个人真的是他们自称的人,这很好,很好,但这并不意味着平均的Joe可以访问整个政府数据库,因为他只能看到他的内容。这是你问题的关键部分 - 在URI中传递的ID实际上是否属于已验证的用户?如果是这样,显示它。否则,你的好朋友403处于待命状态。

CodeIgniter论坛上有关于设置可用作插件的认证/授权系统的资源。