简体   繁体   English

将 JavaScript 文件作为文件的离线版本存储在 localStorage 中是否存在安全风险?

[英]Are there security risks storing JavaScript files in localStorage as an offline version of the file?

I have an app that reads the contents of example.js using an AJAX call to example.php , then saves example.js to a variable, localStorage.exampleJS , every time the user opens the app on their phone.我有读取的内容的应用程序example.js使用AJAX调用example.php ,然后保存example.js一个变量, localStorage.exampleJS ,每次用户打开自己的手机上的应用程序的时间。 The AJAX call uses the following jQuery: AJAX 调用使用以下 jQuery:

$.ajax({
    type: "POST",
    url: "https://example.com/example.php",
    success: function(response){
        localStorage.exampleJS = response;
    },
});

example.php contains the following: example.php包含以下内容:

<?php
    echo file_get_contents('example.js');
?>

In the event that the the user doesn't have an internet connection, I run the offline version using the following jQuery:如果用户没有互联网连接,我会使用以下 jQuery 运行离线版本:

if(navigator.onLine){
    $("head").append("<script src='https://example.com/example.js'></script>");
}
else{
    $("head").append("<script>" + localStorage.exampleJS + "</script>");    
}

I know this could potentially have security risks.我知道这可能存在安全风险。 Is this a bad practice?这是一种不好的做法吗? Are there security risks doing saving an offline version this way?以这种方式保存离线版本是否存在安全风险? Is there a better way to do this?有一个更好的方法吗? Thanks!谢谢!

I think there is no harm storing file in local-storage, but have to keep following things in your mind.我认为将文件存储在本地存储中没有坏处,但必须牢记以下事项。

  • File size should not be more than the local-storage size.文件大小不应大于本地存储大小。 at any point of time you could utilized full local-storage then you would not be able to store more file.在任何时候您都可以使用完整的本地存储,那么您将无法存储更多文件。
  • Ensure that your all files are uglify and minify, since it will help you in security and space optimization.确保您的所有文件都变得丑陋和缩小,因为这将有助于您进行安全和空间优化。 As uglfy is not human readable so anyone try to read it he is not able to read as file is in uglify form, and minify helps to reduce the file size.由于 uglfy 不是人类可读的,因此任何人尝试阅读它都无法阅读,因为文件处于 uglify 形式,而 minify 有助于减小文件大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM