Skip to content

fix: viewer and host configuration (review P1-3) - #202

Open
Derrors wants to merge 2 commits into
mainfrom
fix/p1-viewer-host
Open

Derrors wants to merge 2 commits into
mainfrom
fix/p1-viewer-host

Conversation

@Derrors

@Derrors Derrors commented Nov 25, 2026

Copy link
Copy Markdown
Owner

What and why

P1, third batch, from the full review of main after #195: the viewer and the host's configuration.

  • CSP and Referrer-Policy: the control page and the login page now carry Content-Security-Policy: default-src 'none'; script-src 'sha256-…' (each of their own inline scripts by hash, worked out from the page as served, so each language gets its own), style-src 'unsafe-inline' (the page sets style attributes), and img-src 'self' data: blob:; connect-src 'self'; media-src 'self' blob:; base-uri 'none'; form-action 'self'; frame-ancestors 'none'. Every answer, stream and redirect sends Referrer-Policy: no-referrer.
  • Write timeouts: writing an answer, or one event of /api/agent/tasks/{id}/events or /open, is limited to 30 s (write_timed). A client that read nothing held its connection's place for good.
  • Origin and scheme: origin_allowed takes tls. With HTTPS on, https:// origins are accepted, and http:// only for the loopback names (the network is redirected to HTTPS). With it off, only http://, since this server serves no HTTPS page.
  • TLS files: written atomically (temporary file, fsync, rename; the key 0600 from the start). names.json, which makes the pair reused, is removed first and written last: a stop halfway used to leave a new key beside the old certificate, reused, and "the certificate and key don't match" on every start after.
  • Tokens file:
    • watched by (modified, len, ino): on file systems with whole seconds, a token removed within the same second as another change stayed valid (each save is a new file renamed into place, so the inode always changes);
    • add, renew and remove (and the first load) hold a flock on viewer-tokens.lock across read, change and write.
  • env file:
    • load_into_process takes a key's last value (an empty last one leaves it unset), as systemd and the shell read the file; it used to take the first;
    • save writes a temporary file of its own (create_new, pid and counter) instead of env.tmp.
  • The agent's MCP and the env file: with --task-token-file on the command line, IPHONE_CONTROL_URL/CA from the settings file are skipped. They made the viewer's agent MCP fail on conflicts_with = "url" (review of fix(viewer): request head checks, no body before the login, agent hardening #196). Stale token files are cleared when the viewer starts (review of fix(viewer): request head checks, no body before the login, agent hardening #196: PID reuse).
  • Services:
    • program paths with spaces are allowed (ExecStart="…", which managed already recognises), and $, backquotes, %, quotes, backslashes and control characters are refused (check_exe);
    • resume_viewer (and pause_viewer's fallback) uses start_managed, not enable --now, which turned the viewer's start at login back on;
    • a failed update job setup runs the Manual path once to remove what it wrote (only its own files).
  • Read-only page: no finger, warm-up or wheel goes to the phone from the picture (each touch got a 403 and an error toast); a note at most every 3 s; no hover dot.

Tests

1. Unit tests

  • cargo test --workspace --all-features --locked: 587 passed; clippy -D warnings; cargo fmt --check for all 11 packages; node --test tests/viewer/*.test.mjs: 37 passed; tests/check_regression_cases.py: 0 missing
  • New:
    • Rust: pages_allow_only_their_own_scripts, a_client_that_reads_nothing_is_let_go, a_pair_written_halfway_is_made_again, a_removed_token_is_noticed_within_the_same_second, tokens_added_at_once_are_all_kept, the_last_value_of_a_key_wins, service_program_paths; scheme cases in on_the_lan_its_own_names_but_no_others; stale token clearing in a_prompt_is_never_an_option_and_the_token_never_on_the_command_line;
    • page: a read-only page sends no finger and says so once in a while
  • No test removed, ignored or loosened (origin_allowed and action_allowed calls gained the tls argument)

2. Real-device regression

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

3. Regression cases

4. Real-device proof

  • Headless Chromium (zenika/alpine-chrome, against the deployed viewer):

    • the control page loaded with the policy and no CSP message in the console;
    • its script ran: the device's name, from /api/info, rendered 5 times, and the picture status changed from the static "正在连接…" to the script's "正在连接画面…";
    • the login page, in Chinese and in English, showed no CSP message and its script set data-scheme.

    A control page with a script the policy doesn't name showed "Refused to execute inline script" and kept its static text, so a violation would have shown.

  • With HTTPS on, POST /api/button: Origin: http://192.168.10.100:8202 → 403; https://192.168.10.100:8202 and http://127.0.0.1:8202 → 200

  • A settings file (temporary HOME) with IPHONE_CONTROL_URL:

    • mcp --task-token-file started (it then ended on its closed stdin), where it used to stop on the conflict;
    • plain mcp with the same file used that URL;
    • --url given with the token file is still refused.
  • Six viewer token add at once (temporary data directory): all six kept

  • Not tried:

    • Safari on the phone (the phone can't reach this host's LAN address: "the network connection was lost", also for plain HTTP through a test forward);
    • a macOS service with a space in its path;
    • the update job's partial-setup cleanup (code review);
    • a stalled client against the real viewer (unit test only).

Checklist

  • MCP tool names, parameters and default outputs stay compatible (D10)
  • No new dependencies (base64 for the hashes is a few lines here, to keep Cargo.lock as it is)
  • CHANGELOG.md and CHANGELOG.zh-CN.md (Unreleased)
  • No private data in the outputs above

🤖 Generated with Claude Code

Derrors and others added 2 commits November 26, 2026 00:32
- pages carry a Content-Security-Policy naming their own inline scripts by
  hash; every answer sends Referrer-Policy: no-referrer; answers and event
  stream writes time out (30 s)
- Origin is checked against the scheme served (HTTPS on: http only from this
  machine; off: no https)
- the viewer's certificate, key and names.json are written atomically, names
  last, so a stop halfway makes a new pair instead of a mismatched one
- the tokens file is watched by (time, length, inode); token commands hold a
  flock while they read, change and write
- the env file: a key's last value wins; saves use a temporary file of their
  own; `mcp --task-token-file` skips IPHONE_CONTROL_URL/CA from it
- services: paths with spaces are quoted, `$`, backquotes and `%` refused;
  a paused viewer service is started again without enabling it; a failed
  update job setup removes what it wrote
- stale task token files are cleared when the viewer starts
- a read-only page sends no touches from the picture

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ask-token-file, /mcp writes)

- Turning the update job off unloads it as far as it can: when systemctl or
  launchctl fails (often why setting it up just failed) the unit, timer, plist
  and launcher written here still go, and the error comes after.
- `clear_stale_tokens` only removes token files of this process or of one
  that is gone: the directory is shared by every viewer of the user.
- `mcp --task-token-file` (or `=FILE`) is found after the root options, for
  `mcp` only, from the raw arguments (non-UTF-8 ones don't panic).
- `/mcp` writes its answer and each event within the write limit, and sends
  Referrer-Policy like every other answer; so do the redirect and the MJPEG
  stream; `linger` bounds its shutdown (TLS close_notify) by its second.
- `write_atomic` makes a new temporary file each time and sets its mode;
  the token lock retries on EINTR; the page is built once; `https` reused.

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

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