Skip to content

config: a config.json (and the other JSON files read past a BOM) saved as UTF-16 is read, not refused as invalid JSON - #276

Open
connorburgess wants to merge 3 commits into
spacesheep-dev:mainfrom
connorburgess:fix/config-utf16-bom
Open

connorburgess wants to merge 3 commits into
spacesheep-dev:mainfrom
connorburgess:fix/config-utf16-bom

Conversation

@connorburgess

@connorburgess connorburgess commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Updated against v0.8.18 / main d16a2c01 on 2026-12-05. Current validation and refresh details.

What

A config.json saved as UTF-16 with a byte-order mark is now read. Before this change the GUI stopped every command with "config.json is not valid JSON, so it was not written over", though Notepad shows valid JSON. The same holds for the other files the GUI already reads past a UTF-8 byte-order mark: control/machines.json and the queue files, labels.json / marks.json / limits.json, color themes, and view, skin and plugin manifests. The next write of config.json is UTF-8 without a mark, as with a UTF-8 mark (#92).

Why

Windows PowerShell 5.1 writes UTF-16LE with a byte-order mark (FF FE) for ConvertTo-Json | Out-File config.json and for >. The README and the authoring guides tell people to edit config.json by hand (disabled, enabled, the key). readConfigFile read the file as UTF-8, so the text began ��{\0"\0… and did not parse. The error gave no position and no cause the person could see.

config.ts gains readText(file):

  • A file that starts with FF FE is decoded as UTF-16LE.
  • A file that starts with FE FF (-Encoding BigEndianUnicode) is byte-swapped and then decoded as UTF-16LE. This avoids a utf-16be decoder, which a Node built without full ICU does not have.
  • Any other file is read as UTF-8 past a UTF-8 mark, as withoutBom did.

A UTF-16 file without a mark is not guessed at: it still does not parse. A cut-short UTF-16 file (an odd number of bytes) ends in U+FFFD, so it is refused and left byte for byte. Neither FF FE nor FE FF can start valid UTF-8, so no file that read before reads differently now.

Every reader that went through withoutBom now goes through readText:

  • readConfigFile (config.ts). readGuiConfig, writeChanged, writeGuiConfig, key renew and the search CLI's history setting all read through it.
  • The receiver's live re-read of config.json (app.ts fileText). It is compared with ownText / lastRead, which are now both decoded text. A file that holds the same JSON in another encoding starts nothing, as an equal file did before.
  • readMachinesFile and readJson (queue files) in control/state.ts.
  • docFile (labels, marks, limits).
  • readColorThemes.
  • readManifestOf (view, skin and plugin manifests).

writeIfOther still compares raw UTF-8 bytes, so a UTF-16 file is never taken for "already holds this" and the next write converts it.

Not changed here:

  • readLabels in server/search/cli.ts reads labels.json with a plain JSON.parse and never read past a UTF-8 mark either (named as a follow-up in ui: a labels.json, marks.json or limits.json with a byte-order mark is read past it #171). Adding mark handling there is a separate change to the search CLI.
  • Other plain JSON readers (plugins/settings.ts, plugins/host.ts, runfile.ts, localDoor.ts, sources/outbox.ts, ui/skillsCore.ts, control/files.ts) read files the GUI writes itself, not files people are told to edit.

Shared files with my open pull requests: #269 also touches server/skins/skins.test.ts. git merge-tree --write-tree of this branch with #269's head (1bc8af9) has no conflicts.

An entry is now included under Unreleased in CHANGELOG.md. Other open PRs also add release notes; those entries may need combining as the PRs land.

Original proof (before the v0.8.18 refresh)

New tests:

  • server/config.test.ts: reads a UTF-16LE and a UTF-16BE config.json with CRLF and a non-ASCII value. The read leaves the file byte for byte; the next write is UTF-8 and keeps the values. A cut-short UTF-16 file and UTF-16 without a mark stop with "is not valid JSON", leaving the file byte for byte and never quoting the token.
  • server/keyRules.test.ts: a key written by hand as UTF-16 while the receiver runs is taken.
  • server/control/machines-file.test.ts: a UTF-16 machines.json keeps its machine and queue, and a UTF-16 queue file is kept (on main it was deleted at start).
  • server/ui/docFile.test.ts, server/ui/ui.test.ts, server/skins/skins.test.ts: a UTF-16 document, color theme and skin.json are read.

With the six source files from origin/main (6165bed), the same tests:

     × reads a file saved as UTF-16 with a byte-order mark, as PowerShell 5.1's Out-File writes it 2ms
     × reads a machines.json saved as UTF-16 with a byte-order mark, as PowerShell 5.1's Out-File writes it 7ms
     × keeps a machine's queue file saved as UTF-16 with a byte-order mark 9ms
     × reads a config.json saved as UTF-16 with a byte-order mark, and writes it back as UTF-8 3ms
     × readSkinManifest reads a skin.json saved as UTF-16 with a byte-order mark, as PowerShell 5.1's Out-File writes one 10ms
     × reads a color theme saved as UTF-16 with a byte-order mark, as PowerShell 5.1's Out-File writes one 6ms
     × a key written by hand as UTF-16 (PowerShell 5.1's Out-File) is taken while running 10075ms
 Test Files  6 failed (6)
      Tests  7 failed | 69 passed (76)

On main, config.json fails with ConfigError: …/config.json is not valid JSON, so it was not written over, and the manifest, theme and document fail with Unexpected token '�', "��{ " … is not valid JSON. The "cut short or without a byte-order mark" test passes on both main and this branch; it guards against guessing.

On this branch:

$ npx vitest run server/config.test.ts server/control/machines-file.test.ts server/ui/docFile.test.ts server/ui/ui.test.ts server/skins/skins.test.ts server/keyRules.test.ts
 Test Files  6 passed (6)
      Tests  76 passed (76)

npm run typecheck exits 0. npm run build exits 0. npm run lint exits 0 with Checked 983 files … Found 135 warnings. and no errors; npx biome check on the 12 changed files reports nothing.

npm test has one failure, a timeout in a test this change does not touch:

 FAIL  server/control/remote.parity.test.ts > the receiver's wiring and a page's, through one script > machines, sends, refusals, jobs, a restart, a revoked key, a disconnect and another account: row for row
TestTimeoutError: Test timed out in 15000ms.
 Test Files  1 failed | 247 passed (248)
      Tests  1 failed | 3374 passed | 2 skipped (3377)

A full npx vitest run on unmodified origin/main (6165bed) fails the same test the same way (1 failed | 247 passed (248)). Run on its own, it passes on both main and this branch (11 passed (11)).

Checklist

  • Every commit is signed off (git commit -s): the DCO is the only paperwork.
  • npm run typecheck && npm run lint && npm test pass; npm run test:e2e for a UI change. (Not a UI change. npm test: one timeout that also fails on main, above.)
  • New source files start with // SPDX-License-Identifier: Apache-2.0. (No new files.)
  • No secret, real address, hostname or home path in code, fixtures or screenshots (screenshots come from --demo).
  • A change to shared/* names the wire-contract change in the description. (No change to shared/.)
  • A line under Unreleased in CHANGELOG.md.

…d as UTF-16 is read, not refused as invalid JSON

Windows PowerShell 5.1 writes UTF-16LE with a byte-order mark for
`ConvertTo-Json | Out-File config.json` and for `>`. readConfigFile read
the file as UTF-8, so such a config.json stopped every command with
"is not valid JSON", though Notepad shows valid JSON.

config.ts gains readText: a file that starts with a UTF-16LE or UTF-16BE
byte-order mark is decoded from UTF-16; any other is read as UTF-8 past a
UTF-8 mark, as withoutBom did. A UTF-16 file without a mark is not
guessed at. Every reader that went through withoutBom now goes through
readText: config.json (readConfigFile, and the receiver's live re-read in
app.ts), machines.json and the queue files (control/state.ts),
labels/marks/limits.json (docFile), color themes and view, skin and
plugin manifests. The next write of config.json is UTF-8 without a mark,
as with a UTF-8 mark.

Signed-off-by: Connor Burgess <connorburgesscodes@gmail.com>
@connorburgess

Copy link
Copy Markdown
Contributor Author

#280 also changes server/control/state.ts: it touches only expireQueue, while this pull request changes writeAtomic and the machines-loading code near the top of the file. git merge-tree of the two heads finishes without conflicts.

Signed-off-by: Connor Burgess <connorburgesscodes@gmail.com>
Signed-off-by: Connor Burgess <connorburgesscodes@gmail.com>
@connorburgess

Copy link
Copy Markdown
Contributor Author

The BOM-marked UTF-16 JSON reading fix still applies to SessionPipe GUI v0.8.18. Merged current main (d16a2c01) into this branch; the refreshed head is ef1bf38d. The affected files have not changed upstream, and the original change is preserved.

Added the previously deferred Unreleased changelog entry.

Validation on this head, macOS / Node 22.23.2:

  • npm run typecheck, npm run lint, and npm run build passed. Lint has the same 135 warnings and one informational diagnostic as current main, with no errors.
  • npx vitest run --maxWorkers=2 server/config.test.ts server/control/machines-file.test.ts server/keyRules.test.ts server/skins/skins.test.ts server/ui/docFile.test.ts server/ui/ui.test.ts: 76 tests passed.

GitHub CI run 47453782790 for head ef1bf38d passed: typecheck · lint · unit · build, all three desktop e2e shards, e2e (phone · duo), e2e (control channel), and the aggregate typecheck · lint · unit · build · e2e gate.

Also checked the combined source/test changes from all 19 refreshed PRs on the same main: no source conflicts; typecheck, lint and build passed; all 252 unit-test files passed (3,439 tests, two skipped). This was a local integration check, separate from the individual PR run above. Release-note entries may still need combining as PRs land.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant