<?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: zhihu wu</title>
    <description>The latest articles on DEV Community by zhihu wu (@zhihu_wu_dea1d82af01a04d7).</description>
    <link>https://dev.to/zhihu_wu_dea1d82af01a04d7</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%2F3921997%2Fa775d61f-ed57-461b-ac46-ed108350189e.png</url>
      <title>DEV Community: zhihu wu</title>
      <link>https://dev.to/zhihu_wu_dea1d82af01a04d7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zhihu_wu_dea1d82af01a04d7"/>
    <language>en</language>
    <item>
      <title>Your Hex Converter Is Off by 4,095: Five Base-Conversion Traps</title>
      <dc:creator>zhihu wu</dc:creator>
      <pubDate>Mon, 16 Nov 2026 13:05:31 +0000</pubDate>
      <link>https://dev.to/zhihu_wu_dea1d82af01a04d7/your-hex-converter-is-off-by-4095-five-base-conversion-traps-4fgl</link>
      <guid>https://dev.to/zhihu_wu_dea1d82af01a04d7/your-hex-converter-is-off-by-4095-five-base-conversion-traps-4fgl</guid>
      <description>&lt;p&gt;I was chasing a mismatched ID in a log line last week. The value came in as hex, I pasted it into a converter, got a decimal back, compared it with what the database held — and the two disagreed by 4,095. The hex was fine. The converter was fine for 32-bit values. The number was 64 bits.&lt;/p&gt;

&lt;p&gt;Here are the five traps I have hit often enough to check first every time.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Floats can't hold 64-bit integers
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'FFFFFFFFFFFFFFFF'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// 18446744073709552000  (wrong)&lt;/span&gt;
&lt;span class="nc"&gt;BigInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'0xFFFFFFFFFFFFFFFF'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// '18446744073709551615'  (right)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A JavaScript number is a binary64 float: 53 bits of integer precision. &lt;code&gt;Number.MAX_SAFE_INTEGER&lt;/code&gt; is 9,007,199,254,740,991, and everything above it is rounded to the nearest representable value. Any hex ID, hash prefix, or snowflake ID past that boundary is silently wrong the moment it passes through &lt;code&gt;parseInt&lt;/code&gt;, &lt;code&gt;Number()&lt;/code&gt;, or &lt;code&gt;JSON.parse&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Bitwise operators truncate to 32 bits
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;        &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;        &lt;span class="c1"&gt;// -2147483648&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;|&lt;/code&gt;, &lt;code&gt;&amp;amp;&lt;/code&gt;, &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;~&lt;/code&gt; squeeze both operands into signed 32-bit integers. Flag maths that works in tests fails on a value above 2^31, and it fails quietly — the result is still a number. Use &lt;code&gt;BigInt&lt;/code&gt; with the &lt;code&gt;n&lt;/code&gt; operators (&lt;code&gt;&amp;amp;n&lt;/code&gt;, &lt;code&gt;&amp;gt;&amp;gt;n&lt;/code&gt;) when you mean 64 bits.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Negative numbers have no canonical hex form
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;// '-1'&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// 'ffffffff'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both are "correct". One is a signed magnitude, the other is 32-bit two's complement; the same ciphertext differs if the other side assumes 64 bits (&lt;code&gt;ffffffffffffffff&lt;/code&gt;). Whenever you convert a signed value, decide the width explicitly and write it down in the protocol.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. &lt;code&gt;parseInt&lt;/code&gt; changed meaning, and it stops at garbage
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;parseInt('08')&lt;/code&gt; is &lt;code&gt;8&lt;/code&gt; and &lt;code&gt;parseInt('010')&lt;/code&gt; is &lt;code&gt;10&lt;/code&gt; — the legacy octal default disappeared in ES5. Octal now needs &lt;code&gt;0o&lt;/code&gt; or an explicit radix. The sibling trap: &lt;code&gt;parseInt('12px')&lt;/code&gt; returns &lt;code&gt;12&lt;/code&gt; while &lt;code&gt;Number('12px')&lt;/code&gt; returns &lt;code&gt;NaN&lt;/code&gt;. Lenient parsing turns a typo into a plausible ID.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Fractions are where conversions go feral
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;(255.5).toString(16)&lt;/code&gt; is &lt;code&gt;'ff.8'&lt;/code&gt;, which looks sane. But &lt;code&gt;(1234.567).toString(2)&lt;/code&gt; returns a 40-digit tail — you are looking at the binary expansion of a float, not the binary of 1234.567. Convert integers exactly and scale (cents, milliseconds, basis points) instead of converting fractions.&lt;/p&gt;

&lt;h2&gt;
  
  
  What helps
&lt;/h2&gt;

&lt;p&gt;Convert integers only. Keep &lt;code&gt;BigInt&lt;/code&gt; from input to output. Before copying a value anywhere, check how many bytes it needs: 1 byte covers 0–255, 2 covers 65,535, 4 covers 4,294,967,295. If a 5-byte value is in a field that the protocol says is 4 bytes, that is the bug, not the arithmetic.&lt;/p&gt;

&lt;p&gt;For quick checks I use a &lt;a href="https://codetoolbox.pro/tools/number-base-converter.html" rel="noopener noreferrer"&gt;number base converter&lt;/a&gt; that parses with &lt;code&gt;BigInt&lt;/code&gt;, syncs decimal/hex/binary/octal in one view, and prints the minimum byte width plus the 32-bit and 64-bit unsigned forms of negatives. It runs entirely in the page — no upload, so it is safe for IDs pulled from production logs.&lt;/p&gt;

&lt;p&gt;Most base bugs aren't arithmetic bugs. They're width bugs and precision bugs wearing an arithmetic costume.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Your Image Compression Made the File 2.5x Bigger: 5 Numbers From One 6016px Photo</title>
      <dc:creator>zhihu wu</dc:creator>
      <pubDate>Sat, 14 Nov 2026 13:05:11 +0000</pubDate>
      <link>https://dev.to/zhihu_wu_dea1d82af01a04d7/your-image-compression-made-the-file-25x-bigger-5-numbers-from-one-6016px-photo-1mgg</link>
      <guid>https://dev.to/zhihu_wu_dea1d82af01a04d7/your-image-compression-made-the-file-25x-bigger-5-numbers-from-one-6016px-photo-1mgg</guid>
      <description>&lt;p&gt;I spent an afternoon pushing one photo through a compression pipeline at every setting it has. These are file sizes from a single source image on a single machine — no benchmarks, just bytes, because "quality 80" tells you nothing until you know what it does to the file.&lt;/p&gt;

&lt;p&gt;Source: one 6,016 x 6,016 JPEG, 4.80 MB, already encoded around quality 80.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Output&lt;/th&gt;
&lt;th&gt;File size&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;quality 30&lt;/td&gt;
&lt;td&gt;0.62 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;quality 60&lt;/td&gt;
&lt;td&gt;1.88 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;quality 80&lt;/td&gt;
&lt;td&gt;4.80 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;quality 90&lt;/td&gt;
&lt;td&gt;5.46 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;quality 100&lt;/td&gt;
&lt;td&gt;12.09 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;resized to 1200px wide&lt;/td&gt;
&lt;td&gt;0.07 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;same image saved as PNG&lt;/td&gt;
&lt;td&gt;30.57 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  1. Above ~85, quality is a size knob, not a sharpness knob
&lt;/h3&gt;

