Check upload names before writing files
C++ · C++ · intermediate · modification
Until now `put_file` joined the client's name onto the upload root as is, so a name like `../../etc/cron.d/job` could be written outside the root. This PR adds `uploads::resolve_store_path` (`store_path.hpp`, `store_path.cpp`): it checks the name against the naming rules segment by segment, builds the relative path from the accepted segments, resolves the root and the joined path through any symlinks (the root must exist, the joined path need not), compares the two element by element so the result must lie strictly below the root, and then walks the elements below the root with `symlink_status`, up to the first one that does not exist, refusing a symlink among them. `put_file.cpp` calls it first and answers `400` when it returns nothing; the directory creation and the write are as before. `tests/store_path_test.cpp` builds a scratch tree in the temp directory (an `uploads` root with an `uploads2` sibling) and covers accepted names, refused names, symlinks inside the root and `put_file` end to end.
Internal document drop service in C++20, standard library only, built with CMake (build files unchanged; they pick up every `src/**/*.cpp` and every `tests/*_test.cpp`) by Apple clang 17 locally and GCC 13 in CI with `-Wall -Wextra -Wconversion` treated as errors. Runs on Linux (ext4) in production. The HTTP layer (existing, not in the diff) percent-decodes the path after `/files/` and passes it to `put_file` as raw bytes, caps bodies at 64 MiB, and handles requests one at a time on a single worker thread. `UploadConfig::root` is an absolute path of under 200 bytes from the service's config file; it may itself be a symlink, it is created at deploy time, exists while the service runs, and only the service's user can write to it. Besides the directories and files this service writes, the root holds only symlinks that operators create while the service is stopped (for example a `shared` link to a team share outside the root); no other process writes under the root while the service runs. Tests use the repo's existing helper `tests/check.hpp` (unchanged): `TEST(name)` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure without stopping, and `tests/check_main.cpp` runs every registered test and exits non-zero on any failure.
Requirements
- `PUT /files/<name>` (existing `uploads::put_file`) stores the request body as the file `<name>` under the upload root (`UploadConfig::root`), creating missing directories on the way and replacing an existing file, and answers `201` with `stored`.
- Naming rules: a name is 1 to 512 bytes long and is one or more segments separated by single `/` characters; each segment is 1 to 255 bytes of ASCII letters, digits, `.`, `_` and `-`, and does not start with `.`. A name that breaks a rule is answered `400` with `invalid file name` and nothing is created. Names come from clients and can hold any bytes, including NUL, `\` and non-ASCII bytes.
- A file is only ever stored strictly inside the upload root, after following symlinks. A name whose place is the root itself or outside it, including a name that goes through a symlink pointing outside the root or through a symlink whose target does not exist or cannot be resolved (for example a loop), is answered `400` with `invalid file name` and nothing is created anywhere. A symlink inside the root that points to a place inside it is followed, and the file is stored at the place it points to.
- Unchanged: a name that passes the checks but cannot be stored (a file or a directory already sits where a directory or the file has to go, or the write fails) is answered `500` with `could not store file`; a failed write can leave a partly written file, which is accepted.
- Unchanged: `UploadConfig`, `HttpResponse`, `src/uploads/put_file.hpp` and the HTTP layer.
Files touched
- src/uploads/store_path.hpp
- src/uploads/store_path.cpp
- src/uploads/put_file.cpp
- tests/store_path_test.cpp