From 6ad938ef3ea1466513cfd933da6b9ea10b44176f Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Thu, 26 Nov 2026 19:46:21 +0100 Subject: [PATCH] Survive unreadable paths and name a stale tunnel A !run naming a directory the user may not traverse made Path.exists raise PermissionError inside the command classifier, and the exception ended the chat session. The path is now refused with the reason. A tunnel that died with the VPN keeps its port; the launcher then failed to bind a new one and reported "is the host reachable (VPN?) and your key authorised?". It now says the port is held by a tunnel that no longer answers, with the holder's pid when ss can tell. --- client/harness/command_policy.py | 10 ++++++++-- client/tests/test_command_policy.py | 15 +++++++++++++++ scripts/docker/spear | 9 +++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/client/harness/command_policy.py b/client/harness/command_policy.py index 3ec2936..592fcf1 100644 --- a/client/harness/command_policy.py +++ b/client/harness/command_policy.py @@ -236,8 +236,14 @@ def _outside_read_target(self, value: str) -> "tuple[str | None, str]": return None, (f"{literal} contains the sandbox's own mounts " f"and cannot be exposed — name a subdirectory.") - if not Path(literal).exists(): - return None, "" + # A path the user may not even stat is refused with the reason, rather + # than raising out of the classifier and taking the session down. + + try: + if not Path(literal).exists(): + return None, "" + except OSError as exc: + return None, f"{literal} cannot be read here ({exc.strerror or exc})." return literal, "" diff --git a/client/tests/test_command_policy.py b/client/tests/test_command_policy.py index 0c31b8f..ef68d0d 100644 --- a/client/tests/test_command_policy.py +++ b/client/tests/test_command_policy.py @@ -797,6 +797,21 @@ def test_an_input_redirection_reads_and_an_output_redirection_does_not(self): writable = self.policy.classify(f"echo x > {self.outside}/memory.md") self.assertEqual(writable.classification, CommandClassification.DANGEROUS) + def test_a_path_that_cannot_be_examined_is_refused_not_raised(self): + # A directory the user may not traverse made Path.exists raise, and + # the exception took the whole chat session down. + locked = self.outside / "locked" + locked.mkdir() + locked.chmod(0) + + try: + assessment = self.policy.classify(f"ls -la {locked}/inside") + finally: + locked.chmod(0o700) + + self.assertEqual(assessment.host_read_paths, ()) + self.assertIn("cannot be read here", assessment.reason) + def test_a_missing_outside_path_binds_nothing_and_is_not_refused(self): # ENOENT from the command is the truthful answer and the one the model # can act on; a policy error about a file that was never there is not. diff --git a/scripts/docker/spear b/scripts/docker/spear index 6491ddd..79720e4 100755 --- a/scripts/docker/spear +++ b/scripts/docker/spear @@ -249,6 +249,15 @@ backend() { [ -n "$REDS_KEY" ] && SSH_OPTS+=(-o IdentitiesOnly=yes -i "$REDS_KEY") echo "opening the tunnel :$LPORT -> $REDS_HOST:$REDS_PORT" >&2 + # A tunnel that died with the VPN keeps its port, and a new one + # then fails to bind: say that, not "is the host reachable?". + + if (exec 3<>"/dev/tcp/127.0.0.1/$LPORT") 2>/dev/null; then + held=$(ss -ltnpH "sport = :$LPORT" 2>/dev/null | grep -o 'pid=[0-9]*' | head -1) + WHY="port $LPORT is held by a tunnel that no longer answers${held:+ ($held)}: end it and run spear again" + return 1 + fi + if ! ssh "${SSH_OPTS[@]}" -L "$LPORT:localhost:$REDS_PORT" -N -f "$REDS_HOST"; then WHY="tunnel failed: is $REDS_HOST reachable (VPN?) and your key authorised?" return 1