&lt;p&gt;Quality 90 cost 14% more bytes than 80. Quality 100 cost 152% more — 12.09 MB for one image. You will not see that difference at 100% zoom on a laptop screen. You will see it in your bandwidth bill and in Largest Contentful Paint.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Re-compressing an already-compressed JPEG saves nothing
&lt;/h3&gt;

&lt;p&gt;Quality 80 in, quality 80 out: 4.798 MB versus 4.800 MB. The source had already discarded exactly what quality 80 discards, so there was nothing left to remove. If your upload pipeline re-encodes at the quality the file arrived with, it is a no-op that costs a generation of quality.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Raising the quality later inflates the file without bringing detail back
&lt;/h3&gt;

&lt;p&gt;Re-encoding the quality-60 output at quality 80 gave 2.66 MB (+41%). At quality 100 it gave 7.13 MB (+278%). Both files are still quality-60 images — blurrier than the 1.88 MB they started as, and bigger than many originals. Lossy compression is a ratchet: decide once, at the largest resolution you will ever need, and keep that file as the source.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Dimensions beat quality by an order of magnitude
&lt;/h3&gt;

&lt;p&gt;Resizing to 1200px wide produced 68.8 KB — roughly 70 times smaller than quality 80 at full size. No quality slider gets you there. Resize first, then compress; the reverse order just throws away work.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. PNG is a format decision, not a compression setting
&lt;/h3&gt;

&lt;p&gt;The same photo as PNG: 30.57 MB, 6.4x the JPEG. PNG earns its size on flat colors, screenshots and transparency. For photographs it is the wrong container no matter where the slider sits.&lt;/p&gt;

&lt;h3&gt;
  
  
  The cost that never shows up in the file size
&lt;/h3&gt;

&lt;p&gt;6,016 x 6,016 x 4 bytes of RGBA is 144.8 MB just to have that image decoded in a browser tab. That is why client-side compressors stall or die on camera-original photos: decoding is the expensive step, not encoding, and looping over ten of them is how you run a mobile browser out of memory. Process one at a time, and downscale before anything else touches the pixels.&lt;/p&gt;

&lt;p&gt;I reproduce these numbers locally, but when I just need a resize-and-recompress without uploading a client's photo to a stranger's server, I use &lt;a href="https://codetoolbox.pro/tools/image-compressor.html" rel="noopener noreferrer"&gt;this browser-based image compressor&lt;/a&gt; — it runs on canvas, so the file never leaves the machine, and it strips EXIF (GPS included) on re-encode. That last part is a privacy win but also a real change: bake the orientation in before you strip the metadata, or sideways photos stay sideways.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>performance</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The __proto__ Key in an API Response Is a Merge Bug Waiting to Happen</title>
      <dc:creator>zhihu wu</dc:creator>
      <pubDate>Wed, 11 Nov 2026 13:03:24 +0000</pubDate>
      <link>https://dev.to/zhihu_wu_dea1d82af01a04d7/the-proto-key-in-an-api-response-is-a-merge-bug-waiting-to-happen-514i</link>
      <guid>https://dev.to/zhihu_wu_dea1d82af01a04d7/the-proto-key-in-an-api-response-is-a-merge-bug-waiting-to-happen-514i</guid>
      <description>&lt;p&gt;Every JSON formatter shows it plainly: a key named &lt;code&gt;__proto__&lt;/code&gt; sitting in the middle of an otherwise normal API response. It looks harmless because nothing crashes. That is exactly the problem — the bug shows up two files later, in the code that &lt;em&gt;copies&lt;/em&gt; the object.&lt;/p&gt;

&lt;p&gt;I tested this in Node 22 with one payload:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"__proto__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"polluted"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"yes"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"ok"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;JSON.parse()&lt;/code&gt; itself is safe. The key becomes an ordinary own property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                &lt;span class="c1"&gt;// [ '__proto__', 'ok' ]&lt;/span&gt;
&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;polluted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                    &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'__proto__'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So parsing is not the vulnerability. Copying is.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;merged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assign&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;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;merged&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;polluted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// 'yes' — inherited from the attacker's object&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;polluted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// undefined — the global prototype is still intact&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Object.assign&lt;/code&gt; performs a &lt;code&gt;[[Set]]&lt;/code&gt;, and &lt;code&gt;__proto__&lt;/code&gt; is an accessor defined on &lt;code&gt;Object.prototype&lt;/code&gt;, so the target's prototype is replaced. One object is compromised. A recursive merge is worse:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&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;k&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'object'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&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="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&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="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&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;return&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;merge&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;obj&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="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;polluted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// 'yes' — every object in the process now inherits it&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is prototype pollution, the class behind CVE-2018-3721 and CVE-2019-10744 in lodash's &lt;code&gt;merge&lt;/code&gt; (blocked by default since 4.17.11). In a Node service it escalates quietly: a polluted &lt;code&gt;Object.prototype&lt;/code&gt; can inject options into &lt;code&gt;child_process&lt;/code&gt; calls, template engines, or HTTP client defaults — RCE without a single line of attacker code running at parse time.&lt;/p&gt;

&lt;p&gt;Object spread is the safe path, because it uses CreateDataProperty instead of &lt;code&gt;[[Set]]&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt; &lt;span class="o"&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;polluted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// [ '__proto__', 'ok' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;structuredClone()&lt;/code&gt; keeps it as an own data property too. So "just use spread" is right for shallow copies and completely useless for a deep merge of nested defaults — which is what most config and webhook handlers actually do.&lt;/p&gt;

&lt;p&gt;Fix it once, at the boundary, instead of in every merge helper:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;safe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'__proto__'&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;v&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;Or merge into &lt;code&gt;Object.create(null)&lt;/code&gt; targets, or use a merge library that explicitly rejects &lt;code&gt;__proto__&lt;/code&gt;, &lt;code&gt;constructor&lt;/code&gt;, and &lt;code&gt;prototype&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Practical detection: format the payload and look at the keys. I use &lt;a href="https://codetoolbox.pro/tools/json-formatter.html" rel="noopener noreferrer"&gt;CodeToolbox's JSON formatter&lt;/a&gt; for this — the tree view lists &lt;code&gt;__proto__&lt;/code&gt; like any other key, which is the whole point: JSON itself has no idea the name is special. It runs entirely in the browser, so you can paste a production response without shipping it to a third party.&lt;/p&gt;

&lt;p&gt;Check your merge helper before the next webhook payload does it for you.&lt;/p&gt;

