/

CVE-2026-40876

Severity:

HIGH RISK

(8.8)

Path Traversal in goshs SFTP Jail Enforcement

CVE-2026-40876

Analyzed

Vulnerability type

Category:

Path Traversal / Directory Traversal

CWE:

CWE-22

Impact:

Jail Break and Unauthorized File System Access

Severity:

HIGH RISK

(8.8)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

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.go
  • DefaultHandler.GetHandler() in sftpserver/handler.go
  • readFile, writeFile, listFile, cmdFile sinks
  • Why exploitation is possible: sanitizePath() in sftpserver/helper.go validates 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.Clean and 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.

Sources

SourceLink
github.comgithub.com/patrickhener/goshs/security/advisories/GHSA-5h6h-7rc9-3824

Frequently asked

questions

Yes, but only through the SFTP interface and only by authenticated SFTP users. A remote attacker still needs valid credentials or a compromised SFTP account to exploit the flaw.

The attacker must be able to log in to goshs via SFTP. Once authenticated, they can use crafted paths to read from and write to files outside the configured SFTP root, within the limits of the goshs process's OS-level permissions.

The safest pattern is to normalize and resolve all requested paths to their canonical form and then enforce that the resulting path is a strict subdirectory of the configured root. Directory-aware comparisons should be used instead of simple string-prefix checks, and all file operations should consistently pass through this hardened validation.

Future implementations should rely on well-tested filesystem utilities for path resolution, centralize path validation logic, and include security-focused code review around any jail or sandbox mechanism. Incorporating static analysis checks for improper path validation and enforcing secure coding guidelines for file access helps catch similar bugs before release.

Find out what's already
exploitable in your codebase.