Is a react component's state susceptible to XSS?
I'm wondering if storing a JWT access token in a component's state is any less dangerous than in localStorage?
Is a react component's state susceptible to XSS?
I'm wondering if storing a JWT access token in a component's state is any less dangerous than in localStorage?
Depending on how the state is used and set, it can be less susceptible, but it's not foolproof.
If you're worried about untrustworthy code possibly accessing what they shouldn't, one of the issues with Local Storage is that any script running on the page can easily access any value in Local Storage for that domain. In contrast, a value which is in React state somewhere, by itself, is a whole lot more difficult to access.
If the state is used externally from the component anywhere, it's potentially susceptible to interception by malicious scripts. For example, if the state is used in an API request, a malicious script could patch window.fetch
or window.XMLHttpRequest
and spy on it (unless you take precautions against those too). And if the state is put onto the page, such as in an input (even if hidden), it'd be trivial for a malicious script to retrieve it.
Even if the state isn't used directly anywhere, in some circumstances, it's also barely possible to gain access to the closure in which it's defined or set.
Generally, if malicious code runs on a page, you can't count on anything else running on the page being secure. The best first approach would be to make sure your app doesn't have any XSS vulnerabilities in the first place. Guarding against such possibilities should be a distant second priority.