</description>
      <category>security</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>node</category>
    </item>
    <item>
      <title>chmod 755 Is Not 'Read, Write, Execute': 5 Permission Traps</title>
      <dc:creator>zhihu wu</dc:creator>
      <pubDate>Mon, 09 Nov 2026 13:03:32 +0000</pubDate>
      <link>https://dev.to/zhihu_wu_dea1d82af01a04d7/chmod-755-is-not-read-write-execute-5-permission-traps-27i2</link>
      <guid>https://dev.to/zhihu_wu_dea1d82af01a04d7/chmod-755-is-not-read-write-execute-5-permission-traps-27i2</guid>
      <description>&lt;p&gt;Every tutorial hands you the same two commands: &lt;code&gt;chmod 755&lt;/code&gt; for directories, &lt;code&gt;chmod 644&lt;/code&gt; for files. Both work. Neither explains what you just agreed to. I built a small permissions calculator while cleaning up a deploy script, and these are the traps that bit me hardest — every number below I re-ran in a shell today.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. &lt;code&gt;rwx&lt;/code&gt; on a directory is not about the files
&lt;/h2&gt;

&lt;p&gt;On a regular file the three bits are obvious. On a directory they mean something else entirely: &lt;code&gt;r&lt;/code&gt; lists names, &lt;code&gt;w&lt;/code&gt; creates and deletes entries, &lt;code&gt;x&lt;/code&gt; lets you traverse — &lt;code&gt;stat&lt;/code&gt; a child, &lt;code&gt;cd&lt;/code&gt; into it, open anything inside.&lt;/p&gt;

&lt;p&gt;I set a directory to &lt;code&gt;666&lt;/code&gt; and got exactly that behaviour: listing it returned an empty result without an error, creating a file inside failed with EACCES, and &lt;code&gt;cd&lt;/code&gt; into it failed too. Readable but useless. It is the same reason a "world-readable" web root without &lt;code&gt;x&lt;/code&gt; returns 403 for every file.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Symbolic &lt;code&gt;=&lt;/code&gt; replaces, &lt;code&gt;+&lt;/code&gt; adds
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;chmod g=r&lt;/code&gt; does not "add read for the group". It sets the group triad to exactly &lt;code&gt;r&lt;/code&gt;, dropping &lt;code&gt;w&lt;/code&gt; and &lt;code&gt;x&lt;/code&gt;. On a &lt;code&gt;750&lt;/code&gt; file that produced &lt;code&gt;740&lt;/code&gt; — verified.&lt;/p&gt;

&lt;p&gt;People reach for &lt;code&gt;chmod o=&lt;/code&gt; when they mean "remove other permissions" and get it right by accident. &lt;code&gt;chmod o=x&lt;/code&gt; on a file, though, is &lt;code&gt;--x&lt;/code&gt;: executable, unreadable, and it still runs.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. &lt;code&gt;X&lt;/code&gt; is the one you want with &lt;code&gt;-R&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Lowercase &lt;code&gt;x&lt;/code&gt; sets the execute bit on everything, which is how you end up with &lt;code&gt;.txt&lt;/code&gt; files marked executable (noisy diffs, scanners complaining, backups that churn). Capital &lt;code&gt;X&lt;/code&gt; adds execute only to directories and to files that already have at least one execute bit. On a test tree, &lt;code&gt;chmod -R a+X&lt;/code&gt; left the directories at &lt;code&gt;755&lt;/code&gt;, kept &lt;code&gt;run.sh&lt;/code&gt; at &lt;code&gt;755&lt;/code&gt;, and left &lt;code&gt;plain.txt&lt;/code&gt; at &lt;code&gt;644&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. umask is a mask, not a subtraction
&lt;/h2&gt;

&lt;p&gt;Your umask is ANDed with the complement of the requested mode. &lt;code&gt;umask 027&lt;/code&gt; gives files &lt;code&gt;640&lt;/code&gt; and directories &lt;code&gt;750&lt;/code&gt;, because &lt;code&gt;666 &amp;amp; ~027 = 640&lt;/code&gt; and &lt;code&gt;777 &amp;amp; ~027 = 750&lt;/code&gt;. Subtracting &lt;code&gt;027&lt;/code&gt; from &lt;code&gt;666&lt;/code&gt; gives &lt;code&gt;637&lt;/code&gt; — not what you get, and not a mode anyone meant.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. The leading digit grants privilege, not permission
&lt;/h2&gt;

&lt;p&gt;With four digits, the first one is &lt;code&gt;4&lt;/code&gt; setuid, &lt;code&gt;2&lt;/code&gt; setgid, &lt;code&gt;1&lt;/code&gt; sticky. &lt;code&gt;chmod 4755&lt;/code&gt; produces &lt;code&gt;-rwsr-xr-x&lt;/code&gt;, and I confirmed the setuid bit is set. A single transposed digit turns a hardening command into a privilege grant — &lt;code&gt;6554&lt;/code&gt; is not a typo of &lt;code&gt;755&lt;/code&gt; that fails loudly; it is setuid plus setgid and it applies cleanly. And on Linux, setuid on a shell script is ignored by the kernel, so the obvious "fix" for a script still runs as the invoking user.&lt;/p&gt;

&lt;p&gt;One bonus trap: file creation mode is &lt;code&gt;666 &amp;amp; ~umask&lt;/code&gt; — &lt;code&gt;644&lt;/code&gt; with &lt;code&gt;022&lt;/code&gt; — and redirection applies it. &lt;code&gt;cat id_rsa &amp;gt; /backup/id_rsa&lt;/code&gt; gives you &lt;code&gt;644&lt;/code&gt;, and ssh then refuses with "Permissions 0644 are too open". Use &lt;code&gt;install -m 600&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;When I'm unsure what a mode actually says, I check it in the calculator at &lt;a href="https://codetoolbox.pro/tools/chmod-calculator.html" rel="noopener noreferrer"&gt;https://codetoolbox.pro/tools/chmod-calculator.html&lt;/a&gt; — a 3×3 grid in, octal, symbolic and the exact &lt;code&gt;chmod&lt;/code&gt; command out, both directions. Faster than squinting at &lt;code&gt;ls -l&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>devops</category>
      <category>security</category>
      <category>webdev</category>
    </item>
    <item>
      <title>btoa() Is Lying to You: Base64, Unicode, and the Mojibake in Your JWT</title>
      <dc:creator>zhihu wu</dc:creator>
      <pubDate>Fri, 06 Nov 2026 13:02:45 +0000</pubDate>
      <link>https://dev.to/zhihu_wu_dea1d82af01a04d7/btoa-is-lying-to-you-base64-unicode-and-the-mojibake-in-your-jwt-bjd</link>
      <guid>https://dev.to/zhihu_wu_dea1d82af01a04d7/btoa-is-lying-to-you-base64-unicode-and-the-mojibake-in-your-jwt-bjd</guid>
      <description>&lt;p&gt;A four-character name broke my base64 encoder last week. Not the network, not the token — the string &lt;code&gt;café&lt;/code&gt;. Here is what I found after pulling the whole thing apart.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. &lt;code&gt;btoa()&lt;/code&gt; encodes bytes, and a JS string is not bytes
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;café&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// "Y2Fm6Q=="&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks right. Decode it somewhere that isn't JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;b64decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Y2Fm6Q==&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;caf&lt;/span&gt;&lt;span class="se"&gt;\xe9&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;b64decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Y2Fm6Q==&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;UnicodeDecodeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="n"&gt;codec&lt;/span&gt; &lt;span class="n"&gt;can&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;t decode byte 0xe9 in position 3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;café&lt;/code&gt; in UTF-8 is &lt;strong&gt;five&lt;/strong&gt; bytes (&lt;code&gt;c a f 0xC3 0xA9&lt;/code&gt;), so the correct base64 is &lt;code&gt;Y2Fmw6k=&lt;/code&gt;. &lt;code&gt;btoa&lt;/code&gt; took the code unit &lt;code&gt;é&lt;/code&gt; and emitted the single byte &lt;code&gt;0xE9&lt;/code&gt; — latin1, not UTF-8. And because &lt;code&gt;atob(btoa('café')) === 'café'&lt;/code&gt; is true, a JS-only unit test round-trips perfectly. The corruption only appears on the other side of the wire.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Above U+00FF it throws instead
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;中文&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;// InvalidCharacterError: Invalid character&lt;/span&gt;
&lt;span class="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;👍&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;// InvalidCharacterError: Invalid character&lt;/span&gt;
&lt;span class="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s1"&gt;u00ff&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// "/w=="  ← still fine&lt;/span&gt;
&lt;span class="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s1"&gt;u0100&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// InvalidCharacterError&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cutoff is exactly 255. Half of your user base is above it. Encode to UTF-8 bytes first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toB64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&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;TextEncoder&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;

