最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to secure global scope variables and prevent users from accessing it via console - Stack Overflow

programmeradmin0浏览0评论

I have some special variables declared in the global scope. Now am a bit concern because the data am storing in these variable are the heart of the application am working on.

Upon trying to access it via console I found it was very easy and that is not fully secured.

How do I make it hard to access it and secure these variables still in the global scope ?

code:

`//declare special variables global scope and use it throughout the application when needed

var recipientsInfo          = [],
    recipients              = [],
    buddyList               = [],
    webMessengerDataAr      = [],
    __ActiveConlog_data     = [],
    activeConArray          = [];

Thank you.

I have some special variables declared in the global scope. Now am a bit concern because the data am storing in these variable are the heart of the application am working on.

Upon trying to access it via console I found it was very easy and that is not fully secured.

How do I make it hard to access it and secure these variables still in the global scope ?

code:

`//declare special variables global scope and use it throughout the application when needed

var recipientsInfo          = [],
    recipients              = [],
    buddyList               = [],
    webMessengerDataAr      = [],
    __ActiveConlog_data     = [],
    activeConArray          = [];

Thank you.

Share Improve this question asked Dec 24, 2016 at 18:17 james Odurojames Oduro 6731 gold badge6 silver badges23 bronze badges 6
  • Store them inside function? – kind user Commented Dec 24, 2016 at 18:19
  • Possible duplicate of avoid Javascript variable modification from browser console – Santhosh Kumar Commented Dec 24, 2016 at 18:20
  • You can't prevent access if they are in global scope, they need to be in a closure – charlietfl Commented Dec 24, 2016 at 18:30
  • It must be said that this will not make your code more secure. – lonesomeday Commented Dec 24, 2016 at 18:44
  • 1 Users can use a debugger to access and modify your variables. You can't prevent it. – Oriol Commented Dec 24, 2016 at 18:46
 |  Show 1 more ment

2 Answers 2

Reset to default 5

It's quite simple: if you have variables within your code and you want to store them safely, you have to basically drop them into self invoking anonymous function which will work like a closure.

(function() {
    // just drag your whole code from your script.js and drop it here
})();

Read more at: https://developer.mozilla/en-US/docs/Web/JavaScript/Closures

Try to bundled your code inside IIFE

(function() {
    // your variable goes here
})();

then your variable are no longer in global scope, it's create new scope.

if you don't know what IIFE is take a look at this page IIFE. Also a useful pattern for avoiding global scope based on IIFE called module pattern might help you.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论