# Web Storage

**Web Storage**（网络存储）是WHATWG HTML标准中标准化的一项浏览器API，提供两种客户端机制，类似HTTP会话cookie，用于存储名值对：`localStorage`，跨浏览器重启持久存在；`sessionStorage`，作用域限于单个标签页的生命周期。<sup>[1](https://html.spec.whatwg.org/multipage/webstorage.html)</sup> 两者都以对象形式暴露，持有字符串键和字符串值，按源（origin）分区，因此其他源上的页面无法访问相同的存储对象。<sup>[1](https://html.spec.whatwg.org/multipage/webstorage.html)</sup> Web Storage的W3C推荐标准第二版于2016年4月19日发布。<sup>[2](https://www.w3.org/TR/2016/REC-webstorage-20160419/)</sup>

| 关键事实 | 详情 |
|---|---|
| 两种机制 | `localStorage`（持久、源级共享）与`sessionStorage`（每标签页，标签页关闭即清除）<sup>[3](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)</sup> |
| 配额 | Storage标准为localStorage和sessionStorage各分配5 × 2<sup>20</sup>字节（5 MiB）的配额；MDN报告各浏览器每源的总配额为10 MiB<sup>[4](https://storage.spec.whatwg.org/)</sup><sup> • </sup><sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria)</sup> |
| 数据模型 | 仅支持字符串；超出配额抛出`QuotaExceededError`<sup>[6](https://web.dev/articles/storage-for-the-web)</sup><sup> • </sup><sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria)</sup> |
| 跨标签页更新 | `setItem()`、`removeItem()`和`clear()`所做的更改会在其他能访问受影响数据的Document的Window对象上触发`storage`事件<sup>[1](https://html.spec.whatwg.org/multipage/webstorage.html)</sup> |
| 第三方访问 | Tranco前10,000个站点上90.98%的localStorage访问由第三方脚本发起<sup>[7](https://dl.acm.org/doi/10.1145/3696410.3714887)</sup> |
| 分区 | 自Chrome 115起，第三方Web Storage按顶级站点分区；Firefox始终静态分区<sup>[8](https://privacysandbox.google.com/blog/storage-partitioning-deprecation-trial-second-renewal)</sup><sup> • </sup><sup>[9](https://measuretheweb.org/privacy/browser_storage)</sup> |
| 安全 | 单个XSS漏洞即可被用来窃取Web Storage对象中的全部数据<sup>[10](https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html)</sup> |

## 工作原理

两个存储区域都是字符串键到字符串值的字典。两种机制主要在作用域和生命周期上不同。`sessionStorage`按浏览器标签页（顶级浏览上下文）和源分区，关闭标签页会销毁与之关联的所有sessionStorage数据；HTML标准规定每个顶级浏览上下文拥有一组唯一的会话存储区域，每个源一个。`localStorage`在标准模型中按源分区，因此同源文档共享一个区域；浏览器可能额外按顶级站点对第三方存储分区。数据通常在浏览器关闭并重新打开后仍然保留。<sup>[3](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)</sup><sup> • </sup><sup>[1](https://html.spec.whatwg.org/multipage/webstorage.html)</sup> 一篇学术综述称，二者在同源策略下具有相似的访问接口，主要区别在于过期机制。<sup>[11](https://www.ndss-symposium.org/wp-content/uploads/madweb2022_23005_paper.pdf)</sup>

**跨标签页通信。** 当`setItem()`、`removeItem()`或`clear()`更改存储区域时，浏览器会在其他能访问新存入或移除数据的Document的Window对象上触发`storage`事件。`StorageEvent`携带`key`、`oldValue`、`newValue`、`url`和`storageArea`属性。做出更改的文档不会收到该事件；其他上下文只有在能访问同一存储区域时才会收到，这使该事件成为标签页之间的轻量级消息通道。<sup>[1](https://html.spec.whatwg.org/multipage/webstorage.html)</sup>

**同步访问。** 两个API都是同步的：设置、读取和移除操作会阻塞其他JavaScript执行直到完成。MDN指出，在存储或读取大量数据时这会影响应用性能，web.dev出于同样的理由建议避免用localStorage存储大数据，并补充说它无法从Web Worker或Service Worker访问。<sup>[3](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)</sup><sup> • </sup><sup>[6](https://web.dev/articles/storage-for-the-web)</sup>

## 容量与驱逐

W3C推荐标准建议“每源五兆字节的、大体随意的限制”，Storage标准则将两个区域各自的配额定为5 × 2<sup>20</sup>字节，将localStorage归类为local类型、sessionStorage归类为session类型。<sup>[2](https://www.w3.org/TR/2016/REC-webstorage-20160419/)</sup><sup> • </sup><sup>[4](https://storage.spec.whatwg.org/)</sup> MDN将浏览器行为概括为每源最多10 MiB，其中本地存储最多5 MiB、会话存储最多5 MiB，达到限制时抛出`QuotaExceededError`，应用应当用`try...catch`块处理。<sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria)</sup> 实测容量在实践中差异更大，依浏览器不同从5 MB到25 MB不等。<sup>[12](https://eprints.bournemouth.ac.uk/30202/1/BUCSR-2018-01%20Client%20side%20security%20considerations.pdf)</sup>

**驱逐。** 浏览器采用最近最少使用（LRU）策略，优先删除最近最少使用的源的数据；驱逐会跳过通过`navigator.storage.persist()`获得持久化许可的源。根据Storage标准，本地存储桶只有在用户或用户代理授予`persistent-storage`权限后才能变为持久，一旦持久，用户代理不能在源或用户未参与的情况下清除它。存储桶被清除时，必须整体清除。<sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria)</sup><sup> • </sup><sup>[4](https://storage.spec.whatwg.org/)</sup> WebKit描述了同样的模型：默认情况下所有源都使用尽力而为模式，其数据可被驱逐，驱逐按最近最少使用顺序决定，其中“最近使用”指最后一次用户交互或存储操作。<sup>[13](https://webkit.org/blog/14403/updates-to-storage-policy/)</sup> Safari还会在跨站跟踪预防开启时，删除过去七天浏览器使用中无用户交互（如点击或轻触）的源的脚本创建数据；服务器设置的cookie不在此列。<sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria)</sup>

## 与cookie、IndexedDB及其他存储的比较

Web Storage介于cookie和更大的异步存储API之间。cookie随每个HTTP请求发送，因此把本可存放于别处的数据存在cookie中会不必要地增大请求体积；浏览器还将cookie限制在几千个字符以内，而Web Storage实测为5到25 MB。<sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria)</sup><sup> • </sup><sup>[6](https://web.dev/articles/storage-for-the-web)</sup><sup> • </sup><sup>[12](https://eprints.bournemouth.ac.uk/30202/1/BUCSR-2018-01%20Client%20side%20security%20considerations.pdf)</sup> cookie保留了一项Web Storage不具备的优势：`httpOnly`标志使会话标识符远离JavaScript。<sup>[10](https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html)</sup>

对于更大或性能敏感的数据，[IndexedDB](https://www.edgepedia.org/zh/indexeddb)、源私有文件系统（OPFS）和Cache Storage API在所有现代浏览器中都受支持，它们是异步的，不会阻塞主线程；它们还共享一个以磁盘百分之几十计的配额，远高于Web Storage约5 MiB的水平。<sup>[6](https://web.dev/articles/storage-for-the-web)</sup><sup> • </sup><sup>[9](https://measuretheweb.org/privacy/browser_storage)</sup> Web Storage仍适用于`sessionStorage`中小的、会话特定的状态。<sup>[6](https://web.dev/articles/storage-for-the-web)</sup>

## 安全与隐私

**XSS与令牌窃取。** 由于Web Storage始终可被[JavaScript](https://www.edgepedia.org/zh/javascript)访问，单个跨站脚本漏洞即可被用来窃取这些对象中的全部数据。OWASP建议不要在localStorage中存储敏感信息或会话标识符，并指出cookie可通过`httpOnly`标志缓解标识符窃取。其测试指南将存储在localStorage或sessionStorage中的JWT等认证令牌标记为可通过XSS暴露，并建议验证登出或会话过期后存储条目已被清除。<sup>[10](https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html)</sup><sup> • </sup><sup>[14](https://wstg.owasp.org/latest/4-Web_Application_Security_Testing/11-Client-side/12-Browser_Storage/)</sup>

**源级共享。** 与cookie不同，Web Storage对象无法限制到特定路径；每个对象都在一个源内共享，仅受同源策略保护，因此OWASP建议不要在同一源上托管多个应用。规范本身也不建议在使用共享主机名的网站上使用该技术，并建议将持久存储的数据视为潜在敏感数据。<sup>[10](https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html)</sup><sup> • </sup><sup>[12](https://eprints.bournemouth.ac.uk/30202/1/BUCSR-2018-01%20Client%20side%20security%20considerations.pdf)</sup>

**跨站泄露。** 根据HTML标准，实现这些持久存储特性带来两大危险：让恶意站点读取属于其他域的数据，以及让它们写入随后被其他域读取的数据；用户代理可以限制localStorage访问仅限源自顶级文档的源的脚本——例如阻止第三方iframe内的访问——因为第三方站点读取的数据可能泄露信息。<sup>[1](https://html.spec.whatwg.org/multipage/webstorage.html)</sup> 研究还显示了侧信道风险：通过监测存储用量的攻击在Chrome中以约90%的准确率推断出用户访问的是哪个站点以及登录状态。<sup>[15](https://dl.acm.org/doi/10.1145/2991079.2991080)</sup>

**隐私浏览。** 在隐私模式下，`localStorage`被当作`sessionStorage`对待：API仍可用，但隐私窗口中存储的所有数据会在浏览器或标签页关闭时删除。浏览器在隐私浏览中也可能适用不同的配额。<sup>[3](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)</sup><sup> • </sup><sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria)</sup>

## 数据一览

- 每个存储区域的配额：按Storage标准为5 × 2<sup>20</sup>字节（5 MiB）；按MDN每源合计10 MiB；各浏览器实测范围5到25 MB。<sup>[4](https://storage.spec.whatwg.org/)</sup><sup> • </sup><sup>[5](https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria)</sup><sup> • </sup><sup>[12](https://eprints.bournemouth.ac.uk/30202/1/BUCSR-2018-01%20Client%20side%20security%20considerations.pdf)</sup>
- Tranco前10,000个站点上第三方脚本的存储访问占比：localStorage访问的90.98%、cookie访问的89.84%、IndexedDB访问的72.49%。<sup>[7](https://dl.acm.org/doi/10.1145/3696410.3714887)</sup>
- 持久性客户端XSS：在Alexa前5,000个域中，超过8%存在从持久存储到HTML和JavaScript等危险汇点的未过滤数据流；在使用源自存储的数据的站点中，21%存在漏洞，且至少70%的漏洞站点可被该研究的攻击模型直接利用。<sup>[16](https://www.ndss-symposium.org/ndss-paper/dont-trust-the-locals-investigating-the-prevalence-of-persistent-client-side-cross-site-scripting-in-the-wild/)</sup>
- Safari 17对大型存储API的配额：浏览器应用中每源最多占总磁盘空间的60%，其他应用中为15%，总配额分别为80%和20%；超出源配额抛出`QuotaExceededError`。<sup>[13](https://webkit.org/blog/14403/updates-to-storage-policy/)</sup>

## 近期发展

**第三方分区。** 自Chrome 115起，存储、Service Worker和通信API在第三方上下文中被分区，因此嵌入在两个不同站点上的同一第三方会得到两个独立的存储；Web Storage API按顶级站点分区，尽管它不受配额管理。一项弃用试用允许站点在Chrome 113至126期间临时取消这些API的分区，并以DisableThirdPartyStoragePartitioning2之名从Chrome 127续期至132。<sup>[8](https://privacysandbox.google.com/blog/storage-partitioning-deprecation-trial-second-renewal)</sup><sup> • </sup><sup>[17](https://developers.google.com/privacy-sandbox/cookies/storage-partitioning)</sup> Firefox走得更远，无论用户隐私设置如何，始终静态分区localStorage、IndexedDB和Cache Storage，而自Firefox 103起对cookie进行动态分区。<sup>[9](https://measuretheweb.org/privacy/browser_storage)</sup>

**Storage Access API扩展。** 通过Storage Access API的非分区cookie访问自Chrome 119起可用，从Chrome 125开始，Chrome在该API中提供了对非cookie存储的支持，允许嵌入式第三方上下文请求访问非分区的localStorage和IndexedDB。<sup>[8](https://privacysandbox.google.com/blog/storage-partitioning-deprecation-trial-second-renewal)</sup><sup> • </sup><sup>[18](https://developer.chrome.com/blog/new-in-chrome-125)</sup>

## 实践中的使用

现实世界访问的主导模式并非第一方应用状态。在Tranco前10,000个网站上，所有localStorage访问的90.98%由第三方脚本执行，现有浏览器机制为这些脚本提供对localStorage的全有或全无访问；一项2025年的研究在Firefox中实现了基于标签的最小权限强制执行，阻止未获允许的域访问存储对象。<sup>[7](https://dl.acm.org/doi/10.1145/3696410.3714887)</sup>

常见误用源于该API的便利性。将认证令牌存储在localStorage中会使任何能在该源上运行的脚本都能读取它们，而登出后仍保留的令牌或会话标识符是被列出的客户端存储风险。通过同步API写入的大型或频繁更新的值会阻塞主线程，这就是为什么针对较大数据集的指南指向IndexedDB。<sup>[14](https://wstg.owasp.org/latest/4-Web_Application_Security_Testing/11-Client-side/12-Browser_Storage/)</sup><sup> • </sup><sup>[3](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)</sup><sup> • </sup><sup>[6](https://web.dev/articles/storage-for-the-web)</sup>

## References

1. [HTML Standard — Web storage, WHATWG](https://html.spec.whatwg.org/multipage/webstorage.html)
2. [Web Storage (Second Edition), W3C Recommendation, 19 April 2016](https://www.w3.org/TR/2016/REC-webstorage-20160419/)
3. [Web Storage API, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)
4. [Storage Standard, WHATWG](https://storage.spec.whatwg.org/)
5. [Storage quotas and eviction criteria, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria)
6. [Storage for the web, web.dev](https://web.dev/articles/storage-for-the-web)
7. [Least Privilege Access for Persistent Storage Mechanisms in Web Browsers, ACM Web Conference 2025](https://dl.acm.org/doi/10.1145/3696410.3714887)
8. [Storage Partitioning deprecation trial renewal, Privacy Sandbox](https://privacysandbox.google.com/blog/storage-partitioning-deprecation-trial-second-renewal)
9. [Browser storage privacy notes, Measure The Web](https://measuretheweb.org/privacy/browser_storage)
10. [HTML5 Security Cheat Sheet, OWASP Cheat Sheet Series](https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html)
11. [What Storage? An Empirical Analysis of Web Storage, NDSS MadWeb 2022](https://www.ndss-symposium.org/wp-content/uploads/madweb2022_23005_paper.pdf)
12. [Security considerations around the usage of client-side storage APIs, Bournemouth University](https://eprints.bournemouth.ac.uk/30202/1/BUCSR-2018-01%20Client%20side%20security%20considerations.pdf)
13. [Updates to Storage Policy, WebKit](https://webkit.org/blog/14403/updates-to-storage-policy/)
14. [Testing Browser Storage, OWASP Web Security Testing Guide](https://wstg.owasp.org/latest/4-Web_Application_Security_Testing/11-Client-side/12-Browser_Storage/)
15. [Inferring browser activity and status through remote monitoring of storage usage, ACSAC 2016](https://dl.acm.org/doi/10.1145/2991079.2991080)
16. [Don't Trust The Locals: Investigating the Prevalence of Persistent Client-Side Cross-Site Scripting in the Wild, NDSS](https://www.ndss-symposium.org/ndss-paper/dont-trust-the-locals-investigating-the-prevalence-of-persistent-client-side-cross-site-scripting-in-the-wild/)
17. [Storage Partitioning, Privacy Sandbox documentation](https://developers.google.com/privacy-sandbox/cookies/storage-partitioning)
18. [New in Chrome 125, Chrome for Developers](https://developer.chrome.com/blog/new-in-chrome-125)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Web development and web-platform technologies › Web platform APIs

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/web-storage

English original: https://www.edgepedia.org/web-storage