&lt;span class="c1"&gt;// Node&lt;/span&gt;
&lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;base64&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For long strings, don't spread a 100k-element array into &lt;code&gt;fromCharCode&lt;/code&gt; — chunk it in 8 KB slices or your call stack will complain.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Decoding gives you latin1, not text
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;atob()&lt;/code&gt; returns a "binary string" where every char is one byte. Decode real UTF-8 base64 with it and you get &lt;code&gt;cafÃ©&lt;/code&gt; — the classic mojibake signature of &lt;code&gt;Ã©&lt;/code&gt;, &lt;code&gt;å¼&lt;/code&gt;, &lt;code&gt;â€"&lt;/code&gt;. Feed the bytes to &lt;code&gt;TextDecoder('utf-8')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is where JWTs bite. Take a payload with a Chinese name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;atob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;part&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// {"sub":"42","name":"å¼ ä¼","role":"admin"}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;JSON.parse&lt;/code&gt; succeeds. Nothing throws. You just quietly wrote a corrupted name into your database, your logs, and your audit trail — and it will sit there until someone greps for it.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. base64url is not base64
&lt;/h2&gt;

&lt;p&gt;JWT parts swap &lt;code&gt;+&lt;/code&gt; for &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt; for &lt;code&gt;_&lt;/code&gt;, and drop the padding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a?~&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;base64&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;// "YT9+"&lt;/span&gt;
&lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a?~&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;base64url&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// "YT9-"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;+&lt;/code&gt; in a query string arrives as a space; a &lt;code&gt;/&lt;/code&gt; changes how a router splits the path. Meanwhile &lt;code&gt;atob&lt;/code&gt; happily tolerates missing padding (&lt;code&gt;atob('eyJhIjoxfQ')&lt;/code&gt; works), so client-side code never notices — while a strict decoder on the server raises &lt;code&gt;Incorrect padding&lt;/code&gt;. Pad to a multiple of four before you hand a JWT segment to a standard decoder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The rule I now follow:&lt;/strong&gt; bytes at the boundary, text only at the edge. If decoded output looks like &lt;code&gt;cafÃ©&lt;/code&gt; or &lt;code&gt;å¼&lt;/code&gt;, the token is fine — the decoder guessed latin1. When I need to see both layers, I use &lt;a href="https://codetoolbox.pro/tools/base64.html" rel="noopener noreferrer"&gt;this base64 tool&lt;/a&gt; because it shows the decoded bytes &lt;em&gt;and&lt;/em&gt; the decoded text side by side, which makes a one-byte difference obvious instead of invisible.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>A 28-Character String Freezes Your Regex for 3.6 Seconds</title>
      <dc:creator>zhihu wu</dc:creator>
      <pubDate>Wed, 04 Nov 2026 13:05:57 +0000</pubDate>
      <link>https://dev.to/zhihu_wu_dea1d82af01a04d7/a-28-character-string-freezes-your-regex-for-36-seconds-2fi9</link>
      <guid>https://dev.to/zhihu_wu_dea1d82af01a04d7/a-28-character-string-freezes-your-regex-for-36-seconds-2fi9</guid>
      <description>&lt;p&gt;Your test suite passes, staging passes, and then one user submits a 28-character string and a Node process pins a core for 3.6 seconds.&lt;/p&gt;

&lt;p&gt;I measured this today on Node 22 (V8) with &lt;code&gt;/^(a+)+$/&lt;/code&gt; against an input of &lt;code&gt;n&lt;/code&gt; letters &lt;code&gt;a&lt;/code&gt; followed by a single &lt;code&gt;X&lt;/code&gt; that makes the match fail:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;input&lt;/th&gt;
&lt;th&gt;result&lt;/th&gt;
&lt;th&gt;time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;20 chars&lt;/td&gt;
&lt;td&gt;no match&lt;/td&gt;
&lt;td&gt;8.6 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;22 chars&lt;/td&gt;
&lt;td&gt;no match&lt;/td&gt;
&lt;td&gt;34.2 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;24 chars&lt;/td&gt;
&lt;td&gt;no match&lt;/td&gt;
&lt;td&gt;136.6 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;26 chars&lt;/td&gt;
&lt;td&gt;no match&lt;/td&gt;
&lt;td&gt;553.7 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;27 chars&lt;/td&gt;
&lt;td&gt;no match&lt;/td&gt;
&lt;td&gt;1,103.8 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;28 chars&lt;/td&gt;
&lt;td&gt;no match&lt;/td&gt;
&lt;td&gt;3,586.4 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Every extra character doubles the work. Twelve more characters is 2^12 = 4,096x, so the same 40-character input would take roughly four hours.&lt;/p&gt;

&lt;p&gt;The same pattern against &lt;code&gt;'a'.repeat(5000)&lt;/code&gt; with no trailing &lt;code&gt;X&lt;/code&gt; returns a match in &lt;strong&gt;0 ms&lt;/strong&gt;. That is the whole trap: catastrophic backtracking only fires when the match &lt;em&gt;fails&lt;/em&gt;. Happy-path tests never touch it. A malformed query param, a truncated field, or a name with one unexpected character does.&lt;/p&gt;

&lt;p&gt;The cause is ambiguity inside a nested quantifier. &lt;code&gt;(a+)+&lt;/code&gt; lets the engine split the same run of &lt;code&gt;a&lt;/code&gt;s in every possible way, roughly 2^(n-1) partitions for n characters, and "no match" means it has to try all of them before giving up.&lt;/p&gt;

