Skip to content

Keep a page from taking over a person's terminal, and refuse binary there - #62

Merged
asavor merged 42 commits into
mainfrom
terminal-safe
Nov 20, 2026
Merged

asavor merged 42 commits into
mainfrom
terminal-safe

Conversation

@asavor

@asavor asavor commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Why

A page that string prints comes from whatever site the person asked for, so it's untrusted. On a terminal, an escape sequence in that text can recolour the screen, move the cursor, rewrite the window title or worse. Bidi overrides can make a line read differently from its bytes, so a command shown on screen may not be the command that runs. And --format raw of a PDF or an image poured binary into the terminal.

Summary

Stacked on usestring/cli "Refuse a JSON-only field with another format, and read --body-file - from stdin" (#61). Merge #42 to #61 in order first, then this.

  • When stdout is a person's terminal (a TTY, and no coding agent), string strips from the output:

    • ESC, so no escape sequence runs. What followed it shows as plain text, as [31m;
    • every other control character except tab and newline, C0, DEL and C1 included;
    • bidi overrides and isolates, U+202A to U+202E and U+2066 to U+2069.

    That holds for a page from fetch, for each part with --max-tokens, and for the JSON of search, request and the rest, which stays valid JSON.

  • Bytes that aren't text, judged by the content type or by a NUL in the first 8 KB without one, are refused there. That's a PDF or an image from --format raw. The error is binary content (application/pdf, 1.2 MB): use -o FILE, with the command and -o page.bin as the next step, exit 2. Raw text, such as HTML, is shown, cleaned.

  • Piped, saved with -o, or under a coding agent, output goes out byte for byte, as before.

  • The help, string help formatting and the README say so.

Risks

  • A person who printed raw binary to a terminal on purpose now gets exit 2, and can pipe it or use -o instead.
  • On a terminal, characters a page meant to show, such as a right-to-left override in a mixed-script page, are dropped. Piped output keeps them.

Tradeoffs

  • ESC is dropped, not the whole sequence after it, so a person sees that something was there. The spec asks for exactly that.
  • The optional --wrap, which surrounds a page with a random boundary for agents, isn't part of this change.
  • A refused binary has already been fetched, since its content type is known only from the answer.

Test plan

  • go vet ./... and go test ./... pass on Go 1.27.4 and Go 1.25.0; go vet also passes cross-compiled for Windows, macOS and FreeBSD.
  • TestAPageCantTakeOverAPersonsTerminal checks the exact output for a person of a page with escape sequences, \r, BEL, a C1 character and bidi controls. It also checks:
    • the page byte for byte in a pipe and to an agent at a terminal;
    • search's JSON cleaned the same way, and a part from --max-tokens too;
    • a PDF from --format raw refused for a person, with the exact error and next step, and written as it came in a pipe;
    • raw HTML shown cleaned;
    • every range forTerminal drops.
  • Each mutation fails the new test: no cleaning, bidi kept, C1 kept, binary shown, agents' output cleaned, and parts not cleaned.
  • CI on Linux, macOS and Windows.

Generated with Claude Code

asavor and others added 30 commits November 20, 2026 00:43
main now builds one execCtx before the command runs: which of stdin,
stdout and stderr are terminals, whether a coding agent runs string, and
whether it is a CI job. The two terminal flags the runtime carried become
its stdinTTY and stderrTTY, with no change in behavior.

The agent is named from the variables only an agent's command runner
sets, in a fixed order: STRING_AGENT, which can also force the person's
view; AI_AGENT, cut to its name; the runners' own variables; CLAUDECODE
or CLAUDE_CODE_ENTRYPOINT only when stdin is not a terminal, since IDE
terminals set them for people too; then AGENT. TERM, TERM_PROGRAM and
the process tree are never read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An update notice used to show whenever stderr was a terminal. Coding
agents can run string in a pseudo-terminal, so the notice landed in
what the agent read. Now it shows only when stdout and stderr are both
terminals, no coding agent runs string, and it isn't a CI job. A copy
that only gets notices is checked for a release under the same rule.

A copy the install script installed still updates itself in the
background as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The help says that string finds a coding agent from the variables agents
set and then shows no update notices, and how STRING_AGENT names the
agent or a person. The README gets a Coding agents section with the
variables string reads and what changes under an agent, and the Updating
section says notices are for a person at a terminal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Requests to String's API, portal and sign-in service now carry a
User-Agent of string-cli/<version> (<os>/<arch>), with "; agent=<name>"
when a coding agent runs string, in place of Go's default. They also
carry X-String-CLI-Mode (interactive, agent, non-interactive or ci),
X-String-Agent with the agent's name, and X-String-Agent-Raw with
AI_AGENT as the agent set it. String can then tell people, scripts, CI
and each agent apart in its own request logs. string sends nothing
else, and no events of its own.

DO_NOT_TRACK=1 sends only string-cli/<version> and none of the
X-String headers.

The download host keeps its own User-Agent, as the README promises.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The help now says that requests to String carry string's version, the
system and the agent's name, and that DO_NOT_TRACK=1 sends only the
version. The README's Coding agents section lists each header, what it
carries and what DO_NOT_TRACK leaves out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Update checks and downloads now carry userAgent(): string-cli/<version>
(<os>/<arch>), with the agent's name under a coding agent, and only
string-cli/<version> under DO_NOT_TRACK, so the two can't drift apart.
They still send none of the X-String headers, which the download host
doesn't read. The README's Updating section says what the host gets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
login and init started the system's browser opener on every sign-in:
under a coding agent, in CI, with stdout piped and with --json. Now they
open the link only when execCtx says a person is at a terminal (stdin and
stdout terminals, no agent, no CI), and --no-open, anywhere before --,
stops it there too. What they print is unchanged.

One function now takes --json and --no-open out of the arguments, and
afterCommand uses it too, so a switch before the command name doesn't
hide the command from it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
login, init and trial claim waited for an approval whenever stdin was a
terminal, so a coding agent running string in a pseudo-terminal, or a CI
job with one, blocked until the code expired. They now wait only when
execCtx says a person is at the terminal: stdin and stdout terminals, no
agent, no CI and no --no-input. Anywhere else they print the link and
code and return, and only --poll waits.

--no-input, or STRING_NO_INPUT=1, says nobody will answer, for scripts
and anyone else. -y and --yes are taken by every command, to confirm
what it would ask first; none asks yet. trial claim without --email now
names the flag, with exit 2 and the code missing_input.

The sign-in test fixture builds its execCtx from its variables, as main
does, so a test sets CI, an agent or STRING_NO_INPUT there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
afterCommand finds the command after the global switches, as run()
does, so string --no-open update and string -y update start no update
check. The test now covers both, so the two can't drift apart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A request that never answered held string for five minutes, and an
answer that stalled halfway held it for good. Under a coding agent that
runs past the two minutes some agents allow a command, and the agent
stops string without an error to read.

Each request to String now has a time limit: 120s, 300s for an
integration's action, and 90s for any request under an agent.
--timeout DURATION sets another, as 90s, 5m or seconds, or 0 for none,
anywhere before --. An answer that sends nothing for 60s ends too.
Either exits 7 with code timeout, and the error offers twice the limit,
to run in the background where an agent's shell stops commands at two
minutes.

The client no longer stops at five minutes without response headers;
the request's own limit does that now. Downloads keep their deadlines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sitemap --wait polled until the crawl ended: in silence unless stderr was
a terminal, and forever on a status it didn't know. Under a coding agent
a crawl outlasts the two minutes some agents give a command, so the agent
stopped string with no word of the job or how to pick it up.

--wait now first prints the job and the command that resumes the wait.
A person who sees stderr at a terminal still gets one progress line
redrawn in place; anywhere else, an agent's terminal included, a plain
line comes every 15s. Under an agent the wait stops after 90s, and
--timeout sets another limit for anyone. The limit counts from the
command's first request, and each poll gets only what is left of it.

A wait that reaches its limit, or meets a status string doesn't know,
exits 7 with code wait_timeout, prints the last status on stdout and
names the resume command. The crawl keeps running.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A request that ran past its time limit now fails with exit 7 rather than
as a bare network error, and string login --poll took any failure but a
busy server as the end of the code: it deleted the waiting sign-in. A
server that didn't answer in time hasn't spent the code either, so the
sign-in now stays saved, and the error says that string login --poll can
still finish it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
login --poll and trial claim --poll waited in silence for as long as
their code lived, longer than the two minutes some coding agents give a
command. They now use the waiter that sitemap --wait uses: under an
agent they stop after 90s, and --timeout sets another limit for anyone.
A wait that reaches its limit exits 7 with code wait_timeout, and the
sign-in or claim stays saved for the next --poll. Where no person
watches stderr at a terminal, the wait first says what it waits for and
how to resume, then prints a line every 15s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ctrl-C ended string with exit 130 and said nothing. The first Ctrl-C
now says what keeps running and the command that picks it up: a crawl
that --wait was watching, or a sign-in or claim that is still open. For
a request that changes something, it says the request may already have
been processed. On a person's terminal it clears the line first, and
under --json it prints the error envelope. Then string ends the way
SIGINT ends a program, so the shell sees 128+2 and a script that runs
string stops too; Windows still exits 130. A second Ctrl-C ends string
at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Eight of the thirteen commands printed the help for all of string when
asked for theirs. Every command now has its own page: usage, examples,
what it prints and how it exits, what it costs, its flags, and where to
read more. --help, -h anywhere before --, and string help COMMAND all
print it without a request. -v prints the version, as --version does,
and string version adds the commit, its date, the system and the Go
version, as one object with --json. None of these starts the daily
update check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A mistyped command said only "Unknown command". Now a near miss of a
command, or a word other tools use for one (scrape, crawl, setup, auth,
credits, doctor), names the command meant, with the same command line
as the next step, and string exits 2 without running it. Every flag of
more than one word takes its camelCase, kebab-case and snake_case
spellings, the CLI's own flags and the global switches included, and a
near miss of any flag names the one meant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
string example.com said "Unknown command", and string fetch example.com
said "Invalid URL". A first argument with a dot or :// that is no
command is now fetched, and a URL with no scheme given to fetch,
request or sitemap submit gets https://, with a note on stderr that
names the URL string uses. A scheme in capitals works anywhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…agents

string help exit-codes, environment, formatting and agents cover what
no one command owns: what each exit code means and what to do next,
every variable string reads and what each request tells String, where
output goes and in what shape, and what changes under an agent. A near
miss of a topic names it. Under an agent, bare string, string help and
string --help start with three lines on where to find one command's
page and how to keep big pages out of its context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s out

A --json error now always carries exit_code, retryable,
user_action_required and next_commands. retryable is the API's word, or
true after a rate limit, a failure at String or a timeout.
user_action_required is true when a person must act: a key, credit, a
crawl's approval, or a blocked network. Each of next_commands has a type
(fix_command, wait, retry, login, trial, env, pay or tell_user), so an
agent can take the steps that need no person. A request that got no
answer gives its reason: dns, connect or tls, and it can be sent again.
Inside a sandbox that blocks the network, the error says so, is not
retryable, and its step is to ask the person to let string reach the
host.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An error's text was its message, which for an API error held the status
and up to 2,000 characters of the raw body, an HTML page included, and
for a request that got no answer Go's own words, such as dial tcp. Now
the first line is error: and one sentence: the API's message with its
status when the API sends one, a short plain answer, or the CLI's own
sentence. The details follow, indented, then next: after a blank line.
A request that got no answer says whether the name didn't resolve, the
connection was refused, TLS failed, or the connection was lost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
--errors json gives JSON errors, as --json does, and leaves stdout as it
is, so a script can keep fetch's Markdown and still read errors as JSON
in 0.3 and 0.4 alike. fetch --json now says once, on stderr, that its
stdout becomes a JSON object in 0.4. A fetch whose site answered 404 or
410 warns that it will exit 8 then, and another error that it will exit
6, from the X-Status-Code the API sends. STRING_NO_DEPRECATION_WARNINGS=1
silences both, and so does STRING_COMPAT=0.2. string help formatting
lists the changes, and string help environment the two variables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
asavor and others added 11 commits November 20, 2026 06:10
…n 0.4

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A fetched page over 16 KB now says its size on stderr before the page,
wherever no person reads stdout at a terminal: the bytes and tokens,
what the agent running string keeps of output that long, and the same
command with -o page.md. -o checks its path before the request, so a
bad path no longer spends the call, and says what it saved: the size,
the tokens, a fetch's status and host, and a Markdown page's outline,
each heading after the line it starts on. -o - is stdout. -q leaves
these status lines, and a wait's, out of stderr.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fetch --max-tokens N prints about N tokens of the page, at four
characters a token, and stderr says which part it is of how many and
gives the command for the next part. Parts end on whole tokens, mostly
at a line's end, so the next part starts exactly at its --offset and
the parts give the page back with nothing lost or repeated. The first
part saves a local copy for 15 minutes, readable only by its owner, and
the parts after it read that copy: no request, no credit, and no
keyless allowance spent. The size note for a large page now names
--max-tokens too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
whoami read only local files and always exited 0, so a key String had
revoked still looked fine. Now it checks the key with one request to
the balance endpoint, which spends no credit: it says the key's source,
the account it was saved for, the key's first characters and that
String accepts it; exits 3 when String refuses the key, with the step
for that source; and exits 6 when it couldn't ask. whoami --offline
keeps the old local-only answer, and --json gives one object.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…from stdin

A field that only comes back in JSON, such as --jsonSchema, with
--format markdown or raw was dropped without a word. It now exits 2 and
names the fix. request --body-file - reads the body from stdin, where it
used to look for a file called -; with --input - too, it exits 2, since
only one can read stdin. --body still sends its text as written, and
warns when it is @ and the name of a file that exists, since
--body-file was likely meant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…here

Page content is untrusted. When stdout is a person's terminal, a page
now loses ESC, so no escape sequence runs; every other control
character but tab and newline, C1 included; and bidi overrides and
isolates, which can make text read differently from its bytes. Bytes
that aren't text, such as a PDF from --format raw, are refused there
with exit 2 and the command with -o. Piped, or under a coding agent,
output goes out byte for byte, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@asavor asavor added claude PR primarily authored by Claude merge:same-repo-dependency Needs an unmerged PR in this repo first; no known cross-repo prerequisite. Checks and holds apply. labels Nov 20, 2026
@asavor
asavor changed the base branch from input-fixes to main November 20, 2026 16:40
@asavor
asavor merged commit ad9af5c into main Nov 20, 2026
4 of 5 checks passed
@asavor
asavor deleted the terminal-safe branch November 20, 2026 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

claude PR primarily authored by Claude merge:same-repo-dependency Needs an unmerged PR in this repo first; no known cross-repo prerequisite. Checks and holds apply.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant