[Bug]: /api/v2/user/dirs closes the connection when the path is a regular file
Disclaimer: AI-drafted, verified by me before filing. I answer follow-ups.
⚠️ This issue respects the following points: ⚠️
- This is a bug, not a question or a configuration issue.
- This issue is not already reported on Github (I've searched it).
Bug description
GET /api/v2/user/dirs with a path that exists and is a regular file sends no usable response: the connection closes after a single byte. The same endpoint returns 404 correctly for a path that does not exist, so a caller cannot tell this case apart from a network failure and has no status to branch on.
I hit it writing a script that asked whether a path was a file or a directory before deciding how to link to it.
Steps to reproduce
Authenticated as a normal user, local filesystem backend, with a regular file at /file.mp4:
GET /api/v2/user/dirs?path=/→200GET /api/v2/user/dirs?path=/file.mp4→ connection closed,curl: (52) Empty reply from serverGET /api/v2/user/dirs?path=/nope.txt→404GET /api/v2/user/dirs?path=/nope-dir/→404
Only a path that exists and is not a directory triggers it.
Expected behavior
An error status, the way a nonexistent path already returns 404.
SFTPGo version
2.7.5
Data provider
SQLite
Installation method
Other
Configuration
Two httpd bindings; the one used here has enable_rest_api on and is reached over a private network. No customizations relevant to this endpoint.
Relevant log output
{"level":"debug","sender":"HTTP","connection_id":"HTTP_da0ik64uj4pj5qe5mh5g","message":"error retrieving directory entries: readdirent /srv/sftpgo/max/file.mp4: not a directory"}
{"level":"error","sender":"HTTP","connection_id":"HTTP_da0ik64uj4pj5qe5mh5g","message":"generic error: readdirent /srv/sftpgo/max/file.mp4: not a directory"}
{"level":"debug","sender":"httpd","method":"GET","uri":"/api/v2/user/dirs?path=%2Ffile.mp4","resp_status":200,"resp_size":1,"elapsed_ms":0}What are you using SFTPGo for?
Private user, home usecase (home backup/VPS)
Additional info
resp_status: 200, resp_size: 1 suggests the status line and the opening [ are written before the first read of the directory can fail, so the handler's error branch is never reached and the logged error cannot be sent. Installed from nixpkgs.
Source: drakkan/sftpgo