The vulnerability lies in the improper handling of dynamic group paths in SFTPGo, specifically when creating a user's home directory from a group template with placeholders. The analysis of the patches between versions 2.7.0 and 2.7.1 revealed two key weaknesses that, when combined, lead to a path traversal vulnerability.
First, the dataprovider.validateBaseParams function failed to adequately sanitize usernames. This allowed the creation of users with malicious usernames containing relative path components like ../.
Second, the dataprovider.User.mergeWithPrimaryGroup function, which constructs the user's home directory by replacing placeholders such as %username% in a group's home directory template, did not clean the resulting path.
An attacker could exploit this by creating a user with a crafted username (e.g., ../attacker). When this user is assigned to a group with a dynamic home directory (e.g., /home/%username%), the mergeWithPrimaryGroup function would generate the path /home/../attacker, which resolves to /attacker, a directory outside of the intended parent. This allows the attacker to set their home directory to an arbitrary location on the filesystem that the SFTPGo process has write access to.
The patch addresses these issues by:
- Introducing strict username validation in
dataprovider.validateBaseParams via the new util.IsNameValid function.
- Adding a call to
filepath.Clean() in dataprovider.User.mergeWithPrimaryGroup to sanitize the final home directory path, removing any traversal sequences.