Let's say, I store a plain password of a user in a global JS variable.
May anyone come up with an idea of how a hacker may get that value from user's browser? I now that it's "possible", but I need examples with description.
Let's say, I store a plain password of a user in a global JS variable.
May anyone come up with an idea of how a hacker may get that value from user's browser? I now that it's "possible", but I need examples with description.
Imagine you are using a web site that is a forum (similar to this one) where users can enter their own content and other users can see it. Now imagine this site didn't cleanse my input properly, and I figured out a way to write a post that, when rendered on your browser, embeds executable Javascript. When you go to view my post, that Javascript will execute within your browser and it will be able to access the global JS variable, and therefore obtain the password.
This sort of thing is called a stored XSS attack.
JavaScript runs client-side, and can be seen by anyone who has access to the page they are on. When your browser connects to a site, one of the things it can download and execute is JavaScript. So storing any confidential information in a global JavaScript variable is a terrible idea and you wouldn't need a convoluted XSS script to access it if it is on your own page.
Also, JavaScript is not persistent, or at least it shouldn't be, with the exception of cookies. Cookies can be used to authenticates users on a site on a per-session basis. Essentially, when they connect to a site and enter valid login details, they are issued a valid session cookie that acts as an id granting them access to otherwise inaccessible or private content/pages.
Here's where cross-site scripting can be dangerous. Say that a website allows JavaScript in messages between users, and you send a message to the web admin with JavaScript that grabs the admin's cookie. From there you could theoretically steal the admin's user session and would have all of the permissions the admin has.
Going back to your original question, if a user somehow had their password in a JavaScript variable (it only would exist on their computer, because JavaScript is client-side), you could theoretically send them a message or post a comment on a page they visit with malicious JavaScript that sends you a message with the values of every variable in the JavaScript on their browser. But again, if you're storing passwords in plaintext in JavaScript, you have bigger problems.