&lt;p&gt;This is not a contrived pattern. A very ordinary email validator — &lt;code&gt;^([a-zA-Z0-9])(([-.]|[_]+)?([a-zA-Z0-9]+))*(@){1}[a-z0-9]+[.]{1}([a-z]{2,3})$&lt;/code&gt; — measured 0.4 ms at 16 characters, 12.5 ms at 24, and &lt;strong&gt;200.7 ms at 28&lt;/strong&gt; once I appended &lt;code&gt;!&lt;/code&gt;. Same shape, same failure mode.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three fixes that don't work
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A length limit is not enough.&lt;/strong&gt; 28 characters is short. Plenty of real input is that long.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An upper bound is not a fix.&lt;/strong&gt; &lt;code&gt;^(a+){1,64}$&lt;/code&gt; measured &lt;strong&gt;4,414 ms&lt;/strong&gt; at 26 characters, against 553.7 ms for the unbounded version. The 64-way outer loop multiplies the number of splits instead of capping the depth.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Possessive quantifiers don't exist in JavaScript.&lt;/strong&gt; &lt;code&gt;new RegExp('^a++$')&lt;/code&gt; throws &lt;code&gt;Invalid regular expression: /^a++$/: Nothing to repeat&lt;/code&gt;. Perl and PCRE have &lt;code&gt;a++&lt;/code&gt;; .NET has &lt;code&gt;Regex.MatchTimeout&lt;/code&gt;; PHP has &lt;code&gt;pcre.backtrack_limit&lt;/code&gt; (1,000,000 by default). V8 has neither a limit nor a timeout — it just blocks the event loop.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What actually works
&lt;/h2&gt;

&lt;p&gt;Rewrite the repetition so there is only one way to match. &lt;code&gt;^a+$&lt;/code&gt; rejected the failure case on &lt;strong&gt;1,000,000&lt;/strong&gt; characters in 3.2 ms — three orders of magnitude faster on input 35,000 times longer.&lt;/p&gt;

&lt;p&gt;If you cannot rewrite it, emulate an atomic group with a lookahead and a backreference: &lt;code&gt;/^(?=(a+))\1$/&lt;/code&gt; ran in &lt;strong&gt;0 ms&lt;/strong&gt; on the exact 27-character input that took 1,103.8 ms, and still returns the right answer for &lt;code&gt;"aaa"&lt;/code&gt; (true), &lt;code&gt;"a"&lt;/code&gt; (true), &lt;code&gt;""&lt;/code&gt; (false) and &lt;code&gt;"aab"&lt;/code&gt; (false).&lt;/p&gt;

&lt;p&gt;Finally, bound the blast radius: cap input length, run untrusted patterns inside a worker with a timeout, or use RE2, which is a linear-time engine by construction.&lt;/p&gt;

&lt;p&gt;When I sanity-check a pattern I keep &lt;a href="https://codetoolbox.pro/tools/regex-tester.html" rel="noopener noreferrer"&gt;this regex tester&lt;/a&gt; open and test the &lt;strong&gt;failing&lt;/strong&gt; path on purpose — a long valid prefix plus one invalid trailing character. If the tab hangs, so will your server.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>security</category>
      <category>performance</category>
    </item>
    <item>
      <title>Your Cache Key Has a Collision You Designed Yourself</title>
      <dc:creator>zhihu wu</dc:creator>
      <pubDate>Sun, 01 Nov 2026 13:01:43 +0000</pubDate>
      <link>https://dev.to/zhihu_wu_dea1d82af01a04d7/your-cache-key-has-a-collision-you-designed-yourself-5e75</link>
      <guid>https://dev.to/zhihu_wu_dea1d82af01a04d7/your-cache-key-has-a-collision-you-designed-yourself-5e75</guid>
      <description>&lt;p&gt;Two tenants, two cache entries, one digest. The hash function did its job perfectly — the key you fed it was ambiguous.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Concatenation erases field boundaries
&lt;/h2&gt;

&lt;p&gt;The classic cache key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tenant&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now try two unrelated records:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;acme&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;42&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# 7c187954…3c1fda
&lt;/span&gt;&lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;acm&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;e42&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 7c187954…3c1fda
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Identical. The pair &lt;code&gt;("acme", "42")&lt;/code&gt; and &lt;code&gt;("acm", "e42")&lt;/code&gt; both become &lt;code&gt;acme42&lt;/code&gt;, so tenant &lt;code&gt;acm&lt;/code&gt; reads tenant &lt;code&gt;acme&lt;/code&gt;'s cached profile. The same thing happens with &lt;code&gt;first + last&lt;/code&gt; (&lt;code&gt;"john"&lt;/code&gt; + &lt;code&gt;"smith@x.com"&lt;/code&gt; == &lt;code&gt;"johns"&lt;/code&gt; + &lt;code&gt;"mith@x.com"&lt;/code&gt;) and with any two-variable signature you build by pasting strings together.&lt;/p&gt;

&lt;p&gt;The digest is collision-resistant; the &lt;em&gt;encoding&lt;/em&gt; is not injective. Fix it at the boundary: join with a separator that cannot appear in the parts, length-prefix each field (&lt;code&gt;3:acme2:42&lt;/code&gt;), or hash a canonical serialization. &lt;code&gt;sha256("acme" + "-" + "42")&lt;/code&gt; gives &lt;code&gt;d3f5d66e…&lt;/code&gt; — a different key, and one that no other pair can produce.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The same object, two different hashes
&lt;/h2&gt;

&lt;p&gt;If you hash a JSON payload, key order and whitespace are part of the bytes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;              &lt;span class="c1"&gt;// {"a": 1, "b": 2}   → d8497d9d…&lt;/span&gt;
&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;              &lt;span class="c1"&gt;// {"b": 2, "a": 1}   → d5b8aed2…&lt;/span&gt;
&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;// {"a":1,"b":2}      → 43258cff…&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same data, three digests. Two services "hashing the payload" will disagree forever, and a signature computed in Node will never verify against one computed in Go. Canonicalize before hashing: sort keys recursively and use compact separators (or RFC 8785 JCS). Compare canonical bytes, not objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. A digest is bytes, not a string
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hex vs Base64.&lt;/strong&gt; A 32-byte digest is 64 hex chars, or 44 chars in Base64 (&lt;code&gt;LXEWQrcmsEQBYnyp+6wy9chTD7GQPMTbAiWHF5IaSIE=&lt;/code&gt;). Base64url drops the &lt;code&gt;=&lt;/code&gt; → 43. Strip padding on one side only and every comparison fails.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Case.&lt;/strong&gt; Some libraries upper-case hex; &lt;code&gt;digest()&lt;/code&gt; returns lower-case.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tool output is not the digest.&lt;/strong&gt; &lt;code&gt;openssl dgst -sha256&lt;/code&gt; prints &lt;code&gt;SHA2-256(stdin)= 2cf24dba…&lt;/code&gt;, while &lt;code&gt;shasum -a 256&lt;/code&gt; prints the bare hex plus a filename. Comparing whole output lines is a guaranteed mismatch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And when the digest &lt;em&gt;is&lt;/em&gt; a MAC, compare it in constant time — &lt;code&gt;hmac.compare_digest&lt;/code&gt; or &lt;code&gt;crypto.timingSafeEqual&lt;/code&gt; — not with &lt;code&gt;==&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Never sign with &lt;code&gt;hash(secret + message)&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;SHA-256 is Merkle–Damgård, so anyone holding &lt;code&gt;hash(secret || message)&lt;/code&gt; can append their own suffix and recompute a valid digest without the secret (length extension). Use HMAC-SHA256, which is what the signature you're debugging almost certainly expects.&lt;/p&gt;

&lt;p&gt;If you need to eyeball a digest while debugging, &lt;a href="https://codetoolbox.pro/tools/hash-generator.html" rel="noopener noreferrer"&gt;this SHA-256 tool&lt;/a&gt; runs entirely in the browser — paste the exact bytes, pick hex or Base64, and copy the digest without an &lt;code&gt;openssl&lt;/code&gt; prefix sneaking into your comparison.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>security</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Four Decisions Your CSV Parser Makes Without Asking</title>
      <dc:creator>zhihu wu</dc:creator>
      <pubDate>Sat, 31 Oct 2026 13:03:26 +0000</pubDate>
      <link>https://dev.to/zhihu_wu_dea1d82af01a04d7/four-decisions-your-csv-parser-makes-without-asking-4m9m</link>
      <guid>https://dev.to/zhihu_wu_dea1d82af01a04d7/four-decisions-your-csv-parser-makes-without-asking-4m9m</guid>
      <description>&lt;p&gt;Every CSV import I have debugged failed the same way. Not a crash, not a bad encoding — a parser that quietly made a type decision on my behalf, and made it wrong. Here are the four decisions worth making explicit, with the actual JS behaviour so you can check them yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. An empty cell becomes &lt;code&gt;0&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;// 0  — not NaN&lt;/span&gt;
&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;  &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So a row of &lt;code&gt;{ id: 1, score: "" }&lt;/code&gt; turns into &lt;code&gt;score: 0&lt;/code&gt;, and the bug does not surface until someone looks at an average. A mean over "1, 2, 3, nothing" now divides by four. Worse, a filter like &lt;code&gt;rows.filter(r =&amp;gt; r.score &amp;gt; -1)&lt;/code&gt; starts including customers who never rated anything.&lt;/p&gt;

&lt;p&gt;In CSV there is no null. An empty cell means "unknown", and the only honest JSON is &lt;code&gt;null&lt;/code&gt;. Check &lt;code&gt;cell === ""&lt;/code&gt; before you touch &lt;code&gt;Number&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. &lt;code&gt;parseFloat&lt;/code&gt; reads a prefix, &lt;code&gt;Number&lt;/code&gt; reads the whole string
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;parseFloat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;12kg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// 12&lt;/span&gt;
&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;12kg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;// NaN&lt;/span&gt;
&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0x10&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;// 16    (hex literals are numbers)&lt;/span&gt;
&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1e5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;// 100000 (a SKU that looks like exponent notation)&lt;/span&gt;
&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;007&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;// 7     (a ZIP code)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four different failure modes from two one-line functions. The lesson is not "pick the better one" — it is that the decision belongs to the column, not the characters. A column named &lt;code&gt;order_id&lt;/code&gt; or &lt;code&gt;zip&lt;/code&gt; is a string no matter how numeric it looks.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. JSON numbers are doubles: the 15-digit wall
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Number.MAX_SAFE_INTEGER&lt;/code&gt; is &lt;code&gt;9007199254740991&lt;/code&gt; — sixteen digits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{"id":1234567890123456789}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
&lt;span class="c1"&gt;// 1234567890126800&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The damage happens at parse time, so stringifying afterwards cannot undo it. Any identifier longer than 15 digits — Stripe-style keys, snowflake IDs, IBANs — must cross the CSV→JSON boundary as a string.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Quoting is not optional, and Excel adds two non-standard extras
&lt;/h2&gt;

&lt;p&gt;RFC 4180 allows a field to contain the delimiter, a doubled quote, and a newline. This is one record, not two:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id,note
1,"line1
line2"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;split("\n")&lt;/code&gt; then &lt;code&gt;split(",")&lt;/code&gt; gives you a phantom row and shifts every later column — the import "succeeds" with garbage. Then there are two Excel quirks that are not in the spec at all: a UTF-8 BOM before the first header (your key is &lt;code&gt;\uFEFFid&lt;/code&gt;, not &lt;code&gt;id&lt;/code&gt;) and, on locale-configured machines, a &lt;code&gt;sep=;&lt;/code&gt; first line that becomes a header of its own.&lt;/p&gt;

&lt;p&gt;CSV is untyped and JSON is typed, so the conversion is exactly where these assumptions get baked in. I keep a browser-side converter for this that only coerces plain decimals with no leading zero and leaves IDs alone: &lt;a href="https://codetoolbox.pro/tools/csv-to-json.html" rel="noopener noreferrer"&gt;codetoolbox.pro/tools/csv-to-json.html&lt;/a&gt; — it runs locally, so customer exports never leave your machine. Decide per column, not per character.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Three Hex Color Bugs That Survive Code Review</title>
      <dc:creator>zhihu wu</dc:creator>
      <pubDate>Thu, 29 Oct 2026 13:02:17 +0000</pubDate>
      <link>https://dev.to/zhihu_wu_dea1d82af01a04d7/three-hex-color-bugs-that-survive-code-review-3bdm</link>
      <guid>https://dev.to/zhihu_wu_dea1d82af01a04d7/three-hex-color-bugs-that-survive-code-review-3bdm</guid>
      <description>&lt;p&gt;Designers hand off hex strings. Developers paste them. And three times out of ten the string is fine and the render is still wrong — because a hex value is not a color, it's a set of numbers that each platform reads differently.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Eight digits, two different channel orders
&lt;/h2&gt;

&lt;p&gt;CSS defines 8-digit hex as &lt;code&gt;#RRGGBBAA&lt;/code&gt; — alpha last. Android, Flutter and Kotlin define it as &lt;code&gt;#AARRGGBB&lt;/code&gt; — alpha first. So &lt;code&gt;#80FF0000&lt;/code&gt; is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CSS:&lt;/strong&gt; red &lt;code&gt;0x80&lt;/code&gt;, green &lt;code&gt;0xFF&lt;/code&gt;, blue &lt;code&gt;0x00&lt;/code&gt;, alpha &lt;code&gt;0x00&lt;/code&gt; → completely invisible. Your UI element quietly disappears.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Android:&lt;/strong&gt; alpha &lt;code&gt;0x80&lt;/code&gt;, red &lt;code&gt;0xFF&lt;/code&gt;, green &lt;code&gt;0x00&lt;/code&gt;, blue &lt;code&gt;0x00&lt;/code&gt; → a 50% red overlay.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Same literal, opposite meaning, no error anywhere. Six-digit hex is safe because all three channels are present in the same order; the moment you add the fourth pair you have an ambiguous string. Hand off &lt;code&gt;rgba(255, 0, 0, 0.5)&lt;/code&gt; or a 6-digit hex plus a separate opacity token instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Alpha is not a color (so don't pre-blend it)
&lt;/h2&gt;

