Repository navigation
Security: $'...' quoting bypass, cmd injection in mcp login, managed settings found from the environment - #28
Merged
Conversation
Inside $'...' a backslash escapes the next character, a quote included, so $'\'' is one quote and the ; after it is a real separator. The permission rules' splitter and the protected-path guard's lexer both read it as a plain '...', where the backslash is just a character, and took the rest of the line for one quoted argument: - `echo $'\'' ; rm -rf x #'` was allowed by Bash(echo:*), and bash runs the rm (checked with real bash). - `echo $'\'' ; echo x > tests/a.rs #'` wrote a protected file unseen. src/ansi_c.rs decodes $'...' as bash does (man bash, QUOTING) and is used by permissions::rules (split_into, substitution, words, unquote) and guards::shell (lex, substitution). $$ is read as the pid, so a quote after it stays an ordinary one; $"..." is read as "...". A part with $'...' or $"..." is opaque, so no allow rule passes it. Deny and ask rules are also matched against each form unquoted: `$'\x72m' -rf /`, `$'bash' -c '...'` and plain `'rm' -rf /` or `sudo "rm" -rf /` all run rm, and a deny on rm -rf now meets them. Tests fail with the fix taken out: the allow and deny cases come back Allow, and the guard reports no write target.
The managed file is the administrator's policy over the person running
incode, and its module says its location is fixed so that person cannot
point it somewhere empty. On Windows it was read from the ProgramData
environment variable, which that person sets: with it aimed at an empty
folder, no managed deny, ask or MCP rule applied.
managed_dir now asks Windows for FOLDERID_ProgramData
(SHGetKnownFolderPath). The test points ProgramData, ALLUSERSPROFILE
and SystemDrive at a temp folder and checks the folder does not move;
with the variable read put back it fails ("the environment moved the
managed folder to ...\Temp\...").
Cargo.toml: windows-sys features Win32_UI_Shell and Win32_System_Com;
no new package (Cargo.lock unchanged). README gives the real location.
open_browser ran `cmd /c start "" <url>` on Windows, escaping only `&`. The URL is the authorization endpoint from a remote server's metadata, which the https check lets through with `|`, `^` or `%NAME%` in it. cmd ran the text after a `|`: a login against a hostile server could run any command. Reproduced with the old code: an address ending in `|mkdir,<temp path>` made the folder. Windows now hands the address to ShellExecuteW (COM set up on its own thread), which no shell reads; macOS and Linux keep `open` / `xdg-open` with the address as one argument. Any address that is not http(s) (a path, file:, another scheme) is not opened; it is printed for the user as before. The live test (ignored, it opens a tab; listed in TESTING.md) listens on loopback, opens `http://127.0.0.1:<port>/?q=|mkdir,<path>`, and requires both that a browser asked for it and that no folder was made. It failed on the old code and passes now.
Follow-up to 2d3f2f9, from a review of it. Three ways a command could still hide: - dash. incode runs `sh -c` (tools::shell_choice), and on Debian and Ubuntu that is dash, which reads $' as a `$` and a plain quote (checked: dash printed `$a\tb` where bash printed a tab). Reading only bash's way turned the old hole around: in `echo $'\' ; rm -rf x # '` dash runs the rm and bash does not. A line with $' is now read both ways, in permissions::rules::split_into and guards::shell::collect: a deny or ask rule meets the parts of either reading, every part is opaque (no allow rule passes it), and a write either reading sees is a target. - `\c` moved the closing quote. The decoder let `\c` take the quote as its operand, so `echo $'\c' ; rm -rf x #'` hid the rm. bash finds the end first (a backslash pairs with one character) and decodes after; ansi_c::quoted now does the same, so no escape can move the end. Checked with bash: `$'\c'` ends at its quote, `$'\c\''` is 0x1c and '. - NUL. bash stops the text at a NUL (`$'rm\0junk'` is `rm`; a redirect to $'a.txt\0junk' writes a.txt). The decoder now stops there too, whichever escape spells it (\0, \x00, \000, \c@, \u0000). Each new test fails with its part taken out: with one reading only the guard sees no target and the deny comes back Allow; without the NUL stop the decoder keeps "rm\0x".
long_running ran `ping ... > nul` with the checkout as the job's folder. On Windows the Bash tool runs Git Bash (tools::shell_choice), where `> nul` is an ordinary file, so a job that got as far as its redirect before the test killed it left ?? nul behind, and CI's clean-checkout check failed (PR #28, windows-latest, 4e6735c). The ping needs no redirect: the job manager keeps its output. Not reproduced locally (five runs with the redirect put back left no file: the test kills the job at once, and whether bash opens the redirect first is a race); a Git Bash `> nul` is shown to make the file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three findings from a security review of the code merged in #12–#17 and #27, each reproduced before the fix and covered by a test that fails with the fix taken out.
1.
$'...'quoting hid commands from the permission rules and the protected-path guard (high)Inside bash's
$'...'a backslash escapes the quote, so$'\''is one character and a;after it is real. Both readers took it for a plain'...':echo $'\'' ; rm -rf x #'was allowed byBash(echo:*)(real bash runs therm, checked).echo $'\'' ; echo x > tests/a.rs #'wrote a protected file with no target seen by the guard.rm -rfmissed$'\x72m' -rf x,$'bash' -c '...', and also plain'rm' -rf x/sudo "rm" -rf x.Fix:
src/ansi_c.rsdecodes$'...'as bash does and is used bypermissions::rulesandguards::shell. A part with$'...'/$"..."is opaque (no allow rule passes it), and deny/ask rules also match each form unquoted.2.
incode mcp logincould run a command from a hostile server's metadata (high, Windows)open_browserrancmd /c start "" <url>, escaping only&. Reproduced: an authorization address ending in|mkdir,<path>made the folder. Windows now usesShellExecuteW; only http(s) addresses are opened.3. The managed settings file was located through
%ProgramData%(medium, Windows)The person the policy constrains could point the variable at an empty folder and no managed rule applied. Now
SHGetKnownFolderPath(FOLDERID_ProgramData).Verified locally (Windows)
cargo fmt --checkclean;cargo clippy --all-targets: only the existingwith_tool_serverdead-code warning.cargo test: lib 2167 passed / 0 failed / 10 ignored, bin 52 passed.mcp_oauth::tests::open_browser_runs_nothing_from_the_address: a browser reached the loopback listener and no command ran; it failed on the old code.Cargo.lockunchanged: twowindows-sysfeatures, no new package.Not verified locally: the full Linux/macOS build (C dependencies need those systems). The non-Windows
open_browserblock was compiled on its own forx86_64-unknown-linux-muslandaarch64-apple-darwin. CI is the check for the rest.