gpt4 book ai didi

php - 将 json 传递给 View 模型时保持安全

转载 作者:搜寻专家 更新时间:2023-10-31 20:47:37 25 4
gpt4 key购买 nike

我决定为我的 Web 应用程序使用 knockout.js 并且对安全性有一些担忧。

数据流向如下:

  • 用户通过 url 请求 Controller
  • controller收集所需信息,以json字符串形式发送给view
  • json 字符串被保存到 dom 以允许我的 javascript 代码访问它
  • json 被加载到 $(document).ready 里面的 knockout view model

我的问题是用户只需单击“查看源代码”即可清楚地看到 json 字符串 这让我很担心,因为我知道这可以很容易地在客户端更改,但我不是 完全确定其含义。

下面是一些示例代码来说明我的观点。一、 Controller :

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

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

个人资料页面 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... -->

个人资料页面javascript

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

现在我明白我可以通过从之前的 javascript 代码加载 json 来实现相同的目标创建 View 模型,例如使用 $.getJSON

但是,在浏览器上安装了开发者工具扩展的人也可以看到(并可能编辑?)这个数据也是。这是一个特殊的问题,其中一些数据包含诸如权限标志之类的内容等等。

我的问题是,如何确保传递到 View 模型的数据不会被篡改?

最佳答案

客户端永远无法保证您的信息安全。这就是为什么对信息进行服务器端检查,然后发回您对信息是否可用的响应总是很重要的原因。

对于某些安全检查,您应该使用以下内容:

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

要传递到后端的客户端信息总是可以被篡改,这就是为什么尽可能锁定它很重要,如果任何信息似乎无效,那么您应该立即抛出给客户一个错误,让他们再试一次:)

假设后端数据是安全的,那么前端数据应该不会有问题。

希望这对您有所帮助!

关于php - 将 json 传递给 View 模型时保持安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11694131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com