Skip to content

fix(viewer): request head checks, no body before the login, agent hardening - #196

Merged
Derrors merged 2 commits into
mainfrom
fix/p0-viewer-agent
Nov 25, 2026
Merged

Derrors merged 2 commits into
mainfrom
fix/p0-viewer-agent

Conversation

@Derrors

@Derrors Derrors commented Nov 25, 2026

Copy link
Copy Markdown
Owner

What and why

P0, first batch, from a full review of main after #195 (viewer and AI-task agents):

  • Request heads (Head::parse): the target must be a path. GET @evil.example/ HTTP/1.1 over plain HTTP from the LAN was redirected to https://host:port@evil.example/, which a browser follows to evil.example; a target with a bare LF could add a line to Location. Now: target starting with / (not //), only printable text, tabs and CRLF in the head, HTTP/1.0 or 1.1, one Host, one Content-Length of digits, never a length together with Transfer-Encoding; otherwise 400. A chunked body outside /mcp gets 411 instead of being read as empty.
  • No body before the login. A keep-alive request read its whole body (up to 60 s) before the login check, and stopped counting against its machine's limit of 8 unauthenticated connections once its head was in: 64 bodies that never came, from one machine without a token, held every place. Now the body is read after the login passes, and the connection counts against the limit until then.
  • Video acks past what was sent are taken as all of it (sent - acked underflowed: a panic in debug builds, a frozen picture in release; a read-only token could send it).
  • Origin without a port is the scheme's default (443/80).
  • AI-task agents (agent.rs):
    • the prompt goes last after --; claude's -p is a flag, so a prompt starting with --permission-mode=bypassPermissions was taken as an option;
    • the task's token reaches the MCP server through a 0600 file in the agent's private directory (mcp --task-token-file, hidden), not the command line, which any local user can read; removed when the run ends;
    • TG_BOT_TOKEN, BARK_KEY and IPHONE_CONTROL_VIEWER_TOKEN aren't passed to the agent (the MCP server reads the settings file itself). Defense in depth only: Codex's read-only sandbox can still read the settings file, which is the user's own;
    • output read by bytes, so a non-UTF-8 line is skipped instead of ending the run; a run that ends before the agent was waited for stops its process group.

Tests

