VOOZH about

URL: https://thenewstack.io/leveraging-web-workers-to-safely-store-access-tokens/

⇱ Leveraging Web Workers to Safely Store Access Tokens - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2021-02-24 06:24:08
Leveraging Web Workers to Safely Store Access Tokens
contributed,sponsor-wso2,sponsored,sponsored-post-contributed,
API Management / Security

Leveraging Web Workers to Safely Store Access Tokens

Web workers are used to offload resource-intensive tasks to a background thread in a web app, and also to safely store access tokens.
Feb 24th, 2021 6:24am by Theviyanthan Krishnamohan
👁 Featued image for: Leveraging Web Workers to Safely Store Access Tokens
WSO2 sponsored this post.

WSO2 sponsored this post.

Web workers are used to offload resource-intensive tasks to a background thread in a web app. However, did you know we can also use them to safely store access tokens? This post explains how it can be done and the advantages of doing so.

Access tokens are widely used to authorize users to access restricted resources, such as an API endpoint. However, in single-page applications, storing access tokens safely becomes a significant challenge.

Common Practices

Theviyanthan Krishnamohan
Theviyanthan is a software engineer at WSO2, experienced in React development. He enjoys dawdling in machine learning, artificial intelligence, evolutionary computing and the internet of things.

The usual practice is to store access tokens in the browser’s session storage or local storage. This is because we need to persist access tokens across page reloads, to prevent the need to re-authenticate on every reload. This provides a better user experience. However, these methods are susceptible to cross-site scripting attacks and malicious third-party libraries can easily access these tokens.

Most guidelines, while advising against storing access tokens in the session or local storage, recommend the use of session cookies. However, we can use session cookies only with the domain that sets the cookie.

Another popular suggestion is to store access tokens in the browser’s memory. This is a safer method than storing access tokens in the browser storage and more convenient than using session cookies. JavaScript closure is used to make sure that the token stored in the memory cannot be accessed by third-party libraries and cross-site scripting attacks.

However, there is a caveat. Even though the access token cannot be directly accessed by third-party libraries, they can intercept an HTTP request and extract the token from the header. There is also a risk of developers making inadvertent mistakes by leaking access tokens.

Using Web Workers to Store Access Tokens

We can address most of these issues by simply storing access tokens in web workers. Now, what are web workers? Web workers are essentially background threads. They run in a context that is different from that of the main thread. This means that the code running on the main thread cannot access code running on the web worker and vice versa. The only way a web worker and the main thread can communicate is by exchanging messages.

It is the developer who is responsible for designing how the main thread and the web workers communicate. They can communicate through a simple request-response model. So, by ensuring that at no point the web worker sends the access token as a response, we can prevent third-party libraries and cross-site scripting attacks from getting hold of the access token.

Why Not Use a Service Worker or iframe?

Both service workers and iframes offer different browsing contexts. Unlike web workers, service workers and iframes have a global reference. This means that codes can access them from anywhere.

In contrast, we can keep a reference to a web worker private using JavaScript closure. This adds an extra layer of protection by preventing third-party code from even trying to communicate with web workers.

Founded in 2005, WSO2 enables the composable enterprise. Our open source, API-first, and decentralized approach helps developers and architects to be more productive and rapidly build digital products to meet demand.
Learn More
The latest from WSO2

Furthermore, a site shares service workers over different tabs. This means, only one user can be logged in at a given time. Contrary to this, a web worker is specific to a tab and this allows multiple users to be logged in concurrently.

In addition to preventing third-party code from accessing access tokens, web workers also prevent HTTP-request interceptions. Since third-party codes run on the main thread, they cannot intercept requests initiated by the web workers. Yes, when we store access tokens in web workers, API requests needing those access tokens should also be initiated from the web workers.

The Architecture of the Solution

👁 Image

Figure 1: Solution architecture

Now, let’s discuss what the architecture of this storage mechanism would look like. To make sure the web worker receives the access token, it is the web worker that should make the request for the token. However, the request for sign-in will have to come from the main thread.

So, once a user hits the login page of the app, a post message (postMessage) should be dispatched to the web worker. Upon receiving this message, the web worker can then initiate the authentication flow. Once we receive the token, it can be stored safely in the web worker.

When API requests are to be sent, once again a message should be sent from the main thread with the necessary details to the web worker. Then, the web worker can initiate a request with the access token attached to the header. Once we receive a response, we will have to communicate it back to the main thread as another message.

However, this creates a new front for attacks. An attacker can initiate a request to a URL under their control. Since the web worker attaches the access token to the requests, the attacker will be able to receive the access token at their end.

We can prevent this by passing the allowed URLs to a web worker during the initialization phase. The web worker can then check if the URL to send the request matches one of the allowed URLs before dispatching the request.

The Flipside of Using Web Workers

One of the problems with web workers is that every time we reload the page, we will lose the login session. This happens because, with web workers, we store the access token in the browser memory. Every time we reload a page, the browser resets the memory. This would mean users may have to go through the login flow once again.

Most OAuth2/OIDC providers have ways of addressing this issue. For instance, some use session cookies to identify logged-in users. So, these providers will straight away return the access token without having the user enter their login credentials. Thus, despite having to go through the authentication flow on page reloads, the user experience does not change.

The need to use third-party libraries in the web worker poses another challenge. For example, to send API requests from the web worker, you may have to use certain libraries. These libraries will be running in the context of web workers. Hence, these libraries may get access to the stored token. However, by limiting the third-party libraries used in the web worker to a bare minimum and using only trusted ones, we can largely mitigate these issues.

Summary

Storing access tokens in the browser is a challenge. When comparing all the available methods, web workers seem to provide the safest and most convenient option. Using web workers, we can mitigate cross-site scripting attacks and malicious third-party libraries stealing access tokens.

Additionally, we can also prevent malicious codes from intercepting network requests and gaining access to the tokens. However, you need to be careful about the libraries you use in the web worker and you should try to keep it to a trusted few. By using web workers to send and receive network requests, we are actually reducing the load on the main thread.

WSO2 sponsored this post.

Feature image via Pixabay.

Founded in 2005, WSO2 enables the composable enterprise. Our open source, API-first, and decentralized approach helps developers and architects to be more productive and rapidly build digital products to meet demand.
Learn More
The latest from WSO2
TRENDING STORIES
Theviyanthan is a software engineer at WSO2, experienced in React development. He enjoys dawdling in machine learning, artificial intelligence, evolutionary computing and the internet of things.
Read more from Theviyanthan Krishnamohan
WSO2 sponsored this post.
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Pragma.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.