diff --git a/roborock_onboard.sh b/roborock_onboard.sh index f26a68cb..640078e5 100755 --- a/roborock_onboard.sh +++ b/roborock_onboard.sh @@ -216,7 +216,10 @@ read_config() { ui_error "Configuration is missing or is a symbolic link. Run '$0 config'." >&2 return 1 } - IFS= read -r first < "$CONFIG_FILE" || return 1 + IFS= read -r first < "$CONFIG_FILE" || { + ui_error "Configuration is empty or unreadable. Run '$0 config' to replace it." >&2 + return 1 + } [[ "$first" != '# roborock-onboard-config-v2: literal values, not shell code' ]] || version=2 while IFS= read -r line || [[ -n "$line" ]]; do if [[ "$version" -eq 2 && "$line" == "$first" ]]; then @@ -231,15 +234,25 @@ read_config() { return 1 fi else - [[ "$line" == *=* && "$line" != *$'\r'* ]] || return 1 + if [[ "$line" != *=* || "$line" == *$'\r'* ]]; then + # Do not echo the malformed line: it may contain a credential. + ui_error "Invalid configuration line. Run '$0 config' to replace it." >&2 + return 1 + fi key="${line%%=*}" value="${line#*=}" fi case "$key" in ROBOROCK_DOMAIN) - [[ "$domain_seen" -eq 0 ]] || return 1 + [[ "$domain_seen" -eq 0 ]] || { + ui_error "Duplicate configuration key. Run '$0 config' to replace it." >&2 + return 1 + } ROBOROCK_DOMAIN="$value"; domain_seen=1 ;; SYNC_SECRET) - [[ "$secret_seen" -eq 0 ]] || return 1 + [[ "$secret_seen" -eq 0 ]] || { + ui_error "Duplicate configuration key. Run '$0 config' to replace it." >&2 + return 1 + } SYNC_SECRET="$value"; secret_seen=1 ;; *) ui_error "Unknown configuration key. Run '$0 config'." >&2; return 1 ;; esac @@ -303,11 +316,19 @@ load_config() { echo echo "Find your Protocol Sync Secret:" echo - echo " 1. Open Home Assistant (HA)." - echo " 2. Go to Apps → Roborock Local Server." - echo " 3. Press the Open Web UI button." - echo " 4. Log in with your admin password." - echo " 5. Copy the Protocol Sync Secret and paste it into this terminal." + echo " Home Assistant:" + echo " 1. Open Home Assistant (HA)." + echo " 2. Go to Apps → Roborock Local Server." + echo " 3. Press the Open Web UI button." + echo " 4. Log in with your admin password." + echo " 5. Open Protocol Auth and find the Protocol Sync Secret." + echo + echo " Docker / standalone installation:" + echo " 1. Open Roborock Local Server's admin page in your browser." + echo " 2. Log in with your admin password." + echo " 3. Open Protocol Auth and find the Protocol Sync Secret." + echo + echo "Copy the Protocol Sync Secret and paste it into this terminal." echo echo "Paste the Protocol Sync Secret, not your admin password." echo "Your paste will be hidden; press Enter afterward." @@ -333,22 +354,38 @@ display_server() { esac } -# The small embedded supervisor needs Python 3. Prefer a Python already on PATH; -# otherwise ask uv for its existing managed interpreter. No sudo or keychain -# access is used. Python isolates a process group on macOS without a setsid CLI. +# A path is not proof of a usable interpreter: /usr/bin/python3 may be a macOS +# Command Line Tools stub. Probe execution AND the minimum helper version before +# accepting either a PATH candidate or an interpreter returned by uv. +python_is_usable() { + "$1" -c 'import sys; sys.exit(0 if sys.version_info >= (3, 8) else 1)' >/dev/null 2>&1 +} + +# Prefer a working cached/PATH interpreter, then try uv's existing interpreter. +# Suppress candidate errors so a broken CLT stub does not mask a working uv Python. +# No interpreter is installed automatically, and no sudo or keychain access is +# used. Python supplies process isolation on macOS without a separate setsid CLI. ensure_python() { - [[ -z "${PYTHON_BIN:-}" ]] || return 0 - if command -v python3 >/dev/null 2>&1; then - PYTHON_BIN="$(command -v python3)" - elif command -v uv >/dev/null 2>&1; then - PYTHON_BIN="$(uv python find 2>/dev/null)" || { - ui_error "Python 3 is required. Install it with uv, then retry." >&2 - return 1 - } - else - ui_error "Python 3 and uv are required." >&2 - return 1 + local candidate + if [[ -n "${PYTHON_BIN:-}" ]] && python_is_usable "$PYTHON_BIN"; then + return 0 + fi + PYTHON_BIN='' + if candidate="$(command -v python3 2>/dev/null)" && + [[ -n "$candidate" ]] && python_is_usable "$candidate"; then + PYTHON_BIN="$candidate" + return 0 fi + if command -v uv >/dev/null 2>&1; then + if candidate="$(uv python find 2>/dev/null)" && + [[ -n "$candidate" ]] && python_is_usable "$candidate"; then + PYTHON_BIN="$candidate" + return 0 + fi + fi + ui_error "Could not find a working Python 3.8 or newer interpreter." >&2 + echo "If uv is installed, run 'uv python install 3.11', then retry." >&2 + return 1 } # Pointer files contain a private session-directory path, never executable code. @@ -860,7 +897,13 @@ emergency_cleanup() { stop_mitm() { local pointer_status=0 scan_status=0 - header "STOP MITM SERVER" + # Step 8 already displays a numbered heading. Keep the standalone stop + # command's heading, but allow the wizard to suppress a second adjacent box. + case "${1:-}" in + '') header "STOP MITM SERVER" ;; + --no-header) ;; + *) ui_error "Unknown internal stop option." >&2; return 2 ;; + esac read_session_pointer || pointer_status=$? if [[ "$pointer_status" -eq 2 ]]; then return 1; fi if [[ "$pointer_status" -eq 0 ]]; then @@ -910,7 +953,9 @@ status_mitm() { # Wait for a NEW, owned session, not a token from an old log. Only a real HTTP # 200 from the authenticated /flows endpoint permits opening the Capture page. -# curl reads its URL from stdin so even its token stays out of process arguments. +# curl reads its URL from stdin to keep the token out of curl's argument list. +# Browser dispatch is separate: macOS open receives the full authenticated URL +# in its argv, as noted at that call below. This is the web token, not the sync secret. # Ignore user curl config/proxies: this is strictly a local loopback probe. wait_for_mitm() { local deadline=$((SECONDS + STARTUP_TIMEOUT)) candidate token http_status @@ -1259,6 +1304,9 @@ launch_mitm() { echo "Opening the authenticated MITM capture page..." # macOS open returns after dispatching the URL. mitmweb's own browser opener # is disabled above, so the wizard requests only this one Capture tab. + # The session's web authentication token is necessarily included in open's + # URL argument and may be visible to local process inspection while it runs. + # The Protocol Sync Secret is separate and is never passed to open. if ! open "${MITM_AUTH_URL}#/capture" 2>/dev/null; then ui_warning "Could not open the browser automatically." echo "Open the authenticated URL from the private log, then select Capture." @@ -1504,7 +1552,7 @@ start() { echo "Stopping the temporary MITM server..." echo - stop_mitm + stop_mitm --no-header # The Mac cannot verify iPhone VPN/trust settings; these remain manual steps.