douzao1119 2012-07-27 19:05
浏览 79
已采纳

通过json查看模型时保持安全

I decided to use knockout.js for my web application and have some concerns regarding security.

The flow of data is as follows:

  • user requests controller via url
  • controller gathers together required information, sends to view as json string
  • json string is saved to dom to allow my javascript code to access it
  • json is loaded into knockout view model inside the $(document).ready

My issue is that the json string is clearly viewable by users by just clicking 'View Source' and this worries me because I'm aware this can easily be changed client-side but I'm not fully sure of the implications.

Here is some example code to illustrate my point. First, the controller:

 function view($id = null)
 {
    //other processing...

    $data = array();
    $data['json'] = $this->get_profile_json($id);
    $this->load->view('profile_page',$data);
 }

The profile page view

<script type="text/javascript">
    window.profile_json = "<?php echo $json; ?>";
</script>
<script type="text/javascript" src="<?php echo site_url('assets/js/profile_page.js'); ?>"></script>
<!-- The profile page below... -->

The profile page javascript

var vm = new ViewModel(profile_json); //load the json into view model
ko.applyBindings(vm);

Now I understand I can achieve the same goal by loading the json from the javascript code just before creating the view model, using $.getJSON for example.

However, someone with a developer tools extension on their browser could also see (and possibly edit?) this data too. This is a particular problem is some of that data contains things like permissions flags and so on.

My question is, how do you ensure data passed down to your view model is unable to be tampered with?

  • 写回答

1条回答 默认 最新

  • doudou890510 2012-07-27 19:14
    关注

    There is no way to ever guarantee the safety of your information from the client side. This is why it is always important to do server side checks on the information, and then send back your response on whether the information is usable or not.

    For some security checks you should use the following:

    1) Strip_tags($_POST['key'])
    2) Check to see if the value is numeric (if its a number)
    3) Check to see if it only has alphabet and numbers (if this is all that's allowed)
    ...and so on
    

    The client side information to be passed to your back-end can always be tampered with, which is why it is important to lock it down as much as possible and if any information does not appear to be valid then you should immediately throw an error to the client and ask them to try again :)

    Assuming the back-end data is safe, then the front-end data should never have a problem.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看