1. Unit tests

  • cargo test --workspace --all-features --locked: 558 passed; clippy -D warnings; cargo fmt --check for all 11 packages; node --test tests/viewer/*.test.mjs: 36 passed; tests/check_regression_cases.py: 0 missing
  • New: only_a_plain_request_head_is_taken, a_body_is_not_waited_for_before_the_login, a_report_beyond_what_was_sent_is_taken_as_all_of_it, a_prompt_is_never_an_option_and_the_token_never_on_the_command_line; default ports in on_the_lan_its_own_names_but_no_others
  • No existing test removed, ignored or loosened

2. Real-device regression

  • Device, host: iPhone 15 Pro Max (iPhone16,2), iOS 27.2.1, native control; Debian 12; the viewer service with --lan
  • tests/device/run.py: 16 calls, 0 failed

3. Regression cases

4. Real-device proof

  • Against the deployed viewer from the LAN address over plain HTTP: GET /stream?x=1 → 307 to https://<lan>:8202/stream?x=1 as before; GET @evil.example/ → 400; a target with a bare LF → 400; two Content-Length → 400
  • Not logged in, keep-alive, Content-Length: 4000 with 1 byte sent: 401 at once (before: the read timeout)
  • Logged in, keep-alive: a POST with a body and a GET right after it on one connection both answered
  • AI task from the control page with claude, prompt --permission-mode=bypassPermissions 请只调用一次 iphone_status…: run as the task's text, one iphone_status step recorded for the task (so the MCP server joined it through the token file), completed
  • AI task with codex: completed. While it ran, the MCP server's command line held only the token file's path, the agent's command line had no token, and its environment had no BARK_KEY (the viewer's has one); the token file was gone afterwards
  • Untested: a macOS host; the 64-connection exhaustion itself (the per-machine limit and the no-body-before-login path are tested)

Checklist

  • MCP tool names, parameters and default outputs stay compatible (D10); mcp --task-token-file is hidden and only the viewer uses it
  • No new dependencies
  • CHANGELOG.md and CHANGELOG.zh-CN.md (Unreleased)
  • No private data in the outputs above

🤖 Generated with Claude Code

Derrors added a commit that referenced this pull request Nov 25, 2026
… token dir, stderr)

- mcp --task-token-file: an empty file, or one given with --url (also from
  IPHONE_CONTROL_URL), stops the server instead of running it outside the task;
  the viewer no longer passes IPHONE_CONTROL_URL/CA to its agents
- request heads: the target is visible ASCII only; header names are tokens
  (`Content-Length : 1` and a name with blanks are 400); a line without a
  colon (folded or garbage) is 400 instead of skipped
- token files live in the private directory's `tokens/`, beside the agent's
  `work/`, named with a counter, and removed if writing fails
- the agent's stderr is read by bytes like its stdout
- the login form is read within the head timeout (10 s), not the body's 60 s

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

Derrors commented Nov 25, 2026

Copy link
Copy Markdown
Owner Author

Review fixes (2e6152c):

  1. mcp --task-token-file fails closed: an empty (or blank) file stops the server (read_task_token), and the flag conflicts_with = "url", also when the URL comes from IPHONE_CONTROL_URL. The viewer no longer passes IPHONE_CONTROL_URL/IPHONE_CONTROL_CA to its agents, so that can't stop their MCP server. Tests an_empty_task_token_file_is_refused, the_remote_viewer_from_a_flag_or_the_environment.
  2. Head::parse: the target is visible ASCII only; header names must be tchars (Content-Length : 1, Host: a → 400); the head's ending CRLFs are trimmed and any other line without a colon (folded, garbage, an empty line) → 400.
  3. TokenFile: in the private directory's tokens/ (0700), beside the agent's work/ (0700, now its cwd), named task-<pid>-<counter>.token; Self is built before writing, so a failed write removes the file.
  4. The agent's stderr is read with split(b'\n') + from_utf8_lossy, like stdout.
  • POST /login reads its ≤4 KB form within HEAD_TIMEOUT (10 s).
  • New test the_place_is_kept_until_the_login_passes: unauthenticated → 401 and the Early place is still held; chunked outside /mcp → 411; logged in → the place is let go. More bad heads in only_a_plain_request_head_is_taken.
  • Not done here: origin_allowed scheme vs TLS (P1, with the security headers).

Workspace: 560 tests passed, clippy, fmt. Device (iPhone 15 Pro Max iOS 27.2.1; viewer on the LAN, plain HTTP): tab target, non-ASCII target, Content-Length : 1, a line without a colon and a folded line → 400; a normal request → 307. An empty token file: mcp exits 1 with "the task token file is empty"; with IPHONE_CONTROL_URL set: refused by clap. A claude task and a codex task from the control page completed (their iphone_status step recorded under the task); during the codex run the agent's cwd was work/, the token file was in tokens/, the MCP server's command line held only its path, and none of codex, its helper or the MCP server had IPHONE_CONTROL_URL, BARK_KEY, IPHONE_CONTROL_TASK_TOKEN or IPHONE_CONTROL_VIEWER_TOKEN; the file was gone afterwards. run.py 16/16.

Derrors and others added 2 commits November 25, 2026 20:22
…dening

From a full review of main after #195:

- Request heads: the target must be a path (`GET @evil.example/` made the
  HTTPS redirect `https://host:port@evil.example/`), no control
  characters or lone CR/LF (a header line in `Location`), HTTP/1.0 or
  1.1 only, one Host, one Content-Length of digits only, never a length
  and Transfer-Encoding together. Anything else gets 400. A chunked body
  outside /mcp gets 411 instead of being read as empty.
- Nothing of a body is read before the login, and the connection counts
  against its machine's limit until then: a keep-alive request read its
  whole body first, so 64 bodies that never came, from one machine
  without a token, held every place.
- Video acks past what was sent are taken as all of it (u64 underflow:
  a panic in debug, a frozen picture in release).
- Origin without a port: the scheme's default (443/80).
- AI task agents: the prompt goes last after `--` (claude's -p is a flag,
  so a prompt starting with `--permission-mode=...` was an option); the
  task's token reaches the MCP server through a 0600 file
  (`mcp --task-token-file`, hidden) instead of the command line every
  user can read; notification keys and the viewer login aren't passed
  to the agent; output lines that aren't UTF-8 are skipped instead of
  ending the run, and a run that ends early stops the agent's group.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
… token dir, stderr)

- mcp --task-token-file: an empty file, or one given with --url (also from
  IPHONE_CONTROL_URL), stops the server instead of running it outside the task;
  the viewer no longer passes IPHONE_CONTROL_URL/CA to its agents
- request heads: the target is visible ASCII only; header names are tokens
  (`Content-Length : 1` and a name with blanks are 400); a line without a
  colon (folded or garbage) is 400 instead of skipped
- token files live in the private directory's `tokens/`, beside the agent's
  `work/`, named with a counter, and removed if writing fails
- the agent's stderr is read by bytes like its stdout
- the login form is read within the head timeout (10 s), not the body's 60 s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Derrors
Derrors force-pushed the fix/p0-viewer-agent branch from 2e6152c to 3e5011b Compare November 25, 2026 12:23
@Derrors
Derrors merged commit e478adc into main Nov 25, 2026
7 checks passed
@Derrors
Derrors deleted the fix/p0-viewer-agent branch November 25, 2026 12:30
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