Web Storage: Overview
Local Storage and Session Storage both provide a simple and efficient solution for client-side data storage.
- Local Storage enables the permanent storage of data across browser sessions, ideal for storing user settings and preferences.
- Session Storage provides a temporary storage solution for data that is only relevant during a single browser session, such as the contents of a shopping basket.
How Web Storage works
Using Web Storage is simple and is done via JavaScript interfaces. Data is stored in key-value pairs and can be set, retrieved, and deleted using the Web Storage API. These interfaces provide methods such as setItem(), getItem(), and removeItem() for manipulating the stored data.
Saving data
To store data in Local Storage or Session Storage, the setItem() method is used:
localStorage.setItem('key', 'value');
sessionStorage.setItem('key', 'value');
Retrieving data
Data can be retrieved using the getItem() method:
var value = localStorage.getItem('key');
var value = sessionStorage.getItem('key');
Deleting data
Individual data points can be deleted using the removeItem() method, while the clear() method clears the entire storage area:
localStorage.removeItem('key');
localStorage.clear();
sessionStorage.removeItem('key');
sessionStorage.clear();
Where Web Storage is used
Web Storage is commonly used for the following purposes:
- Saving user settings and application states
- Caching of data to improve loading speed and performance
- Saving form data and user session data
Security and limitations of Web Storage
1. XSS attacks (cross-site scripting)
Web Storage is vulnerable to XSS attacks because malicious code injected into a webpage can access Local Storage or Session Storage and steal sensitive data. You should therefore not store sensitive data such as session IDs or access tokens in Web Storage. For session data, cookies with the HttpOnly attribute are more secure, as they cannot be read by JavaScript.
2. CSRF attacks (cross-site request forgery)
In a CSRF attack, another site makes the browser send requests with the cookies it includes automatically. The browser does not send Web Storage content automatically. A CSRF attack therefore cannot use a token that is kept only in Web Storage, but XSS can read it.
3. Plain text storage
Data in Web Storage is stored in plain text and is not encrypted, which poses a risk when sensitive or personal information is stored.
4. Limited access control
Any script running on a page can access the Local Storage and Session Storage of that page’s origin (protocol, host and port), including embedded third-party scripts. This increases data vulnerability.
Technical limitations
1. Storage capacity
The size of Web Storage is limited, in common browsers to up to 5 MiB of Local Storage and 5 MiB of Session Storage per origin, which restricts the amount of information that can be stored.
2. Lack of persistence
While Local Storage stores data permanently, Session Storage retains data only during a browser session. Local Storage is not always kept either. With cross-site tracking prevention turned on, Safari deletes script-created data of a website if there has been no interaction with it in the last seven days of browser use.
3. No synchronisation between tabs and windows
Data stored in Session Storage is only available in the same tab and cannot be shared between different tabs or windows.
4. Platform and browser differences
The implementation and availability of Web Storage can vary depending on the browser and platform.
5. No server-side communication
Web Storage is purely client-side, which means that stored data is not automatically sent to the server.
Using Web Storage correctly
Local Storage and Session Storage are valuable components of the Web Storage API that offer advantages over cookies for many use cases. They enable efficient handling of data in web applications, as the stored data is not automatically sent to the server with every request. Legally, Web Storage is treated in the same way as cookies. Under Article 5(3) of the ePrivacy Directive, storing information in the browser or accessing it requires consent unless this is strictly necessary to provide a service the user has explicitly requested.
