Skip to content

fix(core): refuse to rewrite files that are not valid UTF-8 - #64868

Merged
neriousy merged 1 commit into
devfrom
non-utf8-files
Nov 19, 2026
Merged

neriousy merged 1 commit into
devfrom
non-utf8-files

Conversation

@neriousy

Copy link
Copy Markdown
Member

Issue for this PR

Closes #45924

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

edit and patch read files through FileMutation.readText, which decodes leniently, so every non-UTF-8 byte (Latin-1, cp1252, GBK...) became U+FFFD and the whole file got written back like that. The tool still reported success.

  • new FileMutation.readEditableText checks the bytes with isUtf8 and fails with FileMutation.InvalidUtf8Error instead
  • edit and patch updates read through it, so the file is left untouched and the model gets told to use a byte-preserving tool or convert the file
  • patch verification fails before any write, so a multi-file patch applies nothing
  • patch deletes, post-write readbacks and write stay lenient since they don't rewrite bytes they decoded

read is still lenient, didn't touch that here.

How did you verify your code works?

  • reproduced on dev with real models on a 406-line Latin-1 Invoice.java (edit on line 405, accents on line 1): claude-sonnet-5-5 via edit and gpt-6.2-sol via patch both replaced the header bytes with ef bf bd
  • with the fix both tools refuse, the models fall back to byte-level sed / python and git diff shows only the edited line
  • new tests in tool-edit.test.ts and tool-patch.test.ts fail on dev and pass here (--rerun-each 10)
  • edit, patch, write and file-mutation suites pass, bun typecheck in packages/core is clean

Screenshots / recordings

n/a

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks good to me. I ran the #45924 reproduction from source with a simulated model on a file containing GBK bytes. On the base, both edit and patch (the latter called through execute) reported success and replaced d6 d0 ce c4 with ef bf bd ×4. With this PR, both tools fail with the new "not valid UTF-8" message and od shows the file byte-for-byte unchanged. The new edit and patch tests fail on the base and pass here. The edit, patch, write and file-mutation suites pass, and tsc -b in packages/core is clean.

Public API is unchanged: readEditableText and InvalidUtf8Error are internal to core.

No blocking concerns.

@neriousy
neriousy merged commit b4d81f0 into dev Nov 19, 2026
11 checks passed
@neriousy
neriousy deleted the non-utf8-files branch November 19, 2026 19:51
@amrkmn amrkmn mentioned this pull request Nov 23, 2026
1 task
agentsofdtd Bot pushed a commit to DefendTheDisabled/acf-opencode that referenced this pull request Dec 1, 2026
Ports upstream b4d81f0 (anomalyco#64868, v2.4.0) to the v2.2.5 base; retires
at the next base move.

edit and patch decoded a file with a non-fatal TextDecoder, so every
byte that is not UTF-8 (Latin-1, cp1252, a UTF-16 BOM) came back as
U+FFFD when the file was written. FileMutation.readEditableText now
refuses such a file with the typed FileMutation.InvalidUtf8Error, and
both tools fail with that message and leave the file unchanged. patch
prepares every update before it writes any, so a refused file leaves
the whole patch unapplied. write replaces the whole file and a patch
Delete never rewrites bytes, so both keep FileMutation.readText.

Files: packages/core/src/file-mutation.ts (InvalidUtf8Error,
readEditableText), packages/core/src/tool/plugin/edit.ts,
packages/core/src/tool/plugin/patch.ts; tests in
packages/core/test/tool-edit.test.ts and tool-patch.test.ts.

Adapted from upstream: both tools read through environment.files here
(upstream's later 47fcf73 reads through target.environment.files),
so the two call sites carry an "ACF: upstream-early anomalyco#64868" marker
where the next merge will conflict.
agentsofdtd Bot pushed a commit to DefendTheDisabled/acf-opencode that referenced this pull request Dec 1, 2026
ACF change, not an upstream port; carry it at the next base move.

After a formatter runs, edit, patch and write sync the file's leading
byte order mark with FileMutation.syncTextBom, which used
Bom.syncBytes. That decoded the whole file and, when the BOM had to be
added or removed, wrote it back re-encoded, so every byte of a file
that is not valid UTF-8 (a formatter's output, say) became U+FFFD.
syncBytes now adds or removes only the leading EF BB BF bytes and keeps
every other byte as it was; whether it writes, and the text it returns,
are unchanged. Bom.syncFile uses it too.

This completes the protection of the UTF-8 refusal (upstream anomalyco#64868,
ported earlier on this branch), which refuses to rewrite such a file
but did not cover the formatter's output. It is not a binary detector
and makes no promise about what an external formatter writes.

Files: packages/util/src/bom.ts; tests in packages/util/src/bom.test.ts
(a non-UTF-8 body is kept byte for byte when a BOM is added, removed or
collapsed).
agentsofdtd Bot pushed a commit to DefendTheDisabled/acf-opencode that referenced this pull request Dec 1, 2026
ACF-CHANGES.md gets one row per fix ported on this branch: UTF-8
refusal (anomalyco#64868, with ACF's byte order mark change), Code Mode fetch
redirects (anomalyco#60502), URL credentials and rule matching (anomalyco#61701, anomalyco#62373),
site-scoped "Allow always" (anomalyco#66000, with the image_generate redirect
limit), service password out of the CLI log (anomalyco#64911), the session_move
ask and its scope (anomalyco#62204), and file reads as downloads with cross-site
API refusal (anomalyco#62422).
agentsofdtd Bot pushed a commit to DefendTheDisabled/acf-opencode that referenced this pull request Dec 1, 2026
… the base

Ports ahead of the base, each with ACF's own fixes from three review rounds: refuse non-UTF-8 rewrites (anomalyco#64868); check Code Mode fetch redirects (anomalyco#60502); reject URL credentials and match rules as written (anomalyco#61701, anomalyco#62373); site-scoped Always for web fetch (anomalyco#66000); keep the service password out of the CLI log (anomalyco#64911); session_move asks external_directory (anomalyco#62204) on the real destination, re-checked before the move, with worktrees exempt only when the project's own Git lists them (read exactly, -z); file reads as downloads and cross-site API refusal (anomalyco#62422). Plus ACF's byte-only BOM sync and the template Ask test. Reviews: 2026-11-29T18-08, 19-01, 19-28 and 20-02 relays (Associate GPT-6.2 Sol). Gates on Bun 1.4.4 at 0c082b0231 (one ledger sentence changed since): core 1398/3 skip/2 fail (project-icon symlink EPERM, same on base), typecheck:tsc 0 in core/cli/server/util, root check only the known custom-elements.d.ts symlink baseline. Private scan: 8 'email' matches, all synthetic URL userinfo examples ([email protected], [email protected]); gitleaks clean.
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.

edit and apply_patch silently corrupt non-UTF-8 files (U+FFFD replacement)

1 participant