&lt;p&gt;Converting a translucent overlay into a solid hex "so it fits the palette" only works against one background:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;rgba(0, 0, 0, 0.5)&lt;/code&gt; over &lt;code&gt;#FFFFFF&lt;/code&gt; → &lt;code&gt;#808080&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rgba(0, 0, 0, 0.5)&lt;/code&gt; over &lt;code&gt;#3366CC&lt;/code&gt; → &lt;code&gt;#1A3366&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pre-blending bakes in the surface it happened to sit on. The same 50% black is mid-grey on a white card and dark navy on a blue hero. Keep alpha as alpha, or record which surface the hex was measured against.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Hex carries no color space
&lt;/h2&gt;

&lt;p&gt;A hex code says nothing about gamut. The same &lt;code&gt;#FF0000&lt;/code&gt; is a different physical color on an sRGB laptop panel and a Display P3 phone — and a color you sample from a wide-gamut screenshot is P3 coordinates wearing an sRGB costume, which is why saturated colors read duller or hotter than the designer's file. CSS &lt;code&gt;color(display-p3 …)&lt;/code&gt; exists precisely because 8-bit hex can't express what those screens can show.&lt;/p&gt;

&lt;p&gt;None of these are exotic edge cases — they show up in handoff, in dark-mode overlays, and in "the color looks different on my machine". What they share is that the number is right and the format is wrong.&lt;/p&gt;

&lt;p&gt;💡 When I'm chasing one of these, I use the &lt;a href="https://codetoolbox.pro/tools/color-picker.html" rel="noopener noreferrer"&gt;CodeToolbox color picker&lt;/a&gt; to see the same color as HEX, RGB, HSL and oklch side by side — it's client-side, so I can paste in a value without uploading a screenshot anywhere.&lt;/p&gt;

&lt;p&gt;Which one has bitten you? For me it's #2 — every translucent overlay we ever "simplified" into a hex eventually sat on a different background.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>javascript</category>
      <category>design</category>
    </item>
    <item>
      <title>Your UUID Column Is 9x Bigger Than It Needs to Be</title>
      <dc:creator>zhihu wu</dc:creator>
      <pubDate>Mon, 26 Oct 2026 13:01:49 +0000</pubDate>
      <link>https://dev.to/zhihu_wu_dea1d82af01a04d7/your-uuid-column-is-9x-bigger-than-it-needs-to-be-2omi</link>
      <guid>https://dev.to/zhihu_wu_dea1d82af01a04d7/your-uuid-column-is-9x-bigger-than-it-needs-to-be-2omi</guid>
      <description>&lt;p&gt;A UUID is 16 bytes. In a surprising number of schemas, it occupies 144 bytes — and the index copy occupies 144 more.&lt;/p&gt;

&lt;p&gt;That's not a rounding error. It's the difference between an index that fits in memory and one that doesn't.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 16-byte value stored as 144 bytes
&lt;/h2&gt;

&lt;p&gt;If your ORM maps a UUID to &lt;code&gt;VARCHAR(36)&lt;/code&gt; or &lt;code&gt;CHAR(36)&lt;/code&gt; — extremely common, because the string form is what the API returns — MySQL with &lt;code&gt;utf8mb4&lt;/code&gt; reserves 36 × 4 = &lt;strong&gt;144 bytes per value&lt;/strong&gt;. Put an index on it and the index stores the same 144 bytes per entry again, plus row overhead.&lt;/p&gt;

&lt;p&gt;An 8 KB index page holds roughly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;~54 keys for &lt;code&gt;CHAR(36)&lt;/code&gt; utf8mb4 (144 + ~6 bytes overhead)&lt;/li&gt;
&lt;li&gt;~372 keys for &lt;code&gt;BINARY(16)&lt;/code&gt; (16 + ~6 bytes)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So a one-million-row index is ~16 MB in the compact form and ~144 MB in the string form — nine times the I/O to walk it, nine times the buffer pool it occupies, and string comparison (&lt;code&gt;memcmp&lt;/code&gt; over up to 36 bytes) instead of a single 16-byte compare.&lt;/p&gt;

&lt;p&gt;Postgres escapes this: its native &lt;code&gt;uuid&lt;/code&gt; type is 16 bytes, so the damage there is usually a &lt;code&gt;text&lt;/code&gt; column chosen by habit. For MySQL the fix is one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;BINARY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;
&lt;span class="c1"&gt;-- write:  UUID_TO_BIN(?, 1)   -- the 1 swaps the time fields for index locality&lt;/span&gt;
&lt;span class="c1"&gt;-- read:   BIN_TO_UUID(id, 1)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The duplicate you fear isn't the one you'll get
&lt;/h2&gt;

&lt;p&gt;Nobody gets UUID collisions. A v4 UUID has 122 random bits: at a billion stored UUIDs the birthday probability is about 9 × 10⁻²⁰. The duplicates people actually hit come from their own code — a &lt;code&gt;Math.random()&lt;/code&gt;-seeded generator, a column with a &lt;code&gt;''&lt;/code&gt; default, or a "friendly" truncated ID.&lt;/p&gt;

