I'm developing a web app which needs good security. In the present design, a number of user actions need the user to re-send their password (or else the server will have to store it in plain text, at least temporarily).
Much user interaction happens through AJAX requests. Rather than have the user re-type the password for each, I'd like to do something like this:
var password_plain = document .getElementById ("password") .value;
ajax ("/login.php", {password: password_plain, username: ...});
// later
ajax ("/api.php", {password: password_plain, action: ...});
Assume for the sake of the argument that the design is sensible in general, for example
the server does not store or leak the plain text password,
SSL is properly set up
password_plain=null
if the session expires (and clears the DOM value)The threat model is that the attacker doesn't have physical access to either the client or server machine, and cannot run arbitrary code on either, but can entice the user to visit malicious other web pages in the client's browser.
Is the password safe in a javascript variable?
Are you trying to secure against something malicious that is directly targeting your program? If so, then no, because environment variables do not have the same level of access control that files do.
To save passwords and secret keys in environment variables on Windows, you will need to open Advance System Setting. You can navigate to control panel > System and Security > System > Advanced system Settings . Now in Advance System Setting click on Environment Variables .
It depends.
If you don't enclose your scopes properly, a third party script can read and modify your variables during execution (assuming it's kept for more than one event-loop tick), however, that's a bit of an edge case. As long as it's done within any sort of function or module, and you don't have an XSS vulnerability right next to it, you're golden.
There are also questions about programs who can look at memory and stuff, but that's also irrelevant because if the user has such things on their computer, your JS code is the least of their worries.
I will add however that it's not recommended that you transmit the password over and over again, you normally want to authenticate once, get some sort of one-time token (like a session token), and use that for authentication during the session. Unlike passwords, sessions are easily invalidated server-side when something goes wrong.
What is important is:
Consider the fact that session IDs are stored in cookies and sent to the server with every request. Sound familiar? It's pretty much exactly what you're doing, but with the password instead of a session ID.
Personally I would recommend using proper sessions, but if you want a "stateless" system then what you have here should be fine, assuming as you are that everything else is "sensible" (HTTPS especially)
No, it's absolutely not safe in a JavaScript variable. If you're storing it locally, then cross-site scripting attacks (XSS) could compromise and steal your users' credentials in plain text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With