From 16a24bca53a78b4d154612b449f85c67950d1bd1 Mon Sep 17 00:00:00 2001 From: dhivadhiva Date: Sun, 15 Nov 2026 04:20:31 +0530 Subject: [PATCH 1/5] Quote bridge commands so a fish login shell cannot reopen them The server's sshd hands the whole command line to the account's login shell. Values were embedded in POSIX single quotes, where nothing is special. fish reads a backslash inside single quotes as an escape, so a value ending in a backslash escaped its closing quote and the rest of the line could be re-read by the shell. Paths and file-name patterns come from the assistant, so on a host whose login shell is fish a read-only tool could be made to run text the app did not intend. The outer command line is now quoted so that no backslash is ever left inside single quotes. Text read by /bin/sh inside the wrapper keeps POSIX quoting, which is correct there. A comment says which shell reads which layer. Tested against real sh and bash with hostile values. fish is not installed where this was written, so its rules are checked against a model; a test runs the same check through real fish when it is present. --- docs/changes/README.md | 1 + docs/changes/bridge-shell-quoting.md | 3 + lib/services/bridge/bridge_tools.dart | 46 +++- test/bridge_shell_quoting_test.dart | 339 ++++++++++++++++++++++++++ test/support/bridge_fakes.dart | 44 +++- 5 files changed, 424 insertions(+), 9 deletions(-) create mode 100644 docs/changes/bridge-shell-quoting.md create mode 100644 test/bridge_shell_quoting_test.dart diff --git a/docs/changes/README.md b/docs/changes/README.md index f830a2b..5e48bee 100644 --- a/docs/changes/README.md +++ b/docs/changes/README.md @@ -28,6 +28,7 @@ One file per piece of work, named for it: | `remote-ops.md` | Commands on many servers, archive and search actions | | `mosh.md` | Mosh | | `localization.md` | Translations | +| `bridge-shell-quoting.md` | AI Bridge commands quoted safely for a fish login shell | Write for the people using the app, in the sections `CHANGELOG.md` uses: `### Added`, `### Changed`, `### Fixed`, `### Security`. One line per change, diff --git a/docs/changes/bridge-shell-quoting.md b/docs/changes/bridge-shell-quoting.md new file mode 100644 index 0000000..905bc38 --- /dev/null +++ b/docs/changes/bridge-shell-quoting.md @@ -0,0 +1,3 @@ +### Security + +- An AI Bridge read on a server whose login shell is fish could be made to run text the app did not intend, through a file name, folder or pattern that contained a backslash. Commands are now quoted so that no login shell (fish included) reads a backslash as anything but a backslash. diff --git a/lib/services/bridge/bridge_tools.dart b/lib/services/bridge/bridge_tools.dart index d9a2cb4..bee3883 100644 --- a/lib/services/bridge/bridge_tools.dart +++ b/lib/services/bridge/bridge_tools.dart @@ -26,8 +26,11 @@ import 'bridge_tool_types.dart'; /// The commands the read tools run, separate from execution so the quoting /// can be asserted character for character. Every value that comes from an -/// assistant reaches the shell only through [posixSingleQuote]; numbers are -/// validated ints before they get here. +/// assistant reaches a shell only through a quoting function: values inside +/// a template (parsed by `/bin/sh`) through [posixSingleQuote], and the +/// whole template on the login shell's command line through +/// [BridgeCommands.loginShellQuote]; numbers are validated ints before they +/// get here. class BridgeCommands { const BridgeCommands._(); @@ -64,9 +67,15 @@ class BridgeCommands { /// does not reliably stop a remote process that has no terminal. A TERM /// or HUP sent to the wrapper itself is passed on in the same way. /// - /// The outer command is one `sh -c` with single-quoted arguments, which - /// every login shell parses the same way; the template reaches the inner - /// shell as `$1`, quoted once. + /// Two layers, two parsers. The OUTER command line returned here is parsed + /// by the account's LOGIN shell (sh, bash, zsh, fish, ...), so the template + /// reaches it through [loginShellQuote]. The wrapper script is fixed text + /// with no backslash, and runs under `sh -c`. The template itself (built + /// by [journal], [tailFile], [findByName] and the other command classes) + /// reaches `/bin/sh -c "$1"` as plain data and is parsed by `/bin/sh` + /// alone, so the values quoted INSIDE a template use [posixSingleQuote], + /// which is exact for `/bin/sh`. Never let a login shell parse a + /// [posixSingleQuote]d value: under fish a backslash is special there. static String supervised(String command) { const wrapper = r'''exec 3<&0 t= @@ -91,7 +100,32 @@ s=$? kill $w 2>/dev/null exit $s'''; return 'sh -c ${posixSingleQuote(wrapper)} ssg-bridge ' - '${posixSingleQuote(command)}'; + '${loginShellQuote(command)}'; + } + + /// [value] as one word for any login shell's command line, not only the + /// POSIX ones. [posixSingleQuote] is exact for sh, bash and zsh, but fish + /// reads `\\` and `\'` inside single quotes as escapes, so a value with a + /// backslash comes out changed, and one ending in a backslash escapes its + /// closing quote. Here no backslash is ever inside single quotes: a quote + /// is `'\''` (a backslash outside any quotes) and a backslash is `'"\\"'` + /// (double quotes, where `\\` is one backslash in sh, bash, zsh and fish + /// alike). For a value with no backslash the result is exactly + /// [posixSingleQuote]'s. + static String loginShellQuote(String value) { + final out = StringBuffer("'"); + for (final unit in value.runes) { + final c = String.fromCharCode(unit); + if (c == "'") { + out.write(r"'\''"); + } else if (c == r'\') { + out.write(r"""'"\\"'"""); + } else { + out.write(c); + } + } + out.write("'"); + return out.toString(); } /// Seconds `search_files` gives `find` when the server has `timeout`. diff --git a/test/bridge_shell_quoting_test.dart b/test/bridge_shell_quoting_test.dart new file mode 100644 index 0000000..3234db9 --- /dev/null +++ b/test/bridge_shell_quoting_test.dart @@ -0,0 +1,339 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:secure_shell_go/services/bridge/bridge_tools.dart'; +import 'package:secure_shell_go/services/log_tail.dart'; +import 'package:secure_shell_go/services/shell_quote.dart'; + +import 'support/bridge_fakes.dart'; +import 'support/bridge_temp_dir.dart'; + +/// Which shell parses which layer of a bridge command: +/// - the OUTER line `sh -c '' ssg-bridge '