&lt;p&gt;Truncation is worth doing the math on, because it fails sooner than people expect. An 8-hex-character prefix is 32 bits, and the birthday bound puts a 50% chance of a collision at roughly &lt;strong&gt;77,000 rows&lt;/strong&gt;. Twelve characters (48 bits) pushes that to ~20 million. If you show short IDs in URLs, generate them deliberately (Base62, sized to the row count you'll ever have) instead of slicing a UUID — and note that a &lt;em&gt;time-ordered&lt;/em&gt; v7 UUID is worse for this, because every ID created in the same window shares its leading timestamp characters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Random inserts are the other cost
&lt;/h2&gt;

&lt;p&gt;v4's first bytes are random, so each insert lands on an arbitrary B-tree leaf. Once the table outgrows cache, every insert becomes a read-miss on a cold page plus a split — which is why write throughput falls off a cliff at a few million rows even though "UUIDs are just 16 bytes."&lt;/p&gt;

&lt;p&gt;UUID v7 (or ULID) fixes this by making the timestamp the high-order bits, so inserts append at the right edge. The tradeoff is real: v7 leaks creation time, so don't use it for IDs that must not reveal when something happened.&lt;/p&gt;

&lt;p&gt;Rule of thumb: store 16 bytes, index a time-ordered value, and never compare UUIDs as truncated strings.&lt;/p&gt;

&lt;p&gt;If you need to generate, format, or sanity-check UUIDs while wiring this up, I keep a small local-only generator here — nothing is uploaded: &lt;a href="https://codetoolbox.pro/tools/uuid-generator.html" rel="noopener noreferrer"&gt;https://codetoolbox.pro/tools/uuid-generator.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>database</category>
      <category>sql</category>
      <category>performance</category>
    </item>
    <item>
      <title>Your String Length Is a Lie: Four Unicode Traps in Everyday Code</title>
      <dc:creator>zhihu wu</dc:creator>
      <pubDate>Sat, 24 Oct 2026 13:01:57 +0000</pubDate>
      <link>https://dev.to/zhihu_wu_dea1d82af01a04d7/your-string-length-is-a-lie-four-unicode-traps-in-everyday-code-2p82</link>
      <guid>https://dev.to/zhihu_wu_dea1d82af01a04d7/your-string-length-is-a-lie-four-unicode-traps-in-everyday-code-2p82</guid>
      <description>&lt;p&gt;Every character counter you have ever written is wrong for at least one of your users. Here are the four Unicode traps I keep finding in code reviews — each one verified in Node, not recalled from a blog post.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. &lt;code&gt;.length&lt;/code&gt; counts UTF-16 code units, not characters
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u{1F44D}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;                    &lt;span class="c1"&gt;// 2  (one thumbs up)&lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u{1F1E8}&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u{1F1F3}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;           &lt;span class="c1"&gt;// 4  (one flag)&lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u{1F468}&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u200D&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u{1F469}&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u200D&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u{1F467}&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u200D&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;u{1F466}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;  &lt;span class="c1"&gt;// 11 (one family)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Code points are closer: &lt;code&gt;[...str].length&lt;/code&gt; gives 7 for that family. But only grapheme clusters give the real answer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Intl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Segmenter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;granularity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;grapheme&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)].&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;  &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dangerous part is slicing. &lt;code&gt;str.substring(0, 1)&lt;/code&gt; on a thumbs up returns a lone high surrogate. Encode that and you get 3 bytes of replacement character — no exception, no warning, just corrupted data in your database.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. "é" is sometimes two characters
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nfc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;é&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;NFC&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// 1 unit, 2 bytes&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nfd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;é&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;NFD&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// 2 units, 3 bytes&lt;/span&gt;
&lt;span class="nx"&gt;nfc&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;nfd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                        &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Identical pixels, different strings. &lt;code&gt;new Map([[nfd, 1]]).has(nfc)&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;, so dedupe, rate-limit keys, email lookups and "already exists" checks quietly miss. macOS filenames arrive decomposed, so this is not a theoretical case. Normalize to NFC at the boundary, before hashing or comparing.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Invisible characters survive &lt;code&gt;trim()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;"  \u200B  ".trim().length&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt; — a zero-width space is not whitespace in JavaScript, and &lt;code&gt;/\s/.test("\u200B")&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;. A non-breaking space, by contrast, &lt;em&gt;is&lt;/em&gt; stripped. So text pasted from a browser can look perfectly clean and still not match.&lt;/p&gt;

&lt;p&gt;Markdown is where it hurts most. I tested rendered output in a GFM previewer: a "blank" line containing only &lt;code&gt;U+00A0&lt;/code&gt; does &lt;strong&gt;not&lt;/strong&gt; split paragraphs, and a line where the space before &lt;code&gt;#&lt;/code&gt; is &lt;code&gt;U+00A0&lt;/code&gt; never becomes a heading — it renders as &lt;code&gt;&amp;lt;p&amp;gt; # Title&amp;lt;/p&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Bytes are a third number
&lt;/h2&gt;

&lt;p&gt;The same family emoji is 11 UTF-16 units, 7 code points, 1 grapheme and &lt;strong&gt;25 UTF-8 bytes&lt;/strong&gt;. Column lengths, index key limits (utf8mb4 reserves 4 bytes per character), and base64 payloads all run on bytes, so a 768-character field is not a 768-byte field.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rule of thumb:&lt;/strong&gt; units for strings, code points for iteration, graphemes for UI, bytes for storage.&lt;/p&gt;

&lt;p&gt;When I need to find which of these is biting me, I dump the string into a character-level inspector — I use &lt;a href="https://codetoolbox.pro/tools/unicode-inspector.html" rel="noopener noreferrer"&gt;Unicode Inspector&lt;/a&gt;, which runs entirely in the browser and shows the code point, UTF-16 units, UTF-8 bytes and entity for every character, plus filters for invisible ones. Paste a failing value in, find the &lt;code&gt;U+200B&lt;/code&gt; you could not see, done.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Your Amortization Loop Never Reaches Zero: 4 Bugs in Loan Math Code</title>
      <dc:creator>zhihu wu</dc:creator>
      <pubDate>Mon, 19 Oct 2026 13:02:41 +0000</pubDate>
      <link>https://dev.to/zhihu_wu_dea1d82af01a04d7/your-amortization-loop-never-reaches-zero-4-bugs-in-loan-math-code-n39</link>
      <guid>https://dev.to/zhihu_wu_dea1d82af01a04d7/your-amortization-loop-never-reaches-zero-4-bugs-in-loan-math-code-n39</guid>
      <description>&lt;p&gt;If you've ever generated an amortization schedule by hand, you've probably hit this: you loop 360 times and the balance lands on... -$3.07. Or +$0.84. Never exactly zero.&lt;/p&gt;

&lt;p&gt;Here are four bugs I keep running into, with numbers you can check yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The loop's last step is a lie
&lt;/h2&gt;

&lt;p&gt;$300,000 at 7% over 360 months. The exact payment is 1,995.9074..., which you round to 1,995.91. Run &lt;code&gt;balance = balance + balance*r - payment&lt;/code&gt; 360 times and the balance ends at &lt;strong&gt;-$3.07&lt;/strong&gt; - you overpaid by three dollars because you rounded the payment up. Round down and you finish with a small balance the borrower still owes.&lt;/p&gt;

&lt;p&gt;Switching to integer cents doesn't fix it; it just relocates the error (same loan, same 360 iterations: -355 cents).&lt;/p&gt;

&lt;p&gt;The fix is to stop trusting the last iteration. After payment 359 the balance is $1,981.28, so the final payment must be &lt;code&gt;balance * (1 + r)&lt;/code&gt; = &lt;strong&gt;$1,992.84&lt;/strong&gt;. Lenders do exactly this: a fixed payment for every month except the last.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Accruing interest after subtracting the payment
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// wrong&lt;/span&gt;
&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// right&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both look reasonable. They differ by one period of interest on the payment itself, and the first version "pays off" in 354 payments instead of 360 while reporting $406,552 of total interest instead of $418,527 - &lt;strong&gt;$11,975 short&lt;/strong&gt;. Nobody notices, because the schedule still ends near zero.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. setMonth() drifts
&lt;/h2&gt;

&lt;p&gt;A schedule assumes "the same day each month". JavaScript disagrees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2026&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;setMonth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// -&amp;gt; Mar 3, not Feb 28&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A 30-year schedule built month-by-month slides forward every time it crosses a short month. The dates in the table look plausible; they're just wrong by a few days that keep accumulating.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. "Extra payment" is not principal by default
&lt;/h2&gt;

&lt;p&gt;Many servicers apply extra money to your &lt;strong&gt;next installment&lt;/strong&gt;, which only advances the due date. The loan still runs the full term and total interest is unchanged. You have to mark it principal-only, then verify on the next statement that the principal balance actually dropped.&lt;/p&gt;

&lt;p&gt;Get it right and the payoff is real: $100/month extra on that $300k loan clears it in 310 payments - about 4.2 years early, roughly $69,300 of interest saved.&lt;/p&gt;

&lt;p&gt;I keep a free &lt;a href="https://codetoolbox.pro/tools/loan-calculator.html" rel="noopener noreferrer"&gt;loan calculator&lt;/a&gt; open for checking these by hand - it runs entirely in the browser, shows the full schedule and the year-end summaries, which is how I caught the rounding bug in my own script.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
