1 Safety
liquibyte edited this page 2026-11-19 16:23:31 -06:00

Safety

What a program prints in a terminal can come from anyone: a file you cat, a page you fetch, a machine you reach with ssh. This page explains how Hanshi keeps such output from harming you: the jail in which every picture file and every sixel or compressed picture is taken apart, what a program in the terminal may and may not do, how a paste is made safe, and what a program gets from Hanshi when it starts.

1. Pictures are taken apart in a jail

To show a picture, a program has to read and take apart every byte of it. The code that decodes PNG, JPEG, GIF and BMP files and sixel data is large and complicated, and like all such code it can have bugs. A picture made on purpose to trigger such a bug could make the program do something it was never meant to do, with all the rights you have: read your files, type into your other windows through the X server, or send your data away.

So Hanshi does not take pictures apart itself. That work is done by a separate process, the worker, which is locked in a jail: a set of rules, enforced by the Linux kernel, that leaves the process nothing to read and no way to send anything out or change anything. If a hostile picture did take over the worker, it would find itself in an empty room. And because a worker that was taken over could send back anything, Hanshi treats every answer as possibly hostile too, and checks it before it uses it.

A program in the terminal sends output to Hanshi; Hanshi sends the bytes of each picture to the jailed worker and gets back pixels, which it checks before it shows them

The red border is the jail. The worker talks only to Hanshi, over a private connection that the jail leaves open; it has no other way out.

