CodeIgniter应用程序中的URL安全性


URL Security in CodeIgniter Application

免责声明:我是web开发的新手。

场景:我使用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');
    }

使用以下逻辑

 <?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 );
            }
    }
    ?>      

这就是访问权限的来源。基本上,在加载页面之前,从表中检查用户(owner_id)是否真的与正在浏览的用户共享。如果没有,只显示一条错误消息,否则正常显示。

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

您可以使用CodeIgniters加密库。

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

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

对于邀请用户检查,只需从数据库中获取邀请用户的id,并使用in_array()检查特定登录用户是否被邀请,然后根据需要重定向。。。

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

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

CodeIgniter论坛上有关于建立可作为插件使用的身份验证/授权系统的资源。