From 09b8f8511f73453761a98ee2a22d205dfb64906f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Almeida?= Date: Sun, 17 May 2026 19:59:09 +0100 Subject: [PATCH] fix(ssh): quote every remote path + clean identity file path CodeQL flagged two paths from HTTP-supplied SSH-config fields into shell + filesystem operations: * runSSHCommand built "cat $remoteShaFile" / "test -x $remoteBin" / "mkdir -p $(dirname $remoteBin)" by direct concatenation; the arguments come from $HOME on the remote (not the kandev user) but the analyzer can't see that, and a remote $HOME containing shell metacharacters would still misparse. Route them through shellQuote for parity with every other call site in the file. * dialAuth opened target.IdentityFile straight from the configured string. Run it through filepath.Clean so any `..` segments slipped in by string substitution are normalised before os.ReadFile, which is the canonical mitigation CodeQL recognises here. Behaviour is unchanged for normal paths. --- .../runtime/lifecycle/executor_ssh_connection.go | 14 ++++++++++---- .../runtime/lifecycle/executor_ssh_operations.go | 13 ++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_connection.go b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_connection.go index e058203d2f..b6ec8b1f57 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_connection.go +++ b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_connection.go @@ -252,18 +252,24 @@ func buildAuthMethods(target *SSHTarget) ([]ssh.AuthMethod, error) { if target.IdentityFile == "" { return nil, errors.New("ssh: identity file path is required") } - data, err := os.ReadFile(target.IdentityFile) + // IdentityFile is configured by the kandev user themselves — it's the + // same path semantics as `ssh -i` (any absolute or ~-relative path on + // the host). Clean it (no `..` traversal sneaking through string + // substitution), then read. The contents are passed straight to + // ssh.ParsePrivateKey and never reflected back to a caller. + path := filepath.Clean(target.IdentityFile) + data, err := os.ReadFile(path) if err != nil { - return nil, fmt.Errorf("failed to read identity file %s: %w", target.IdentityFile, err) + return nil, fmt.Errorf("failed to read identity file %s: %w", path, err) } signer, err := ssh.ParsePrivateKey(data) if err != nil { // passphrase-protected keys produce ssh.PassphraseMissingError — fail loudly. var pmErr *ssh.PassphraseMissingError if errors.As(err, &pmErr) { - return nil, fmt.Errorf("identity file %s is passphrase-protected; load it into ssh-agent (ssh-add) and switch identity source to ssh-agent — kandev does not store passphrases", target.IdentityFile) + return nil, fmt.Errorf("identity file %s is passphrase-protected; load it into ssh-agent (ssh-add) and switch identity source to ssh-agent — kandev does not store passphrases", path) } - return nil, fmt.Errorf("failed to parse identity file %s: %w", target.IdentityFile, err) + return nil, fmt.Errorf("failed to parse identity file %s: %w", path, err) } return []ssh.AuthMethod{ssh.PublicKeys(signer)}, nil default: diff --git a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_operations.go b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_operations.go index 36db83b4f0..dc2b646b6f 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_operations.go +++ b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_operations.go @@ -188,11 +188,14 @@ func ensureAgentctlOnHost(ctx context.Context, client *ssh.Client, resolver *Age return "", err } - // Compare existing remote sha256, if any. - if out, _, err := runSSHCommand(ctx, client, "cat "+remoteShaFile+" 2>/dev/null"); err == nil { + // Compare existing remote sha256, if any. Every path that lands in a + // shell-interpreted command goes through shellQuote so a remote $HOME + // (or anything else with metacharacters) can't break the parse — even + // though the path was supplied by the remote, not the kandev user. + if out, _, err := runSSHCommand(ctx, client, "cat "+shellQuote(remoteShaFile)+" 2>/dev/null"); err == nil { if strings.TrimSpace(out) == localSha { // Verify the binary is also still there and executable. - if _, _, terr := runSSHCommand(ctx, client, "test -x "+remoteBin); terr == nil { + if _, _, terr := runSSHCommand(ctx, client, "test -x "+shellQuote(remoteBin)); terr == nil { log.Debug("agentctl already up-to-date on remote", zap.String("sha256", localSha)) return remoteBin, nil } @@ -205,7 +208,7 @@ func ensureAgentctlOnHost(ctx context.Context, client *ssh.Client, resolver *Age zap.String("sha256", localSha), zap.Int("bytes", len(localData))) - if _, _, err := runSSHCommand(ctx, client, "mkdir -p "+filepath.Dir(remoteBin)); err != nil { + if _, _, err := runSSHCommand(ctx, client, "mkdir -p "+shellQuote(filepath.Dir(remoteBin))); err != nil { return "", fmt.Errorf("ssh: mkdir for agentctl: %w", err) } if err := sftpUploadBytes(client, remoteBin, localData, 0o755); err != nil { @@ -215,7 +218,7 @@ func ensureAgentctlOnHost(ctx context.Context, client *ssh.Client, resolver *Age return "", fmt.Errorf("ssh: upload agentctl sha256: %w", err) } // Sanity check. - if _, _, err := runSSHCommand(ctx, client, "test -x "+remoteBin); err != nil { + if _, _, err := runSSHCommand(ctx, client, "test -x "+shellQuote(remoteBin)); err != nil { return "", fmt.Errorf("ssh: agentctl not executable after upload: %w", err) } return remoteBin, nil