Decode UTF-16 .env files on variable import - #3187
Conversation
File.text() always decodes UTF-8, but .env files written on Windows are often UTF-16 (PowerShell's > redirect defaults to it). Decoded as UTF-8, every character gains an interleaved NUL byte, so an imported key was stored with a NUL after every letter - an invalid env var name that broke every subsequent deployment for the resource. readEnvFile() detects UTF-16 by BOM, or by the interleaved-NUL pattern when the BOM is missing, and decodes accordingly; both import modals now use it instead of File.text().
Console (appwrite/console)Project ID: Sites (1)
Tip HTTPS and SSL certificates are handled automatically for all your Sites |
Greptile SummaryAdds encoding-aware reading for uploaded
Confidence Score: 4/5The PR does not appear safe to merge until BOM-less UTF-16 files with Unicode-heavy content are decoded correctly. The previously reported failure remains: the file-wide 70% NUL threshold misses realistic BOM-less UTF-16 files containing substantial Unicode text, after which existing validation rejects the NUL-interleaved variable names. Files Needing Attention: src/lib/helpers/envfile.ts, src/lib/helpers/envfile.test.ts Important Files Changed
Reviews (2): Last reviewed commit: "test: build UTF-16 fixtures as ArrayBuff..." | Re-trigger Greptile |
| const units = buffer.length / 2; | ||
| if (oddNuls > units * 0.7) { | ||
| encoding = 'utf-16le'; | ||
| } else if (evenNuls > units * 0.7) { | ||
| encoding = 'utf-16be'; | ||
| } |
There was a problem hiding this comment.
UTF-16 heuristic misses Unicode
When a BOM-less UTF-16 file contains at least 30% non-ASCII code units in its values or comments, the file-wide NUL ratio falls below this threshold and readEnvFile decodes it as UTF-8. This leaves interleaved NULs in ASCII keys, causing variable validation to reject the entire import.

What does this PR do?
File.text()always decodes UTF-8, but.envfiles written on Windows are often UTF-16 -- PowerShell's>redirect defaults to it. Decoded as UTF-8, every character gains an interleaved NUL byte, so importing such a file stored keys with a NUL after every letter -- an invalid environment variable name that invalidated the Kubernetes build job manifest and failed every subsequent deployment for that resource (this is the origin of a production incident on cloud).This adds
readEnvFile()to$lib/helpers/envfile: it detects UTF-16 by BOM (FF FE/FE FF), or by the interleaved-NUL byte pattern when the BOM is missing, and decodes with the rightTextDecoder. Both import modals (importVariablesModal,uploadVariablesModal) now use it instead ofFile.text()-- so a PowerShell-authored.envsimply imports correctly instead of erroring on every key.Test plan
src/lib/helpers/envfile.test.ts-- 7 cases: UTF-8 (with/without BOM), UTF-16LE/BE (with/without BOM), and UTF-8 containing a genuine stray NUL staying UTF-8. All passing; lint clean.Related PRs and Issues