Skip to content

fix: detect actual home directory for SFTP auto-open - #461

Merged
binaricat merged 4 commits into
mainfrom
fix/sftp-home-dir
Mar 23, 2026
Merged

fix: detect actual home directory for SFTP auto-open#461
binaricat merged 4 commits into
mainfrom
fix/sftp-home-dir

Conversation

@binaricat

Copy link
Copy Markdown
Owner

Summary

  • Query the remote server for the real home directory instead of hardcoding /home/{username}
  • Two detection methods with fallback chain:
    1. SSH exec echo ~ — reliable for any user
    2. SFTP realpath('.') — SFTP cwd is typically the home dir
    3. Hardcoded /home/{username} and /root candidates (unchanged fallback)

Root cause

The SFTP auto-open sidebar assumed all non-root users have their home at /home/{username}, which fails for:

  • BSD systems (/usr/home)
  • Solaris (/export/home)
  • Custom configurations
  • Service accounts (/var/lib/...)

Fixes #458

Test plan

  • SSH to a server with standard /home/user path — should still work
  • SSH as root — should navigate to /root
  • SSH to a server with non-standard home dir — should now navigate correctly
  • SFTP auto-open sidebar should open at the correct home directory

🤖 Generated with Claude Code

Query the remote server for the real home directory using two methods:
1. SSH exec `echo ~` — works for any user regardless of home path
2. SFTP realpath('.') — fallback, SFTP cwd is typically home dir

Falls back to the previous hardcoded /home/{username} candidates if
both methods fail. This fixes SFTP auto-open sidebar not navigating
to the correct directory for users with non-standard home paths
(e.g. /usr/home, /export/home, custom paths).

Fixes #458

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ad15d8ceb5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +308 to +309
const statSftp = bridge?.statSftp;
if (statSftp) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve home-path fallback when statSftp is missing

If getSftpHomeDir is unavailable/fails and the bridge does not expose statSftp, this path now skips all candidate checks and leaves startPath at /. Before this change, the else branch still probed /home/{username} and /root via listSftp, so non-root sessions could still auto-open in a home directory. Since statSftp is explicitly optional in global.d.ts, this is a real regression for optional/older bridge implementations and some test/mocked environments.

Useful? React with 👍 / 👎.

Preserve the original fallback behavior for bridges that don't expose
statSftp — probe candidate directories via listSftp instead.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@binaricat

Copy link
Copy Markdown
Owner Author

@codex review

1 similar comment
@binaricat

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3db9b0aa26

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread electron/bridges/sftpBridge.cjs Outdated
const sshClient = client.client;
if (sshClient && typeof sshClient.exec === "function") {
try {
const result = await execSshCommand(sshClient, "echo ~");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Time-box the echo ~ probe before falling back

getSftpHomeDir now awaits execSshCommand(sshClient, "echo ~") before trying SFTP realpath('.'), but this exec path has no timeout and only falls through on exceptions. On hosts where non-interactive shell startup hangs (for example a shell init script that reads stdin or a forced command that never exits), this promise never settles, so SFTP auto-open can block indefinitely instead of reaching the existing fallback logic.

Useful? React with 👍 / 👎.

Prevent indefinite blocking when the remote shell init hangs or a
forced command never exits. Falls through to SFTP realpath after
timeout.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@binaricat

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 34ab6c0e98

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1608 to +1611
const result = await Promise.race([
execSshCommand(sshClient, "echo ~"),
new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), 5000)),
]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Abort timed-out home probe commands

When the 5s timeout wins, Promise.race rejects but the in-flight execSshCommand("echo ~") is never cancelled, so that SSH channel can remain open indefinitely. This is most likely on the same hosts with blocking shell init scripts called out in the comment, and it can consume session slots (MaxSessions) for the lifetime of the connection and cause later channel opens to fail. Close/terminate the exec stream when the timeout path is taken.

Useful? React with 👍 / 👎.

Comment thread electron/bridges/sftpBridge.cjs Outdated
Comment on lines +1625 to +1626
if (absPath) {
return { success: true, homeDir: absPath };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Treat root realpath result as non-authoritative

The realpath('.') fallback is accepted unconditionally, so a returned '/' is treated as the user's home and bypasses the legacy /home/${username} probe. On servers where exec is disabled and SFTP starts in /, this regresses non-root users to opening root instead of their actual home directory. Consider treating '/' as ambiguous for non-root users and continuing to the candidate fallback chain.

Useful? React with 👍 / 👎.

- Close/destroy the SSH exec stream when the 5s timeout fires to
  avoid leaking session slots (MaxSessions).
- Treat SFTP realpath('.') returning '/' as non-authoritative so
  non-root users fall through to the candidate probe chain instead
  of incorrectly opening at root.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@binaricat

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@binaricat
binaricat merged commit 65f6298 into main Mar 23, 2026
@binaricat
binaricat deleted the fix/sftp-home-dir branch April 1, 2026 04:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: sftp 勾选,启用自动打开侧边栏,不会自动进入到对应的home目录

1 participant