Repository navigation
fix(server): reject ill-formed UTF-8 in inbound frames with 1007 - #390
Conversation
RFC 6455 closes a connection with 1007 on a text frame or close reason that is not valid UTF-8. The inbound check takes any lead byte from C0 to FF with the right number of continuation bytes, so it lets through overlong forms, UTF-16 surrogates, code points above U+10FFFF and the bytes F5 to FF. Seven text-frame cases and one close-reason case fail on main. A case with the first and last code point of each UTF-8 length passes on main and must keep passing. Part of #385 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
The inbound check took any lead byte from C0 to FF with the right number of continuation bytes. A text frame or close reason with an overlong form, a UTF-16 surrogate, a code point above U+10FFFF or a byte F5 to FF passed, and the plugin decoded and ran the request. RFC 6455 requires the endpoint to close such a connection with 1007. is_valid_utf8 now walks the text with utils.utf8_sequence_length, the decoder behind the outbound valid_utf8, so both directions share one definition of UTF-8. It is as fast as the old loop on 10 MB of ASCII and faster on multibyte text. Part of #385 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
…check Only server/utils.lua changed. A stricter frame check closes the connection of a client that sends ill-formed UTF-8, which reaches diffs through the same on_disconnect path as any other close. The model already lets a client disconnect at any step, and check.sh passes. Part of #385 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
|
@codex review |
|
@codex security review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
…, #388, #390 The merge keeps this branch's bulk-close cases on main's ordered bulk_closes list (#386). Main changed selection.lua, utils.lua, and visual_commands.lua, which cannot reach diff state. check.sh passes on the merged tree. Part of #363 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
The next claudecode.nvim release. Merging this PR creates the release
tag and GitHub Release, then starts Communique GitHub Release note
generation.
---
## [0.4.0] - 2026-11-09
### Added
- `:ClaudeCodeSendText {text}` command and
`require("claudecode.terminal").send_to_terminal(text, opts)` to send
arbitrary text to the open Claude terminal as if typed at the prompt
(submitting by default; `:ClaudeCodeSendText!` inserts without
submitting). Multi-line text is sent via bracketed paste. Works with the
in-editor `native`/`snacks` providers only.
([coder#197](coder#197),
[coder#272](coder#272))
- `:checkhealth claudecode` health check that verifies Neovim version,
`setup()`, the Claude CLI, terminal provider, WebSocket server, lock
file, and live connection state.
([coder#275](coder#275))
- `:ClaudeCodeCloseAllDiffs` command to close pending Claude diffs at
once. A diff accepted with `:w` closes once the file holds the accepted
text; otherwise the accepted text is put into the buffer as unsaved
changes for `:w`/`:e!` so edits are never discarded.
([coder#248](coder#248),
[coder#261](coder#261))
- `diff_opts.layout = "unified"`: a VS Code-style unified diff rendered
in a single buffer with interleaved deleted/added lines, as a compact
alternative to `"vertical"`/`"horizontal"`. Documented to require Neovim
>= 0.9.0. Customizable via the
`ClaudeCodeInlineDiffAdd`/`Delete`/`AddSign`/`DeleteSign` highlight
groups. ([coder#195](coder#195),
[coder#295](coder#295),
[coder#297](coder#297))
- `User ClaudeCodeSendComplete` autocmd, fired once per file when a send
is accepted while Claude is connected, with `data = { file_path,
start_line, end_line, context }` (lines 0-indexed). Enables post-send
logic such as focusing a Claude session running outside Neovim.
([coder#228](coder#228),
[coder#265](coder#265))
- `User ClaudeCodeDiffOpened` / `ClaudeCodeDiffClosed` autocmds carrying
diff metadata, plus `diff_opts.auto_resize_terminal` (default `true`) to
opt out of plugin-managed terminal width during diffs and
`terminal.diff_split_width_percentage` to set a distinct terminal width
while a diff is open.
([coder#270](coder#270))
- `terminal.auto_insert` option (default `true`): set to `false` to stay
in Normal mode when refocusing the Claude terminal and preserve your
scroll position instead of jumping to the prompt.
([coder#233](coder#233))
- `ClaudeCodeTreeAdd` / `<leader>as` support in snacks.nvim pickers
(`snacks_picker_list`), covering `Snacks.explorer()` and modal
`Snacks.picker.files()`/`grep()`.
([coder#192](coder#192),
[coder#269](coder#269))
- netrw file selection for `<leader>as` / `:ClaudeCodeTreeAdd`,
supporting marked files and the file under the cursor.
([coder#62](coder#62))
### Changed
- Model picker labels are now version-free evergreen aliases (`Claude
Opus (Latest)`, `Claude Sonnet (Latest)`, `Claude Haiku (Latest)`), with
new 1M-context variants (`opus[1m]`, `sonnet[1m]`) and a `Default
(account recommended)` entry.
([coder#146](coder#146),
[coder#256](coder#256))
- `:checkhealth claudecode` and the MCP `serverInfo.version` now report
the real plugin version from the `VERSION` file instead of a hard-coded
value. ([coder#437](coder#437))
### Fixed
- The Snacks provider no longer fails to launch Claude when a model
alias contains shell-glob characters (e.g. `opus[1m]`); the command is
now passed as an argv list with no shell interpretation.
([coder#256](coder#256))
- The native terminal now adopts only terminal buffers it started
(marked with `b:claudecode_native_terminal`), instead of any visible
terminal whose name contains `claude`. Upgrade note: a terminal that an
older plugin version started (for example after `:Lazy reload` following
an upgrade), or that a session file restored, carries no mark and is no
longer adopted. The next open starts a second Claude session, and the
old terminal keeps running until you close its buffer or quit Claude in
it. ([coder#349](coder#349),
[coder#352](coder#352),
[coder#364](coder#364))
- `closeAllDiffTabs` is now scoped to claudecode's own tracked diffs and
no longer wipes out an open diffview.nvim, fugitive, or native
`:diffsplit` review; `openFile`/`openDiff` also never reuse a window
that is in diff mode.
([coder#277](coder#277),
[coder#290](coder#290))
- `close_tab` now closes only the diff with that tab name and never
deletes a user buffer.
([coder#373](coder#373))
- Closing the native terminal, for example with `:ClaudeCodeClose`, now
stops the Claude job instead of leaving it running.
([coder#336](coder#336))
- The Claude terminal now adds loopback hosts to `no_proxy`/`NO_PROXY`
so a configured proxy no longer tunnels Claude's `ws://127.0.0.1` IDE
connection and times out queued @ mentions.
([coder#70](coder#70),
[coder#268](coder#268))
- `focus_after_send = true` with `provider = "none"`/`"external"` now
emits a one-time warning pointing to the `User ClaudeCodeSendComplete`
autocmd instead of failing silently.
([coder#228](coder#228),
[coder#265](coder#265))
- Rejecting a Claude diff with `:q` / `:close` / `<C-w>c` (or closing
the tab) now resolves it as rejected via a `WinClosed` autocmd.
([coder#266](coder#266))
- Quickly-made and single-line visual selections are now pushed to
Claude reliably; selections are flushed synchronously on visual-mode
exit and persist until the cursor moves, including in the external
`/ide` flow and Select mode.
([coder#246](coder#246),
[coder#267](coder#267),
[coder#367](coder#367),
[coder#381](coder#381),
[coder#388](coder#388))
- Pending diffs are now automatically closed when the client that opened
them disconnects or the integration stops, so orphaned proposals no
longer linger.
([coder#248](coder#248),
[coder#261](coder#261))
- `openDiff` now works when the Claude terminal is the only window,
creating a split to host the diff instead of failing with "No suitable
editor window found".
([coder#260](coder#260))
- Fixed the "climbing cursor" in the Snacks terminal, where the cursor
sat one row above Claude's prompt after hide and show. On Neovim >= 0.10
a floating terminal is now hidden instead of closed. A split is still
closed on hide and created again on show, which keeps the cursor in
place. Floating terminals on Neovim < 0.10 keep the old behavior.
([coder#240](coder#240),
[coder#183](coder#183),
[coder#271](coder#271))
- Worked around a Neovim core bug (< 0.12.2) that fragmented large
bracketed pastes into the terminal; controlled by
`terminal.fix_streamed_paste` (`"auto"` by default).
([coder#252](coder#252))
- `openFile` now selects exactly the requested line range or matched
text, keeps the user's Visual mode, unsaved changes, focus and preview
window intact, never prompts W11/W12, and never picks the preview window
as the main editor window.
([coder#353](coder#353),
[coder#365](coder#365),
[coder#370](coder#370),
[coder#375](coder#375),
[coder#382](coder#382),
[coder#384](coder#384),
[coder#387](coder#387),
[coder#396](coder#396),
[coder#408](coder#408))
- `getDiagnostics` now handles plain file paths without a `file://` URI
scheme, and IDE diagnostics return grouped URI-based payloads with
editor-native ranges and severity names.
([coder#163](coder#163),
[coder#274](coder#274))
- `:ClaudeCodeAdd` and `openFile` now handle `$` in file paths.
([coder#286](coder#286))
- Prevented a segfault when accepting a new-file diff with
`render-markdown.nvim` installed, by turning off diff mode before the
post-write redraw.
([coder#224](coder#224))
- The auth token is now generated from a CSPRNG and the lock file is
written atomically with mode `0600` in a `0700` directory.
([coder#259](coder#259))
- Detect system sleep/wake and reset client keepalive timestamps to
prevent false disconnections.
([coder#141](coder#141))
- The WebSocket server now retries port binding on `EADDRINUSE`
(parallel instances), closes connections on malformed/oversized or
binary frames with the correct status, joins fragmented messages,
validates UTF-8 per message, and never repeats a client id within a
process. ([coder#258](coder#258),
[coder#283](coder#283),
[coder#378](coder#378),
[coder#383](coder#383),
[coder#390](coder#390),
[coder#395](coder#395),
[coder#405](coder#405),
[coder#422](coder#422),
[coder#431](coder#431))
- Legacy `diff_opts` (e.g. `vertical_split`, `open_in_current_tab`) now
take effect again after the config merge-order regression.
([coder#142](coder#142))
- `:q` on a diff window no longer hangs; floating diff terminals keep
focus; empty buffers are wiped when the terminal provider is `none`; and
accepted diffs are handed over in the buffer's own line-ending format.
([coder#178](coder#178),
[coder#223](coder#223),
[coder#266](coder#266),
[coder#392](coder#392))
### Compatibility
- Integration tests now run on Neovim v0.8.0, and a new-tab diff no
longer fails there because Neovim 0.8 has no `statuscolumn`. The unified
layout is documented to require Neovim >= 0.9.0, and the v0.8.0
integration tests skip it.
([coder#369](coder#369),
[coder#394](coder#394))
---
Maintained by the release-please workflow. CHANGELOG.md and the release
PR body are regenerated by Communique on every push to main, so manual
edits to this PR are overwritten.
---------
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Thomas Kosiewski <tk@coder.com>
…e binary (coder#395) ## Why The operator goal is to clear the open follow-up issues in this repository (coder#393 now) with small reviewed PRs. This PR takes coder#393 item 6. RFC 6455 (section 5.4) says that servers "MUST support receiving both fragmented and unfragmented messages". A fragmented text message is a TEXT frame with FIN clear, then CONTINUATION frames up to one with FIN set, and control frames can come between them. Section 5.6 says that one frame may hold a partial UTF-8 sequence, and that only the whole message must be valid UTF-8. The plugin checked UTF-8 per frame and did not join fragments: 1. A valid message whose fragments split a code point got Close 1007. 2. Any other fragmented message had its first fragment dispatched alone as a message, and the next frame closed the connection with 1003. 3. An ill-formed continuation frame got 1003, not 1007, because no continuation frame was ever checked. 4. A binary frame skipped the UTF-8 check, and the plugin ran it as JSON-RPC. With this PR, the plugin joins the fragments and checks UTF-8 once, on the whole text. It refuses binary messages with 1003, as the RFC's own example for that status suggests. ## What changed - `lua/claudecode/server/client.lua`: a new `add_data_frame` keeps the fragments of the open text message on the client (`client.fragments`). It returns the whole text once the last fragment arrives, and `process_data` then calls `on_message` once. Pings and pongs between fragments are answered as before. The status codes: | Input | Close status | | ------------------------------------------------ | ---------------------------------------- | | Joined text that is not well-formed UTF-8 | 1007 | | A binary message | 1003 "Unsupported data". Nothing runs it | | A CONTINUATION frame with no message to continue | 1002 | | A TEXT frame inside a fragmented message | 1002 | | Fragments that add up to more than 100 MiB | 1009 | The `on_error` texts are short. A longer one wrapped in a 160-column terminal and left Neovim at "Press ENTER", which blocked the next scheduled message in my first run. - `lua/claudecode/server/frame.lua`: `parse_frame` no longer checks the UTF-8 of a text frame. It still checks the close reason, because control frames are never fragmented. The 100 MiB limit is now `frame.MAX_PAYLOAD_LENGTH`, and the message check uses it too. - `lua/claudecode/server/README.md`: the fragmentation invariant (it said "Fragmentation is not supported"), the `parse_frame` invariant and the new constant. - `tests/unit/server/message_spec.lua` (new) drives `process_data`. The `parse_frame` text-frame UTF-8 cases from coder#390 move there as message cases. `frame_spec.lua` keeps the close-reason cases, and a new case checks that `parse_frame` returns a text frame that ends inside a code point. - `.agents/skills/verify-diff-lifecycle/covered.lock`: re-recorded. Only the file hashes of `client.lua` and `frame.lua` changed, and no covered block or call site changed. A joined message reaches the diffs as one `tools/call`, and a refused frame closes the connection through the usual `on_disconnect` path. The model already has both. ## Scope coder#393 item 6. Outbound frames do not change: the server still sends each message as one text frame. ## Blast Radius - **Clients that fragment text messages.** Valid messages now work. Before, they were closed or half-dispatched. - **Clients that send JSON-RPC in binary frames.** They now get Close 1003, where the plugin used to run the call. Claude Code 2.1.312 sends text frames: its WebSocket transport calls `ws.send` with a JSON string. The MCP TypeScript SDK sends text frames too. I did not find a client that sends binary. - **Memory.** A fragmented message is buffered until its last fragment arrives, up to the same 100 MiB that one frame could already hold. The pieces are joined as they arrive, the way a binary counter carries, so n fragments keep O(log n) strings and an empty fragment costs nothing. ## Verification | Run | Code | Result | | --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `busted tests/unit/server/` (red) | ce4c7ae, the tests on main | 9 failures and 1 error. The fragment cases get 1007 or a half message, binary runs, a stray continuation gets 1003, and there is no 1009 | | Same (green) | this PR | 69 successes. `mise run test` on 41964a6: 974 successes, 0 failures | | 100000 empty and 100000 one-byte continuation frames, then the last one (Codex round 1 finding) | f830afe / this PR | f830afe held 200001 table entries and grew by 2063 KiB. This PR holds 7 strings and grows by 126 KiB. The unit case (40000 fragments, limit 256 KiB) fails at 519 KiB on b1f01b2 | | Raw WebSocket client in real Neovim 0.12.3 (`verify-claudecode-nvim` harness), eight cases | main (c05a629) and this PR (3f15865, again on 0c11e64) | see the table below | | Real Claude CLI 2.1.312: `claude mcp add-json -s user nvim '{"type":"ws",…}'`, then `claude mcp get nvim` | this PR | `Status: ✔ Connected`, so the CLI's `initialize` went through. The CLI then closed with 1000. This makes no model request, and `ANTHROPIC_API_KEY` was unset | | `journey-opendiff-accept.sh` | this PR | `PASS opendiff-accept` | | `verify-diff-lifecycle/check.sh` | this PR (41964a6, after merging main up to coder#396) | `PASS`. `main: pass (89205 distinct states found)`, `live: pass (5269 distinct states found)` | Results of the raw-client run: | Frames sent | main | this PR | | ----------------------------------------------------- | ---------------------------------------------- | -------------------------- | | A `tools/call` in two fragments, split inside 😀 | Close 1007 | answer for `/x😀` | | Same, split at an ASCII byte | Close 1003 | answer | | Split inside 😀, with a ping between the fragments | Close 1007 | Pong `hi`, then the answer | | `ED A0 80` (a surrogate) in the continuation frame | Close 1003 | Close 1007 | | A binary frame with a `tools/call` holding `ED A0 80` | the plugin ran it: `File must be open … /x���` | Close 1003 | | A continuation frame with no message started | Close 1003 | Close 1002 | | A text frame while a fragmented message is open | Close 1007 | Close 1002 | | One unfragmented text frame | answer | answer | After both runs, the plugin still had its launch client, and Neovim was in Normal mode. Part of coder#393 --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
Why
The operator goal is to clear the open follow-up issues in this repository (#385 now) with small reviewed PRs. This PR takes the inbound part of #385 item 6. Items 7 and 8 are in #386.
RFC 6455 requires an endpoint to close a connection with 1007 when a text frame or a close reason is not valid UTF-8. The plugin's inbound check,
server/utils.luais_valid_utf8, took any lead byte from C0 to FF with the right number of continuation bytes. So it let through overlong forms, UTF-16 surrogates, code points above U+10FFFF, and the bytes F5 to FF. The plugin then decoded the request and ran it. The outbound side already has a strict decoder,claudecode.utils.utf8_sequence_length(behindvalid_utf8, since #378). This PR makes the inbound check use that decoder.The outbound part of item 6 does not change: a buffer name with invalid bytes still reaches Claude as U+FFFD, as
lua/claudecode/server/README.mddocuments.What changed
lua/claudecode/server/utils.lua:is_valid_utf8walks the text withutils.utf8_sequence_lengthand returnsfalseat the first ill-formed sequence.frame.parse_frame(text frames and close reasons) andframe.validate_framecall it as before.tests/unit/server/frame_spec.lua: seven text-frame cases and one close-reason case that must get 1007, and one frame with the first and last code point of each UTF-8 length that must still parse.lua/claudecode/server/README.md: the 1007 invariant names what counts as ill-formed..agents/skills/verify-diff-lifecycle/covered.lock: re-recorded. Only theserver/utils.luafile hash changed, with no covered block or call site. A rejected frame closes the connection through the sameon_disconnectpath as any other close, and the model already lets a client disconnect at any step.Scope
#385 item 6, inbound only. No change to what the plugin sends.
Blast Radius
A client that sends ill-formed UTF-8 now gets Close 1007, where the plugin used to run its request. Claude Code cannot send such bytes: it serializes each message with
JSON.stringify, which escapes a lone surrogate as\uD800, and its WebSocket sends a JS string as UTF-8. The real CLI run below shows that its non-ASCII text still goes through.Verification
busted tests/unit/server/frame_spec.lua(red)mise run test: 908 successes, 0 failuresverify-claudecode-nvimharness)ED A0 80,C0 AF,F4 90 80 80andF5 80 80 80each get agetDiagnosticsanswer. The plugin ran the call with the ill-formed pathClose frame, code 1007. A frame withF0 9F 98 80(😀) still gets its answer, and the plugin keeps its other clientclaude -p --model haikuwith the plugin as an MCP server over the CLI's own WebSocket transport, asked to callgetDiagnosticsforfile://…/naïve-日本-😀.txtgetDiagnostics arguments: {uri=50 bytes}and finds the open buffer. Claude repliesünïcode ✓ diagnostic, the diagnostic set on that buffer. The CLI closes with 1000uri=50 bytes, replyünïcode ✓ diagnostic, close 1000is_valid_utf8in LuaJIT 2.1, best of 3verify-diff-lifecycle/check.shPASS.main: pass (89205 distinct states found),live: pass (5269 distinct states found)How the real CLI run works
claude -p --idedoes not connect to an IDE in print mode. So the script hands the CLI an--mcp-configfile with an MCP server of typewsat the plugin's port. The lock file's token is in thex-claude-code-ide-authorizationheader. The CLI's IDE transport (ws-ide) and this one build the sameWebSocket(url, { protocols: ["mcp"], headers })and the same transport class, so the bytes on the wire come from the same code. The run uses its ownHOMEandCLAUDE_CONFIG_DIRunder the harness state dir, andANTHROPIC_API_KEYfor one haiku request. The 50-byteuriis the UTF-8 length offile:///tmp/vcn.<id>/work/naïve-日本-😀.txt.Part of #385
Generated with
xum• Model:anthropic:claude-opus-5-5• Thinking:xhigh