85 lines
2.6 KiB
C++
85 lines
2.6 KiB
C++
// Spec: plans/01-runtime.md 6.7. Jailed disk provider.
|
|||
|
|
#include "disk_provider.h"
|
||
|
|
|
||
|
|
#include <fcntl.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <sys/stat.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
|
||
|
|
namespace molo::runtime::static_ {
|
||
|
|
|
||
|
|
DiskProvider::DiskProvider(std::string root, bool follow_symlinks,
|
||
|
|
bool allow_dotfiles)
|
||
|
|
: root_(std::move(root)),
|
||
|
|
follow_symlinks_(follow_symlinks),
|
||
|
|
allow_dotfiles_(allow_dotfiles) {
|
||
|
|
root_fd_ = ::open(root_.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
||
|
|
}
|
||
|
|
|
||
|
|
DiskProvider::~DiskProvider() {
|
||
|
|
if (root_fd_ >= 0) close(root_fd_);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::optional<Asset> DiskProvider::open(std::string_view rel) {
|
||
|
|
if (root_fd_ < 0) return std::nullopt;
|
||
|
|
std::string path(rel);
|
||
|
|
if (path.empty()) return std::nullopt;
|
||
|
|
if (path.size() > 4096) return std::nullopt;
|
||
|
|
if (path[0] == '/') return std::nullopt;
|
||
|
|
if (path.find('\0') != std::string::npos) return std::nullopt;
|
||
|
|
if (!allow_dotfiles_) {
|
||
|
|
// Any dotfile segment denies the request.
|
||
|
|
std::size_t i = 0;
|
||
|
|
while (i <= path.size()) {
|
||
|
|
std::size_t slash = path.find('/', i);
|
||
|
|
if (slash == std::string::npos) slash = path.size();
|
||
|
|
if (slash > i && path[i] == '.') return std::nullopt;
|
||
|
|
i = slash + 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
int fd = openat(root_fd_, path.c_str(),
|
||
|
|
O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
|
||
|
|
if (fd < 0 && follow_symlinks_) {
|
||
|
|
// Resolve through realpath and verify containment (v1: small TOCTOU
|
||
|
|
// window, documented; strict deployments keep follow_symlinks off).
|
||
|
|
std::string full = root_ + "/" + path;
|
||
|
|
char* resolved = realpath(full.c_str(), nullptr);
|
||
|
|
if (resolved == nullptr) return std::nullopt;
|
||
|
|
std::string r(resolved);
|
||
|
|
free(resolved);
|
||
|
|
char* root_resolved = realpath(root_.c_str(), nullptr);
|
||
|
|
if (root_resolved == nullptr) return std::nullopt;
|
||
|
|
std::string rr(root_resolved);
|
||
|
|
free(root_resolved);
|
||
|
|
if (r != rr && (r.size() <= rr.size() ||
|
||
|
|
r.compare(0, rr.size(), rr) != 0 ||
|
||
|
|
r[rr.size()] != '/')) {
|
||
|
|
return std::nullopt;
|
||
|
|
}
|
||
|
|
fd = ::open(r.c_str(), O_RDONLY | O_CLOEXEC);
|
||
|
|
if (fd < 0) return std::nullopt;
|
||
|
|
}
|
||
|
|
if (fd < 0) return std::nullopt;
|
||
|
|
struct stat st;
|
||
|
|
if (fstat(fd, &st) != 0) {
|
||
|
|
close(fd);
|
||
|
|
return std::nullopt;
|
||
|
|
}
|
||
|
|
AssetMeta meta;
|
||
|
|
meta.mtime = (int64_t)st.st_mtime;
|
||
|
|
if (S_ISDIR(st.st_mode)) {
|
||
|
|
close(fd);
|
||
|
|
meta.is_dir = true;
|
||
|
|
return Asset::ForDir(meta);
|
||
|
|
}
|
||
|
|
if (!S_ISREG(st.st_mode)) {
|
||
|
|
close(fd);
|
||
|
|
return std::nullopt;
|
||
|
|
}
|
||
|
|
meta.length = (uint64_t)st.st_size;
|
||
|
|
meta.is_file = true;
|
||
|
|
return Asset::ForFile(fd, meta);
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::runtime::static_
|