Summary
goshs's SFTP subsystem performs only prefix-based path validation, allowing authenticated SFTP users to escape the configured SFTP root directory.
By crafting paths that share the same string prefix as the root, an attacker can read and write files outside the intended jail, potentially exposing or tampering with unrelated server data.
Affected context
This issue affects deployments of goshs that:
- Enable the SFTP subsystem for authenticated users
- Rely on the configured SFTP root as a hard boundary ("jail") for file access
- Use versions prior to 2.0.0-beta.6 where path validation is implemented using string-prefix checks
The vulnerability manifests when SFTP users interact with paths that start with the same byte sequence as the configured root but are not actually inside the root directory.
Exploit preconditions
- Attack surface: SFTP interface exposed by goshs
- Required access: Valid, authenticated SFTP account on the server
- Trigger point: File operations (read, write, list, or command) routed through:
sftpserver/sftpserver.goDefaultHandler.GetHandler()insftpserver/handler.goreadFile,writeFile,listFile,cmdFilesinks- Why exploitation is possible:
sanitizePath()insftpserver/helper.govalidates paths by raw string-prefix comparison instead of checking real directory boundaries, allowing sibling directories that share the root prefix (e.g.,/tmp/goshsroot_evil) to be treated as if they were under the configured root (e.g.,/tmp/goshsroot).
Why this happens
The root cause is improper restriction of filesystem paths (CWE-22) due to naive string-based validation.
Instead of resolving and normalizing requested paths and confirming they reside within the configured root directory, the code uses a simple prefix comparison on the raw path string returned by sanitizePath().
This approach does not account for directory boundaries, symbolic links, or path normalization issues, so any path that starts with the same byte prefix as the configured root is incorrectly accepted as "inside" the jail even when it points elsewhere on the filesystem.
Potential impact
- Authenticated SFTP users may read sensitive files outside the intended SFTP root
- Attackers could overwrite or modify configuration files, logs, or application data stored outside the jail
- Partial or full compromise of application confidentiality and integrity within the filesystem accessible to the goshs process
- Escalation of impact if external systems trust files stored in the affected directories (e.g., tampering with scripts, data pipelines, or other services that read these files)
General mitigation guidance
Effective mitigation focuses on robust, path-aware enforcement of filesystem boundaries and minimizing trust in user-supplied paths.
Common defensive patterns include:
- Resolving and normalizing paths (e.g., with
filepath.Cleanand absolute path resolution) before validation - Verifying that the canonical path of a requested resource is a strict descendant of the configured root directory, using directory-aware comparisons instead of raw string prefixes
- Avoiding reliance on user-controlled path segments when constructing filesystem paths; centralizing path handling in a single, well-reviewed utility
- Running the SFTP subsystem under a dedicated OS account with least-privilege permissions and constrained filesystem access
- Structuring deployments so that valuable or sensitive files are not colocated with SFTP roots, reducing the blast radius of any boundary failures
How teams detect issues like this
Organizations can detect similar issues through a combination of code-level and operational controls:
- Static analysis: Create rules or queries that flag:
- Direct string-prefix comparisons used for path validation
- Validation logic that does not call canonicalization APIs before comparing paths
- Code review: During review of file-handling code, look for:
- Custom "sanitize path" helpers that use simple substring or prefix checks
- Any logic assuming a path is "inside a directory" without checking directory boundaries
- Policy enforcement: Establish guidelines requiring:
- Use of standardized path normalization utilities
- Reuse of vetted path-guard functions instead of ad hoc checks
- Runtime monitoring: Enable audit logging of SFTP operations and periodically scan for access to unexpected directories or filenames, which may indicate attempted or successful root escapes.

