Explaining SFTP RealPath
If you are an application developer, you are likely already familiar with local path manipulation functions, like path.join() in Node.js, os.path.join() in Python, or Path.Combine() in C#.
In the SFTP protocol, the realpath message (and its compose path feature) is basically the remote server’s version of those functions. It allows your application to ask the remote server to safely build, clean up, and resolve file paths for you.
Here is a conceptual breakdown of why this exists and how it works.
The Core Concept: What is realpath?
When you interact with a remote server, you don't always know exactly where you are, or you might be dealing with "messy" paths that include shortcuts like . (current directory), .. (parent directory), or symbolic links.
If your app sends a realpath request to the server, you are asking: "Take this messy or relative path, and tell me the clean, absolute path on your hard drive."
The Problem: Why do we need "Compose Path"?
Imagine your app is running on a Windows machine (which uses backslashes ), but you are connecting to a Linux server (which uses forward slashes /).
Your app knows the user's current remote folder is /home/appuser. The user clicks on a folder named invoices in your UI. If your app tries to blindly combine those strings itself, it might accidentally generate /home/appuser\invoices (mixing slashes) or /home/appuserinvoices (forgetting a slash). The remote server will reject this.
The "Compose Path" feature solves this. Instead of your application trying to guess the remote server's operating system rules, you hand the pieces to the server and say: "Start at this base path, append this next folder to it, and give me the final result."
Examples: Inputs and Outputs
Here is how your application interacts with the server using these concepts.
Example 1: Finding your current location (Standard realpath)
When your app first logs in, it needs to know what folder the server put it in. Your app asks the server to resolve the standard shortcut for "current directory."
- Input (Base Path):
. - Output (Result):
/var/www/html
Example 2: Safely joining paths (Using compose path)
Your app knows it is in /var/www/html. You want to open a file named config.json inside the settings folder. Instead of guessing the slashes, you let the server compose it.
- Input (Base Path):
/var/www/html - Input (Compose Path):
settings/config.json - Output (Result):
/var/www/html/settings/config.json - Note: The server automatically injected the correct
/between the two pieces.
Example 3: Navigating up a directory (Using compose path)
Your app is currently working in /home/user/documents/2026, but you want to move the user to the backups folder located one level up. You can pass the "go up" shortcut (..) as the compose path.
- Input (Base Path):
/home/user/documents/2026 - Input (Compose Path):
../backups - Output (Result):
/home/user/documents/backups - Note: The server did the math, stripped away the
2026folder, and appendedbackups.
Example 4: Overriding the base path
If your app accidentally passes an absolute path into the "compose" argument, the server is smart enough to realize that the base path doesn't matter anymore.
- Input (Base Path):
/home/user/documents - Input (Compose Path):
/etc/ssh(Wait, this is an absolute path!) - Output (Result):
/etc/ssh - Note: The server discards the base path entirely because the compose path was already absolute.
Summary for Developers
As an app developer, using the SFTP realpath and compose path features means you never have to write logic to parse or format remote file paths. You simply hand the raw folders or shortcuts to the SFTP server, and rely on the server to safely join them together according to its own operating system rules.