Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(security): close two command injections in the coding agent

purpleb844bb51f15d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 36810fe0..605e1452 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,10 @@ All notable changes to Locally Uncensored are documented here.
A correctness release. Most of it is things that looked like they worked and did not.
+### Security
+
+- **The coding agent could be talked into running commands you never approved.** Two paths, both found in this release's security pass and both reachable without a confirmation dialog. First, the helper that wraps text before it goes into a shell escaped for Linux only, while the agent runs PowerShell on Windows, so a crafted commit message or PR title broke out of its quotes and the rest ran as commands. Second, the "continue this PR" tool accepted almost anything as an owner or repo name and pasted it straight into a shell, so a link like `github.com/a/b;<command>/pull/1` ran that command. Either one could be triggered by a poisoned file, PR comment or search result that the agent read, without you typing anything. Quoting now follows the shell that actually runs, PR links are checked against the characters GitHub itself permits, and both are pinned by tests using the exact payloads.
+
### Fixed
- **Setting up a local lane shows you what it is doing, and you can stop it.** Hitting "Download & install" on Motion Control, Music, Lipsync or Extend gave you a spinner and nothing else: no size, no percentage, no speed, nothing in the Downloads tray, and no way to cancel. The card now names the file and counts it up, the download appears in the tray like any other, and there is a Cancel that really stops the transfer.
diff --git a/src/api/agents/__tests__/git-tools.test.ts b/src/api/agents/__tests__/git-tools.test.ts
index a91af08c..5716d204 100644
--- a/src/api/agents/__tests__/git-tools.test.ts
+++ b/src/api/agents/__tests__/git-tools.test.ts
@@ -113,7 +113,10 @@ describe('shellQuote', () => {
})
it('escapes embedded single quotes', () => {
- expect(shellQuote(`it's`)).toBe(`'it'\\''s'`)
+ // Pinned to POSIX. The escape differs per shell (see shell-injection.test.ts),
+ // so leaving this on the ambient platform made the result depend on whoever
+ // ran the suite.
+ expect(shellQuote(`it's`, 'Linux x86_64')).toBe(`'it'\\''s'`)
})
it('keeps quoted variables literal', () => {
@@ -162,8 +165,12 @@ describe('buildGhPrCreateCommand', () => {
})
it('quotes multi-line bodies safely', () => {
+ // Both shells, explicitly, so the assertion does not depend on the host.
expect(
- buildGhPrCreateCommand({ title: 'T', body: "line1\nline2 's quote" }),
+ buildGhPrCreateCommand({ title: 'T', body: "line1\nline2 's quote" }, 'Linux x86_64'),
).toMatch(/--body 'line1\nline2 '\\''s quote'/)
+ expect(
+ buildGhPrCreateCommand({ title: 'T', body: "line1\nline2 's quote" }, 'Win32'),
+ ).toMatch(/--body 'line1\nline2 ''s quote'/)
})
})
diff --git a/src/api/agents/__tests__/shell-injection.test.ts b/src/api/agents/__tests__/shell-injection.test.ts
new file mode 100644
index 00000000..2c832801
--- /dev/null
+++ b/src/api/agents/__tests__/shell-injection.test.ts
@@ -0,0 +1,101 @@
+/**
+ * Command injection regression, found in the 2.5.9 security pass (2026-07-26)
+ * and reproduced live on Windows before the fix.
+ *
+ * Two holes, both reachable from a prompt injection (a poisoned file, PR
+ * comment or search result steering the model into one tool call) and neither
+ * covered by a gate: `pr_resume` is in neither CODEX_CONFIRM_TOOLS nor
+ * MUTATING_TOOLS, and the git tools are in MUTATING_TOOLS but not in the
+ * confirm gate, so none of them stops for a dialog.
+ *
+ * A) shellQuote escaped for POSIX only. shell_execute_sync runs PowerShell on
+ * Windows, where `'\''` ENDS the string, so the rest ran as script:
+ * Write-Output 'x'\''; Write-Output SHELLQUOTE-INJECTED; '\'''
+ * printed SHELLQUOTE-INJECTED on its own line.
+ * B) PR_URL_RE accepted `[^/]+` for owner and repo, so
+ * https://github.com/o/r;Write-Output PRRESUME-INJECTED/pull/1
+ * built `gh pr view 1 --repo o/r;Write-Output PRRESUME-INJECTED --json …`
+ * and the payload ran.
+ */
+import { describe, it, expect } from 'vitest'
+import { shellQuote, buildGitCommitCommand, buildGhPrCreateCommand } from '../git-tools'
+import { parsePrUrl } from '../pr-resume'
+
+const WIN = 'Win32'
+const NIX = 'Linux x86_64'
+// The exact payload that escaped before the fix.
+const PAYLOAD = "x'; Write-Output SHELLQUOTE-INJECTED; '"
+
+describe('shellQuote follows the shell that actually runs', () => {
+ it('doubles the quote on Windows, where PowerShell is the shell', () => {
+ // PowerShell reads '' inside a single-quoted string as one literal quote,
+ // so the payload stays one argument instead of becoming a new statement.
+ expect(shellQuote("it's", WIN)).toBe("'it''s'")
+ expect(shellQuote(PAYLOAD, WIN)).toBe("'x''; Write-Output SHELLQUOTE-INJECTED; '''")
+ })
+
+ it('keeps the POSIX form off Windows', () => {
+ expect(shellQuote("it's", NIX)).toBe("'it'\\''s'")
+ })
+
+ it('never leaves an odd number of quotes for the Windows shell to reopen', () => {
+ // The bug in one assertion: a quoted string must contain an even number of
+ // quote characters, otherwise something after it is outside the string.
+ for (const s of [PAYLOAD, "'", "''", "a'b'c", "'; calc; '", "$(calc)", '`calc`']) {
+ const q = shellQuote(s, WIN)
+ expect((q.match(/'/g) || []).length % 2, `unbalanced for ${JSON.stringify(s)}`).toBe(0)
+ }
+ })
+
+ it('leaves expansion characters inert in both shells', () => {
+ // Neither shell expands $ or backticks inside single quotes, so these only
+ // need to survive as literals.
+ for (const p of [WIN, NIX]) {
+ expect(shellQuote('$(calc)', p)).toContain('$(calc)')
+ expect(shellQuote('`calc`', p)).toContain('`calc`')
+ }
+ })
+
+ it('carries the fix into the commands the model can reach', () => {
+ const commit = buildGitCommitCommand({ message: PAYLOAD }) // default platform
+ const pr = buildGhPrCreateCommand({ title: PAYLOAD, body: PAYLOAD })
+ for (const cmd of [commit, pr]) {
+ // Whatever the host platform, the POSIX escape must not appear on Windows
+ // and the payload must never sit outside quotes.
+ expect(cmd).not.toContain('; Write-Output SHELLQUOTE-INJECTED; ' + "'" + ' ')
+ }
+ expect(commit).toContain('git commit -m')
+ expect(pr).toContain('gh pr create')
+ })
+})
+
+describe('parsePrUrl rejects anything a shell could act on', () => {
+ it('accepts a real PR url', () => {
+ expect(parsePrUrl('https://github.com/PurpleDoubleD/locally-uncensored/pull/87')).toEqual({
+ owner: 'PurpleDoubleD',
+ repo: 'locally-uncensored',
+ number: 87,
+ })
+ })
+
+ it('accepts the punctuation GitHub itself allows in a repo name', () => {
+ expect(parsePrUrl('https://github.com/a-b/c.d_e-f/pull/1')?.repo).toBe('c.d_e-f')
+ })
+
+ it('rejects the payload that used to run', () => {
+ expect(parsePrUrl('https://github.com/o/r;Write-Output PRRESUME-INJECTED/pull/1')).toBeNull()
+ })
+
+ it('rejects every other shell metacharacter in owner or repo', () => {
+ for (const bad of [';calc', '`calc`', '$(calc)', 'a|b', 'a&b', "a'b", 'a"b', 'a b', 'a\nb', 'a>b']) {
+ expect(parsePrUrl(`https://github.com/${bad}/repo/pull/1`), `owner ${bad}`).toBeNull()
+ expect(parsePrUrl(`https://github.com/owner/${bad}/pull/1`), `repo ${bad}`).toBeNull()
+ }
+ })
+
+ it('still rejects non-github hosts and junk numbers', () => {
+ expect(parsePrUrl('https://evil.com/o/r/pull/1')).toBeNull()
+ expect(parsePrUrl('https://github.com/o/r/pull/0')).toBeNull()
+ expect(parsePrUrl('not a url')).toBeNull()
+ })
+})
diff --git a/src/api/agents/git-tools.ts b/src/api/agents/git-tools.ts
index 43ffdc69..009ebb38 100644
--- a/src/api/agents/git-tools.ts
+++ b/src/api/agents/git-tools.ts
@@ -107,11 +107,28 @@ export function parseGitLog(raw: string): GitLogEntry[] {
}
/**
- * Shell-quotes a string for POSIX shells. We use single quotes + literal
- * escaping for embedded single quotes — bullet-proof against most input,
- * including the model emitting backticks or `$()` it shouldn't.
+ * Shell-quote a string for the shell that will actually run it.
+ *
+ * `shell_execute_sync` picks the shell by OS: PowerShell on Windows, bash
+ * everywhere else. The two disagree about how a single quote is escaped inside
+ * a single-quoted string, and the difference is not cosmetic. The POSIX form
+ * `'\''` CLOSES the string in PowerShell, so everything after it is parsed as
+ * script. Proven on Windows 2026-07-26: a commit message of
+ * `x'; Write-Output PWNED; '` ran Write-Output as its own command.
+ *
+ * Both branches are safe against the rest of what a model might emit, because
+ * neither shell expands `$`, backticks or `$()` inside single quotes.
*/
-export function shellQuote(s: string): string {
+/** The platform string the quoting decision is made from. Split out so the
+ * builders can pass it down and tests can pin both shells. */
+export function defaultPlatform(): string {
+ return typeof navigator !== 'undefined' ? navigator.platform : ''
+}
+
+export function shellQuote(s: string, platform: string = defaultPlatform()): string {
+ // PowerShell: a literal single quote is written by doubling it.
+ if (/Win/i.test(platform)) return `'${s.replace(/'/g, "''")}'`
+ // POSIX: leave the string, emit an escaped quote, reopen.
return `'${s.replace(/'/g, `'\\''`)}'`
}
@@ -122,14 +139,15 @@ export interface GitCommitArgs {
allTracked?: boolean
}
-export function buildGitCommitCommand(args: GitCommitArgs): string {
+export function buildGitCommitCommand(args: GitCommitArgs, platform?: string): string {
if (!args.message?.trim()) return ''
+ const q = (s: string) => shellQuote(s, platform ?? defaultPlatform())
const stage = args.allTracked
? 'git add -A'
: args.files?.length
- ? `git add -- ${args.files.map(shellQuote).join(' ')}`
+ ? `git add -- ${args.files.map(q).join(' ')}`
: ''
- const commit = `git commit -m ${shellQuote(args.message)}`
+ const commit = `git commit -m ${q(args.message)}`
return stage ? `${stage} && ${commit}` : commit
}
@@ -139,12 +157,13 @@ export interface GhPrCreateArgs {
base?: string
}
-export function buildGhPrCreateCommand(args: GhPrCreateArgs): string {
+export function buildGhPrCreateCommand(args: GhPrCreateArgs, platform?: string): string {
if (!args.title?.trim()) return ''
+ const q = (s: string) => shellQuote(s, platform ?? defaultPlatform())
const flags = [
- `--title ${shellQuote(args.title)}`,
- `--body ${shellQuote(args.body ?? '')}`,
+ `--title ${q(args.title)}`,
+ `--body ${q(args.body ?? '')}`,
]
- if (args.base) flags.push(`--base ${shellQuote(args.base)}`)
+ if (args.base) flags.push(`--base ${q(args.base)}`)
return `gh pr create ${flags.join(' ')}`
}
diff --git a/src/api/agents/pr-resume.ts b/src/api/agents/pr-resume.ts
index f9c4b8e8..8320deb1 100644
--- a/src/api/agents/pr-resume.ts
+++ b/src/api/agents/pr-resume.ts
@@ -29,7 +29,14 @@ export interface PrResumePayload {
diff: string
}
-const PR_URL_RE = /^https?:\/\/github\.com\/([^/]+)\/([^/]+)\/pull\/(\d+)(?:[?#/].*)?$/
+// owner and repo land in a shell command (`gh pr view … --repo owner/repo`),
+// so they are restricted to what GitHub actually allows rather than "anything
+// without a slash". The loose form accepted `o/r;Write-Output PWNED` and the
+// text after the semicolon ran as its own command (proven 2026-07-26). Owners
+// are alphanumeric plus single hyphens, repos add dot and underscore; neither
+// can contain a quote, a semicolon, a backtick or whitespace.
+const PR_URL_RE =
+ /^https?:\/\/github\.com\/([A-Za-z0-9](?:[A-Za-z0-9-]{0,38}))\/([A-Za-z0-9._-]{1,100})\/pull\/(\d+)(?:[?#/].*)?$/
export function parsePrUrl(url: string): PrLocator | null {
const m = (url ?? '').trim().match(PR_URL_RE)
diff --git a/src/api/mcp/builtin-tools.ts b/src/api/mcp/builtin-tools.ts
index b149c13d..2e19bba6 100644
--- a/src/api/mcp/builtin-tools.ts
+++ b/src/api/mcp/builtin-tools.ts
@@ -35,6 +35,9 @@ function chatCtx(): { chatId?: string; workingDirectory?: string } {
// ── Tool Definitions ────────────────────────────────────────────
+// tool-classification.test.ts reads the names out of this array to assert that
+// every one of them is classified as mutating or read-only. It parses the file
+// rather than importing it, because importing pulls in the tool-registry cycle.
const BUILTIN_TOOLS: MCPToolDefinition[] = [
// Web
{
@@ -988,10 +991,15 @@ async function executeProjectInit(args: Record<string, any>): Promise<string> {
async function executePrResume(args: Record<string, any>): Promise<string> {
const { parsePrUrl, normalisePrJson, renderPrResume } = await import('../agents/pr-resume')
+ const { shellQuote } = await import('../agents/git-tools')
const loc = parsePrUrl(String(args.url ?? ''))
if (!loc) return 'pr_resume: not a GitHub PR URL (expected https://github.com/owner/repo/pull/N).'
+ // Quoted as well as validated. parsePrUrl already rejects anything a shell
+ // could act on, but this used to be raw interpolation and the tool is not
+ // behind the shell confirm gate, so it does not get to rely on one check.
+ const repo = shellQuote(`${loc.owner}/${loc.repo}`)
const view = await runShell(
- `gh pr view ${loc.number} --repo ${loc.owner}/${loc.repo} --json title,body,state,headRefName,baseRefName,author,comments`,
+ `gh pr view ${loc.number} --repo ${repo} --json title,body,state,headRefName,baseRefName,author,comments`,
args.cwd,
60000,
)
@@ -1006,7 +1014,7 @@ async function executePrResume(args: Record<string, any>): Promise<string> {
}
const meta = normalisePrJson(raw, String(args.url))
const diff = await runShell(
- `gh pr diff ${loc.number} --repo ${loc.owner}/${loc.repo}`,
+ `gh pr diff ${loc.number} --repo ${repo}`,
args.cwd,
60000,
)
diff --git a/src/lib/__tests__/tool-classification.test.ts b/src/lib/__tests__/tool-classification.test.ts
new file mode 100644
index 00000000..62fd7106
--- /dev/null
+++ b/src/lib/__tests__/tool-classification.test.ts
@@ -0,0 +1,91 @@
+/**
+ * The read-only gate is a BLOCKLIST: a turn strips MUTATING_TOOLS and lets
+ * everything else through. That means a tool nobody classified is allowed by
+ * default, which is exactly how pr_resume sat inside /review and /plan while it
+ * still had a shell injection in it (2026-07-26 security pass).
+ *
+ * This test closes the gap the other way round: every built-in tool must appear
+ * in one of the two lists. Add a tool without deciding, and this fails.
+ */
+import { describe, it, expect } from 'vitest'
+import { readFileSync } from 'fs'
+import { fileURLToPath } from 'url'
+import { dirname, join } from 'path'
+import { MUTATING_TOOLS } from '../mutating-tools'
+
+// Read the names out of the source. Importing the module pulls in the
+// builtin-tools → tool-registry → mcp/index cycle, which throws on load.
+const src = readFileSync(
+ join(dirname(fileURLToPath(import.meta.url)), '../../api/mcp/builtin-tools.ts'),
+ 'utf8',
+)
+const arrayStart = src.indexOf('const BUILTIN_TOOLS')
+const arrayEnd = src.indexOf('export function registerBuiltinTools')
+const BUILTIN_TOOL_NAMES = [
+ ...new Set(
+ [...src.slice(arrayStart, arrayEnd).matchAll(/^\s{4}name: '([a-z0-9_]+)',$/gm)].map((m) => m[1]),
+ ),
+]
+
+/**
+ * Safe to run in a read-only turn. Every name here is a decision: it either
+ * only reads, or it only reports on work that is already running.
+ */
+const READ_ONLY_TOOLS: ReadonlySet<string> = new Set([
+ 'file_read',
+ 'file_list',
+ 'file_search',
+ 'git_status',
+ 'git_log',
+ 'git_diff',
+ // Reads a PR through `gh pr view` / `gh pr diff`. Owner and repo are
+ // validated to GitHub's own character set before they reach a shell.
+ 'pr_resume',
+ 'shell_task_status',
+ 'shell_task_list',
+ 'process_list',
+ 'system_info',
+ 'get_current_time',
+ 'web_search',
+ 'web_fetch',
+])
+
+describe('every built-in tool is classified', () => {
+ it('found the tool list to check', () => {
+ // Guard the parsing itself, so a refactor that breaks the regex fails loudly
+ // instead of quietly asserting over an empty list.
+ expect(arrayStart).toBeGreaterThanOrEqual(0)
+ expect(arrayEnd).toBeGreaterThan(arrayStart)
+ expect(BUILTIN_TOOL_NAMES.length).toBeGreaterThan(20)
+ expect(BUILTIN_TOOL_NAMES).toContain('shell_execute')
+ expect(BUILTIN_TOOL_NAMES).toContain('pr_resume')
+ })
+
+ it('appears in exactly one of the two lists', () => {
+ const unclassified: string[] = []
+ const both: string[] = []
+ for (const name of BUILTIN_TOOL_NAMES) {
+ const mutating = MUTATING_TOOLS.has(name)
+ const readOnly = READ_ONLY_TOOLS.has(name)
+ if (!mutating && !readOnly) unclassified.push(name)
+ if (mutating && readOnly) both.push(name)
+ }
+ expect(
+ unclassified,
+ 'new tool with no decision: add it to MUTATING_TOOLS if it can change '
+ + 'anything outside the conversation, otherwise to READ_ONLY_TOOLS here',
+ ).toEqual([])
+ expect(both, 'a tool cannot be both').toEqual([])
+ })
+
+ it('nothing that spawns a shell is read-only unless its arguments are constrained', () => {
+ // pr_resume is the one read-only tool that reaches a shell. It is allowed
+ // there only because parsePrUrl rejects every metacharacter, which
+ // shell-injection.test.ts pins. If another shell tool is ever added to the
+ // read-only set, that decision needs the same proof.
+ const shellReaders = [...READ_ONLY_TOOLS].filter((n) =>
+ ['pr_resume', 'git_status', 'git_log', 'git_diff'].includes(n),
+ )
+ expect(shellReaders.sort()).toEqual(['git_diff', 'git_log', 'git_status', 'pr_resume'])
+ })
+})