Skip to content

The agent that grows with you

fix(whatsapp): refuse legacy-pidfile kills without a start-time fingerprint

liuhao10247a23b00101ba
diff --git a/plugins/platforms/whatsapp/adapter.py b/plugins/platforms/whatsapp/adapter.py
index 485608f9917..c390a40543b 100644
--- a/plugins/platforms/whatsapp/adapter.py
+++ b/plugins/platforms/whatsapp/adapter.py
@@ -100,15 +100,20 @@ def _kill_port_process(port: int) -> None:
os.kill(pid, signal.SIGTERM)
-def _bridge_pid_is_ours(pid: int, session_path: Path, expected_start) -> bool:
- """``pid`` alive AND still our bridge: kernel start time (definitive), else legacy ``node`` + session path in cmdline."""
+def _bridge_pid_is_ours(pid: int, expected_start) -> bool:
+ """``pid`` alive AND still our bridge: kernel start time (definitive); fail closed without it.
+
+ Legacy pidfiles record only the PID. The old fallback accepted a ``node`` + session-path cmdline
+ substring as kill evidence — but a log tail, editor, or grep that merely *mentions* the session
+ path matches that same substring (#116883), so a legacy pidfile could signal a stranger. Without
+ a start-time fingerprint the caller must reap via the bridge-port scan instead.
+ """
from gateway import status
if not status._pid_exists(pid):
return False
- if expected_start is not None:
- return status.get_process_start_time(pid) == expected_start
- cmdline = status._read_process_cmdline(pid)
- return bool(cmdline) and ("node" in cmdline) and (str(session_path) in cmdline)
+ if expected_start is None:
+ return False
+ return status.get_process_start_time(pid) == expected_start
def _unlink_quietly(path: Path) -> None:
@@ -129,13 +134,15 @@ def _kill_stale_bridge_by_pidfile(session_path: Path) -> None:
except (ValueError, OSError, TypeError, IndexError):
_unlink_quietly(pid_file)
return
- if _bridge_pid_is_ours(pid, session_path, recorded_start):
+ if _bridge_pid_is_ours(pid, recorded_start):
with suppress(OSError): # ProcessLookupError / PermissionError included
os.kill(pid, signal.SIGTERM)
logger.info("[whatsapp] Killed stale bridge PID %d from pidfile", pid)
elif _pid_exists(pid):
- logger.warning("[whatsapp] Not killing pidfile PID %d: it is no longer the bridge (recycled onto an unrelated process); "
- "skipping to avoid killing a stranger.", pid)
+ reason = ("legacy pidfile lacks a start-time fingerprint and cmdline substring evidence can name a stranger"
+ if recorded_start is None else "it is no longer the bridge (recycled onto an unrelated process)")
+ logger.warning("[whatsapp] Not killing pidfile PID %d: %s; "
+ "skipping to avoid killing a stranger.", pid, reason)
_unlink_quietly(pid_file)
diff --git a/tests/gateway/test_whatsapp_bridge_pidfile.py b/tests/gateway/test_whatsapp_bridge_pidfile.py
index d628479b65f..93f98231d76 100644
--- a/tests/gateway/test_whatsapp_bridge_pidfile.py
+++ b/tests/gateway/test_whatsapp_bridge_pidfile.py
@@ -9,8 +9,10 @@ the user's browser main process got SIGTERMed, closing the browser at irregular
intervals (no crash, no coredump — a clean kill of a stranger).
These tests prove the identity guard: a PID is only signalled when it is still
-our bridge (kernel start time matches, or — for legacy pidfiles — its command
-line names node + this session). A recycled PID is left alone.
+our bridge (kernel start time matches). A recycled PID — or a legacy pidfile
+with no start-time fingerprint — is left alone: a ``node`` + session-path
+cmdline substring also matches log tails, editors, and greps that merely
+mention the session (#116883), so it is not accepted as kill evidence.
"""
import subprocess
@@ -32,10 +34,10 @@ from plugins.platforms.whatsapp.adapter import (
from gateway.status import get_process_start_time, _pid_exists
-def _spawn_sleeper(*extra_argv) -> subprocess.Popen:
- """Spawn a real, short-lived process; optional extra argv shapes its cmdline."""
+def _spawn_sleeper(*extra_argv, seconds: float = 0.2) -> subprocess.Popen:
+ """Spawn a real process living ``seconds``; optional extra argv shapes its cmdline."""
return subprocess.Popen(
- [sys.executable, "-c", "import time; time.sleep(0.2)", *extra_argv]
+ [sys.executable, "-c", f"import time; time.sleep({seconds})", *extra_argv]
)
@@ -77,18 +79,24 @@ class TestIdentityGuard:
proc.wait()
- def test_legacy_pidfile_kills_matching_bridge_cmdline(self, tmp_path):
- """Legacy pidfile: a PID whose cmdline names node + session IS reaped."""
+ def test_legacy_pidfile_refuses_cmdline_only_kill(self, tmp_path):
+ """Legacy pidfile (pid only): a node+session-looking cmdline is NOT trusted — fail closed (#116883).
+
+ The same substring evidence matches a log tail / editor / grep that merely mentions the
+ session path, so without a start-time fingerprint the guard must refuse the kill; the
+ bridge-port scan reaps the orphan instead.
+ """
# Shape the cmdline to look like the node bridge for this session.
- proc = _spawn_sleeper("node", str(tmp_path))
+ proc = _spawn_sleeper("node", str(tmp_path), seconds=30)
try:
(tmp_path / "bridge.pid").write_text(str(proc.pid)) # legacy: pid only
_kill_stale_bridge_by_pidfile(tmp_path)
- assert _wait_dead(proc), "a cmdline-confirmed bridge should be killed"
+ time.sleep(0.5) # give a wrongly-delivered SIGTERM time to land and be observed
+ assert proc.poll() is None, "a cmdline-only match must not be killed (fail closed)"
+ assert not (tmp_path / "bridge.pid").exists()
finally:
- if proc.poll() is None:
- proc.kill()
- proc.wait()
+ proc.kill()
+ proc.wait()
class TestKillPortProcess: