-27

In the code below I want to hide the contents of the key(speaker) key being viewed by browser console.

var App_Version = 1; 
var App_id = 35; 
var Speaker = "password";
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Visakh B Sujathan
  • 229
  • 1
  • 4
  • 25

8 Answers8

8

If var Speaker = "password"; is hardcoded somewhere in your client code, you are out of luck. See Password encryption at client side, and Howto hide Credentials in a pure Javascript HTML Web App and Is it worth hashing passwords on the client side because everyone will say not to hide/obfuscate a password on the client-side.


However, If you really, really just want to remove the password altogether from your client code, then use a server-side script to "proxy" your AJAX request and silently add the password as a POST parameter (for example) in transit to the true destination. See Angular REST API security


If you are adamant about using some kind of crypto on the client-side, I found angularjs-crypto which is an "AngularJS module for decryption/encryption of JSON in HTTP requests/responses". It is based on crypto-js. I still strongly against taking a hardcoded password and encrypting it on the client-side, however.

Derek Wang
  • 10,098
  • 4
  • 18
  • 39
Drakes
  • 23,254
  • 3
  • 51
  • 94
1

Well you can't "hide" javascript.

You can encrypt it with an obfuscator but most people can decrypt it using a beautyfier. However check this out:

OBFUSCATOR

Joakim M
  • 1,793
  • 2
  • 14
  • 29
1

There is no way in Javascript to hide the contents of a variable without unsetting the variable, which is of no use to you if you need to reference whatever that value was later. A few options to choose from, depending on exactly what you need would be this:

Hashing the value

You could has the value with MD5 or SHA. This way the user doesn't know what the value is but you can still send it off to the server and store it that way

Store the value on the server

You could store the value on the server and reference it with some sort of key. That way the user never sees the actual value, they are just checking the server to see if the value they have matches what the server has.

If this is a password that is being generated and we never want the user to see it, your best bet is to handle all of that on there server, this way there is no chance the user will ever even have the value on the client side.

Mike Hamilton
  • 1,519
  • 16
  • 24
1

Javascript code is considered temperable so you can't really hide a password in your code. The best practice is to use oAuth2 which uses temporary tokens(to defer from passwortds) that in conjunction with things like CSRF can make you client side code more secure. Here's one library that can help you do that in JS. It does require that on your server side when accessing resources you need to validate that token (preferably on every call). To some it up if you need to store a password on the browser in order to log into a server - don't. Let the user log in and the afterwards use the token issues by the server or use a third party login and use the token you got from the third party authentication server.

Gabriel Kohen
  • 4,166
  • 4
  • 31
  • 46
1

Nice question even I had came across such situation where my API key and authentication token was visible at client side.Obfuscating a client side Javascript file is best option. You can use UglifyJS for obfuscating the clientside code. And also there must be permissions on the server side for creating and deleting objects.

kailash yogeshwar
  • 836
  • 1
  • 9
  • 26
0

although it's not fully clear from the small code provided, protecting it from the browsers console is straight forward IF we are only talking about someone console.logging the value (as your question seems to hint).

you can put it inside a closure, something like a revealing/modular pattern:

var app = (function () {

    var Speaker = "something";
    // only available on app declaration as the function is immediately called
    return // some object/function protecting the speaker value
})();

console.log(Speaker); // gives an error
console.log(app.Speaker); // gives an error

This of course only stops someone using basic dev tools like console.log, it wont stop someone adding breakpoints. the nature of javascript I'm afraid

atmd
  • 7,430
  • 2
  • 33
  • 64
  • i mean using browser inspect method we can view the js code – Visakh B Sujathan Jun 01 '15 at 12:51
  • I see, then as others have said, no, it can't be hidden. if the browser can't see it then it can't run it. having said that, anything secure like a password should/would be encrypted anyway. a server solution is the better method here. maybe if the problem was described we can better understand the purpose – atmd Jun 01 '15 at 12:52
  • in the case of using angularjs in frontend how can secure it – Visakh B Sujathan Jun 01 '15 at 12:55
  • secure what? what are you trying to secure? the whole code? if you dont want people to see the code thats created the application then flash is a good option, or javaFX, but certainly not javascript – atmd Jun 01 '15 at 12:56
  • but there are many tools that can get to that information regardless of the javascript code. if you are trying to protect post information then that is a very different question. are you on a ssl? – atmd Jun 03 '15 at 07:22
0

using jwt we can encrypt data.try this link

Visakh B Sujathan
  • 229
  • 1
  • 4
  • 25
-1

Well you can't "hide" javascript. but you can encrypt them using OBFUSCATOR and other tools

Visakh B Sujathan
  • 229
  • 1
  • 4
  • 25