What goes to the worker:

  • The picture files a program sends with iTerm2's inline images (OSC 1337;File=) and with kitty's graphics protocol (PNG, f=100): PNG, JPEG, GIF and BMP.
  • Sixel pictures.
  • Data that a program compressed with zlib (kitty's o=z).
  • The background picture of Preferences > View (PNG, JPEG, GIF, BMP or XPM), since a theme or a setting can come from anyone too: Hanshi reads the file and hands the worker its bytes, and does so again each time the file changes.

What Hanshi does itself, outside the jail, because none of it is a file format to take apart:

  • It reads the sequence that carries a picture (its keys, such as width and height) and turns the base64 text of the data back into bytes.
  • It turns kitty's plain pixels (f=24 RGB and f=32 RGBA, without o=z) into the pixels it draws. It first checks that the width and height are at most 4096 and that the data holds every pixel they need; then each pixel is copied as it is.
  • It scales the pixels the worker sent back to the size the program asked for.

2. The worker and its jail

The worker is Hanshi's own program started again under the name xtk-worker: a fresh process with none of Hanshi's memory, no connection to the X server, and no open file but its connection to Hanshi (its standard input and output are /dev/null). It is started the first time a picture needs it and kept for the rest of the session, since a terminal can get many pictures a second. It ends when Hanshi ends, even when Hanshi is killed. It enters its jail before it reads its first request.

The jail is made from two features of the Linux kernel, Landlock and seccomp. They need no administrator rights and nothing to install. The jail is all or nothing: if any part of it cannot be set up, the worker does not run. There is no weaker fallback and no setting to turn it off, on purpose.

Area The worker
Files It may read no file at all, write none, and create, rename or remove nothing. Everything it works on comes as bytes from Hanshi.
Network No sockets of any kind: no internet, no local network, and no local sockets, so no way to reach the X server or D-Bus.
Other programs It cannot start a program, create a process, or send a signal to anything.
Kernel features No access to other processes' memory, no debugging (ptrace), no new namespaces, no mounting, no kernel keyring, no io_uring, no bpf, no performance counters, no loading kernel modules, no rebooting.
Privileges It can never gain rights (no_new_privs).
Size and time At most 2 GB of memory and 16 open files; it can write no file and leaves no core dump. Each request must be answered within 5 seconds.

The checks on each answer. A request or an answer is at most 256 MB. The size an answer announces is checked against what the request allows before any memory is set aside for it, and then the answer must hold exactly the pixels its width and height say. A picture may be at most 16384 pixels on a side in the worker, and the terminal takes at most 4096; compressed data may grow to at most 64 MB. A worker that crashes, takes longer than its time, or answers nonsense is killed, and the next picture starts a new one. A picture the worker could not decode is simply not shown.

Without the jail, no pictures

The jail needs Linux 5.13 or newer with Landlock enabled, on an x86-64 or ARM64 processor. Most current distributions have it. On a system where it cannot be made, Hanshi still works in every other way, but it shows no picture that would have to be taken apart: no PNG, JPEG, GIF or BMP file, no sixel picture, no compressed picture, and no background picture. Pictures that a program sends as plain pixels (kitty's RGB and RGBA formats, without compression) need no decoding: Hanshi copies them itself, outside the jail (section 1), so they are shown either way.

3. What a program in the terminal may do

Some things a program can ask of a terminal could be used against you, so the terminal refuses them unless you allow them in Preferences > Programs > Safety:

The request At first Why it is off
Read the clipboard (OSC 52) refused A program that reads the clipboard sees what you copied elsewhere, a password for example, and that includes programs on other machines over ssh. Allow it only when you trust every program you run.
Set the clipboard (OSC 52) refused A program could put a command on your clipboard for you to paste. tmux and vim use it over ssh; allow it when you want that.
Move, size, raise, lower, iconify or maximize the window, change its number of rows and columns (CSI 8 t, DECSLPP, DECSCPP, DECSNLS), and report its title, icon name, position and state (the window functions of CSI t) refused A program could hide the window or move it off the screen, and a title report sends text back as if you had typed it. xterm refuses these too.

Other things are never done, whatever the settings:

  • No file is read or written for a program. Of kitty's graphics protocol, the terminal only takes data sent in the sequence itself; it does not read a file or shared memory that a program names. An iTerm2 file that is not shown inline (a download) is ignored.
  • A link opens only when you open it: with Ctrl+click, the menu or a link letter. A file name opens only when the file exists.
  • Printing goes nowhere unless you name a print command, and then only to that command.
  • A program can still set the window's title, ring the bell, send notifications, and ask how large the text area and a character are; the title it sets is shown, never sent back unless window functions are allowed. It can switch between 80 and 132 columns only after it allowed that itself (mode 40), as on a VT terminal.

4. Pasting safely

  • Pasted text is cleaned before the program gets it: every control character but the tab and the line end is removed, so a paste cannot carry an escape sequence, and it cannot end bracketed paste early.
  • A program that asked for bracketed paste (bash, zsh, fish, vim ...) gets the paste marked as one, and does not run its lines.
  • A paste of several lines into a program without bracketed paste waits for Enter, with "Confirm a paste of several lines" on (Using the Terminal).

5. What a program gets from Hanshi

  • Its environment is Hanshi's own, with TERM=xterm-256color and COLORTERM=truecolor. With the Sun, HP, SCO, rxvt or Linux console keyboard, TERM names that terminal instead, when its terminfo entry is installed (Keyboard and Mouse). COLUMNS, LINES and TERMCAP, which describe the terminal Hanshi itself was started from, are removed. The variables of shell integration are added (Shell Integration).
  • It gets none of Hanshi's open files: every descriptor above the standard three is closed.
  • It starts in a session of its own, with the default action for every signal and no signal blocked, even when Hanshi was started from a program that ignores some.
  • Hanshi never sends a signal to a program in a terminal. When a terminal closes, the kernel sends its program the hangup signal, as for any terminal.
  • Hanshi's question before it closes a terminal names the program from the system's process table; it does not ask the program.

6. Known limitations

  • The terminal itself, the control sequences that programs print to move the cursor, set colors and so on, is read in Hanshi, not in the jail. Only the pictures that need taking apart go to the worker (section 1): moving the whole terminal into a jail would mean running its screen in another process.
  • On processors other than x86-64 and ARM64, and on other systems than Linux, the jail cannot be made, so those pictures are not shown there. This is on purpose: the jail's rules are written for those two processors.