55 size_t max_bytes = 8 * 1024 * 1024,
ModuleSources overrides = {},
56 std::vector<std::string> approved_roots = {}) {
57 std::vector<std::filesystem::path>
roots;
58 for (
const auto&
root : approved_roots) {
61 return [max_bytes, overrides = std::move(overrides),
63 std::string_view reference,
64 std::string_view importer) -> absl::StatusOr<ModuleSource> {
65 if (
roots.empty() && overrides.empty()) {
66 return absl::PermissionDeniedError(
67 "Source imports are disallowed in the current runtime: no source "
68 "roots are approved.");
70 if (reference.find(
"://") != std::string_view::npos ||
71 reference.find(
'\0') != std::string_view::npos) {
72 return absl::PermissionDeniedError(
73 "Source imports require a local path.");
75 std::filesystem::path
path(reference);
76 if (
path.is_relative()) {
77 if (importer.empty() || importer ==
"-" || importer.front() ==
'<') {
78 return absl::PermissionDeniedError(
79 "A relative source import requires a document path.");
81 path = std::filesystem::path(importer).parent_path() /
path;
84 if (
const auto found = overrides.find(
path.generic_string());
85 found != overrides.end()) {
86 if (found->second.size() > max_bytes) {
87 return absl::ResourceExhaustedError(
88 "Imported source exceeds the byte limit.");
90 return ModuleSource{found->first, found->second};
93 return absl::PermissionDeniedError(
94 "Source imports are disallowed in the current runtime: "
95 "no source roots are approved.");
97 std::filesystem::path approved;
98 std::filesystem::path relative;
100 const auto candidate =
path.lexically_relative(
root);
101 if (candidate.empty() || candidate.is_absolute()) {
104 bool contained =
true;
105 for (
const auto& component : candidate) {
106 if (component ==
"..") {
112 relative = candidate;
116 if (approved.empty()) {
117 return absl::PermissionDeniedError(
118 "Source import is outside the runtime's approved source roots.");
120#if defined(__unix__) || defined(__APPLE__)
123 explicit Descriptor(
int value) : value_(
value) {}
128 Descriptor(
const Descriptor&) =
delete;
129 Descriptor& operator=(
const Descriptor&) =
delete;
130 int get()
const {
return value_; }
131 void Reset(
int value) {
140 Descriptor directory(::open(
"/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
141 for (
const auto& component : approved.relative_path()) {
142 if (component ==
".")
145 ::openat(directory.get(), component.c_str(),
146 O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
148 return absl::PermissionDeniedError(
149 "Cannot open an approved source root without traversing symlinks.");
151 directory.Reset(next);
153 std::vector<std::filesystem::path> components;
154 for (
const auto& component : relative) {
155 if (component !=
".")
156 components.push_back(component);
158 if (components.empty()) {
159 return absl::InvalidArgumentError(
"An imported source must be a file.");
161 for (
size_t index = 0; index + 1 < components.size(); ++index) {
163 ::openat(directory.get(), components[index].c_str(),
164 O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
166 return absl::PermissionDeniedError(
167 "Cannot resolve imported source within approved roots; "
168 "symlink traversal is disallowed.");
170 directory.Reset(next);
172 Descriptor file(::openat(directory.get(), components.back().c_str(),
173 O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK));
174 if (file.get() < 0) {
175 return absl::PermissionDeniedError(
176 "Cannot open imported source within approved roots; "
177 "symlink traversal is disallowed.");
181 return absl::InvalidArgumentError(
182 "An imported source must be a regular file.");
185 static_cast<uint64_t
>(
metadata.st_size) > max_bytes) {
186 return absl::ResourceExhaustedError(
187 "Imported source exceeds the byte limit.");
192 const auto count = ::read(file.get(), buffer,
sizeof(buffer));
198 return absl::DataLossError(
"Cannot read imported source.");
200 const auto size =
static_cast<size_t>(count);
201 if (size > max_bytes - source.size()) {
202 return absl::ResourceExhaustedError(
203 "Imported source exceeds the byte limit.");
205 source.append(buffer, size);
207 return ModuleSource{
path.generic_string(), std::move(source)};
209 return absl::UnimplementedError(
210 "This runtime has no confined filesystem source resolver.");
std::vector< std::string > roots
Definition main.cc:137
ModuleResolver FileModuleResolver(size_t max_bytes=8 *1024 *1024, ModuleSources overrides={}, std::vector< std::string > approved_roots={})
Filesystem imports confined to explicitly approved roots without symlinks.
Definition file_modules.h:54