# Is auth0-spa-js storing tokens in localStorage vulnerable to XSS?

**URL:** <https://community.auth0.com/t/is-auth0-spa-js-storing-tokens-in-localstorage-vulnerable-to-xss/77145>\
**Category:** Get Help\
**Tags:** auth0-spa-js\
**Created:** [January 27, 2022, 3:21pm UTC](https://community.auth0.com/t/is-auth0-spa-js-storing-tokens-in-localstorage-vulnerable-to-xss/77145 "2022-01-27T15:21:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![quequierebego](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/quequierebego/32/34725_2.png) [@quequierebego](https://community.auth0.com/u/quequierebego)\
**Post date:** [January 27, 2022, 3:21pm UTC](https://community.auth0.com/t/is-auth0-spa-js-storing-tokens-in-localstorage-vulnerable-to-xss/77145/1 "2022-01-27T15:21:43Z")

</div>

Hello, I want to use Auth0-spa-js lib and I saw that token are stored in memory.  
I know that’s better than store them in localstore (that can be exposed to XSS attack)

But, if i load a malicious script, can he stole tokens stored in memory ?  
I supose tokens are stored in simple vars, so why can’t they be exposed to this type of attack ?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [January 27, 2022, 5:35pm UTC](https://community.auth0.com/t/is-auth0-spa-js-storing-tokens-in-localstorage-vulnerable-to-xss/77145/3 "2022-01-27T17:35:22Z")

</div>

Hi @quequierebego,

Welcome to the Auth0 Community!

As stated in the auth0-spa-js SDK’s GitHub repo:

> The `cacheLocation: 'localstorage'` feature will allow the caching of data **such as ID and access tokens** to be stored in local storage. Exercising this option changes the security characteristics of your application and **should not be used lightly**. Extra care should be taken to mitigate against XSS attacks and minimize the risk of tokens being stolen from local storage.

Let me know if you have any questions!

---

<div class="post-metadata">

**Author:** ![quequierebego](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/quequierebego/32/34725_2.png) [@quequierebego](https://community.auth0.com/u/quequierebego)\
**Post date:** [January 27, 2022, 5:48pm UTC](https://community.auth0.com/t/is-auth0-spa-js-storing-tokens-in-localstorage-vulnerable-to-xss/77145/4 "2022-01-27T17:48:32Z")

</div>

> [@dan.woda](#):
>
> The `cacheLocation: 'localstorage'` feature will allow the caching of data **such as ID and access tokens** to be stored in local storage. Exercising this option changes the security characteristics of your application and **should not be used lightly**. Extra care should be taken to mitigate against XSS attacks and minimize the risk of tokens being stolen from local storage

I don’t plan to use localstorage option.  
The documentation say

> The Auth0 SPA SDK stores tokens in memory by default.

What i want to know if that “memory” storage could be vulnerable to malicious script who can access to that memory to stole tokens.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [January 27, 2022, 6:06pm UTC](https://community.auth0.com/t/is-auth0-spa-js-storing-tokens-in-localstorage-vulnerable-to-xss/77145/5 "2022-01-27T18:06:05Z")

</div>

Here is a SO post that answers your question:

[cookies - How storing JWT in-memory is not vulnerable to XSS? - Stack Overflow](https://stackoverflow.com/questions/60566690/how-storing-jwt-in-memory-is-not-vulnerable-to-xss#:~:text=1%20Answer&text=Storing%20JWTs%20in%2Dmemory%20still,malicious%20library%20in%20your%20code).

---

<div class="post-metadata">

**Author:** ![quequierebego](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/quequierebego/32/34725_2.png) [@quequierebego](https://community.auth0.com/u/quequierebego)\
**Post date:** [January 27, 2022, 8:47pm UTC](https://community.auth0.com/t/is-auth0-spa-js-storing-tokens-in-localstorage-vulnerable-to-xss/77145/6 "2022-01-27T20:47:54Z")

</div>

Thank you ! that’s solve my question 😇

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [February 11, 2022, 8:48pm UTC](https://community.auth0.com/t/is-auth0-spa-js-storing-tokens-in-localstorage-vulnerable-to-xss/77145/7 "2022-02-11T20:48:53Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
