<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adel ZEMZEMI</title>
    <description>The latest articles on DEV Community by Adel ZEMZEMI (@adelzemzemi).</description>
    <link>https://dev.to/adelzemzemi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4255365%2F7b685517-25ef-451e-904e-c10e15a182b5.png</url>
      <title>DEV Community: Adel ZEMZEMI</title>
      <link>https://dev.to/adelzemzemi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adelzemzemi"/>
    <language>en</language>
    <item>
      <title>Your encrypted localStorage values can be swapped</title>
      <dc:creator>Adel ZEMZEMI</dc:creator>
      <pubDate>Fri, 20 Nov 2026 09:31:25 +0000</pubDate>
      <link>https://dev.to/adelzemzemi/your-encrypted-localstorage-values-can-be-swapped-cgg</link>
      <guid>https://dev.to/adelzemzemi/your-encrypted-localstorage-values-can-be-swapped-cgg</guid>
      <description>&lt;p&gt;I maintain a small library that encrypts what a web app keeps in &lt;code&gt;localStorage&lt;/code&gt;. It uses AES-256-GCM, so a stored value that has been modified is rejected rather than returned. For a year I read that sentence as "the stored data cannot be tampered with".&lt;/p&gt;

&lt;p&gt;It can. Not by changing a value: by moving one.&lt;/p&gt;

&lt;h2&gt;
  
  
  The attack
&lt;/h2&gt;

&lt;p&gt;Two values, same password, two keys:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;SecureStorage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;MemoryStorage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AesGcmEncryptor&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@azemzemi/cryptobox'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;backend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MemoryStorage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SecureStorage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;backend&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AesGcmEncryptor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;bindToKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setSecure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'role:guest'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setSecure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'role:admin'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now play the attacker. You do not know the password. You cannot read either value, and you cannot forge a new one. You can only write to the store, so you copy one ciphertext over the other:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;backend&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'role:guest'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;backend&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'role:admin'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getSecure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'role:guest'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// { admin: true }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No error. The ciphertext is authentic: it was produced with the right key and no bit of it changed. GCM did its job. The library returned a value that is perfectly valid and sits in the wrong place.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{ bindToKey: false }&lt;/code&gt; above reproduces what version 1 did everywhere. Nothing in the ciphertext knew which storage key it belonged to.&lt;/p&gt;

&lt;h2&gt;
  
  
  Who can write to your storage without your password
&lt;/h2&gt;

&lt;p&gt;This is the right question to ask before caring. An attacker who runs script in your page (XSS, a compromised dependency) does not need this trick: they see the password when it is typed and the plaintext when it is decrypted. No storage encryption helps there.&lt;/p&gt;

&lt;p&gt;The swap matters for the cases storage encryption is actually for, where someone reaches the bytes but not the running application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a copied or restored browser profile,&lt;/li&gt;
&lt;li&gt;a synced storage area or a backup,&lt;/li&gt;
&lt;li&gt;a shared device,&lt;/li&gt;
&lt;li&gt;a server-side key-value store or a database dump, if you use the same format there.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In all of them the promise "they cannot read it and they cannot change it" was only half true. They could rearrange it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix: tell the cipher where the value lives
&lt;/h2&gt;

&lt;p&gt;AES-GCM takes a second input next to the plaintext, the additional authenticated data. It is not encrypted and not stored. It is mixed into the authentication tag, and decryption fails unless the reader supplies exactly the same bytes.&lt;/p&gt;

&lt;p&gt;So the storage key goes in there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// write&lt;/span&gt;
&lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subtle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'AES-GCM'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;additionalData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;header&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s1"&gt;'role:admin'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// read, under the key the value was found at&lt;/span&gt;
&lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subtle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'AES-GCM'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;additionalData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;header&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s1"&gt;'role:guest'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// throws: the tag does not match&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ciphertext is the same length as before. The binding costs nothing to store, because the reader already knows the key it is reading from.&lt;/p&gt;

&lt;p&gt;With the default settings in version 2, the same swap ends differently:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SecureStorage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;backend&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AesGcmEncryptor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// same writes, same copy&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getSecure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'role:guest'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// CryptoBoxError: DECRYPTION_FAILED&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same mechanism authenticates the envelope header, which records the format version and the PBKDF2 iteration count. Lowering the count in a stored value, to make the password cheaper to guess later, now breaks the tag too.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it still does not do
&lt;/h2&gt;

&lt;p&gt;Binding to the key stops a value from being moved. It does not stop these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rollback.&lt;/strong&gt; Yesterday's value for the same key, put back in place, still decrypts. The format has no notion of freshness. If that matters, keep a counter or a timestamp inside the payload.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deletion.&lt;/strong&gt; Whoever can write can also erase. Tampering is detected, not prevented.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Old data.&lt;/strong&gt; Values written by version 1 carry no binding. They stay movable until you rewrite them, and the library keeps accepting them until you tell it to stop. Migration is one call per key, then one option to close the door.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And the library has not been audited by a third party. What it has instead is a second implementation: the test suite writes and reads the same format with OpenSSL through &lt;code&gt;node:crypto&lt;/code&gt;, in both directions, and a set of frozen ciphertexts that must keep decrypting forever.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;There is a page where you can do this to your own browser: store a value, look at what &lt;code&gt;localStorage&lt;/code&gt; holds, then read it back with the wrong password, after flipping one bit, and after moving it to another key.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Demo: &lt;a href="https://adelzemzemi.github.io/CryptoBox/" rel="noopener noreferrer"&gt;https://adelzemzemi.github.io/CryptoBox/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Source: &lt;a href="https://github.com/adelzemzemi/CryptoBox" rel="noopener noreferrer"&gt;https://github.com/adelzemzemi/CryptoBox&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install @azemzemi/cryptobox&lt;/code&gt; (zero dependencies, about 6.5 kB gzipped)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you encrypt anything keyed by a name, an id or a path, check what your ciphertext knows about where it lives. Mine knew nothing. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>security</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
