Skip to content

dialogs: Check file content before opening files in the editor - #1525

Draft
lacutah wants to merge 5 commits into
cockpit-project:mainfrom
lacutah:editor-content-sanity
Draft

lacutah wants to merge 5 commits into
cockpit-project:mainfrom
lacutah:editor-content-sanity

Conversation

@lacutah

@lacutah lacutah commented Oct 22, 2026

Copy link
Copy Markdown

Builds upon #1524 — the content check promised in
#901 (comment). Draft until that one lands;
the file list here also shows its changes, this PR is the single commit on top.

"Open text file" decides by extension and size alone, so a binary whose name gives nothing away opens straight
into the editor, where saving it back can damage the file. This weighs the bytes instead: before the editor opens
anything, its first 8 KiB is read through fsread1 and classified in plain JS by the rule git uses for buffers (a
NUL byte or undecodable UTF-8 = binary — a multi-byte character split by the cut does not count as undecodable). A
file that looks binary gets a warning first — "This doesn't appear to be a text file", with Open anyway,
Download and Cancel.

The check sits in edit_file(), the single choke point, so it applies to every route in: the context menu warns,
and a double-click with "Prefer editor" warns too rather than opening a binary silently. Nothing reads the content
twice and no route skips it; the listing-based check still decides what is offered (editor_supports() is
untouched), the bytes decide what is safe to show. Cost is one small read per editor open; unreadable files count
as text and let the editor report the error itself.

Tests: new TestFiles.testEditorNonTextWarning covers Cancel, Open anyway, and Download, plus that a text file
opens with no dialog; #1524's testDefaultFileAction now expects the warning on its binary fixtures. 3/3 pass on
fedora-44 / chromium in the nested-VM harness.

I wrote this with an AI coding assistant (opencode + Qwen); I reviewed, tested, and carry every line myself.


Files: Warns before editing non-text files

Opening a file that turns out to be binary now says so: the editor checks the first chunk of bytes and offers
"Open anyway", "Download" or Cancel instead of showing garbled content that could be saved back over.

"Open text file" decides by extension and size alone, so a binary whose
name gives nothing away opens straight into the editor, where saving it
back can damage the file.

Read the first chunk of whatever the editor is asked to show, and when it
looks binary (a NUL byte or undecodable UTF-8, the heuristic git uses)
warn before opening, with Download offered right there. Every route into
the editor goes through this, so a double-click with "Prefer editor" set
warns as well: the listing decides what is offered, the bytes decide what
is safe to display.

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