This repository has been archived by the owner on Dec 14, 2024. It is now read-only.
forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnode_modules.cc
453 lines (397 loc) · 15 KB
/
node_modules.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include "node_modules.h"
#include <cstdio>
#include "base_object-inl.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_url.h"
#include "permission/permission.h"
#include "permission/permission_base.h"
#include "util-inl.h"
#include "v8-fast-api-calls.h"
#include "v8-function-callback.h"
#include "v8-primitive.h"
#include "v8-value.h"
#include "v8.h"
#include "simdjson.h"
namespace node {
namespace modules {
using v8::Array;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::NewStringType;
using v8::Object;
using v8::ObjectTemplate;
using v8::Primitive;
using v8::String;
using v8::Undefined;
using v8::Value;
#ifdef __POSIX__
constexpr char kPathSeparator = '/';
constexpr std::string_view kNodeModules = "/node_modules";
#else
constexpr char kPathSeparator = '\\';
constexpr std::string_view kNodeModules = "\\node_modules";
#endif
void BindingData::MemoryInfo(MemoryTracker* tracker) const {
// Do nothing
}
BindingData::BindingData(Realm* realm,
v8::Local<v8::Object> object,
InternalFieldInfo* info)
: SnapshotableObject(realm, object, type_int) {}
bool BindingData::PrepareForSerialization(v8::Local<v8::Context> context,
v8::SnapshotCreator* creator) {
// Return true because we need to maintain the reference to the binding from
// JS land.
return true;
}
InternalFieldInfoBase* BindingData::Serialize(int index) {
DCHECK_IS_SNAPSHOT_SLOT(index);
InternalFieldInfo* info =
InternalFieldInfoBase::New<InternalFieldInfo>(type());
return info;
}
void BindingData::Deserialize(v8::Local<v8::Context> context,
v8::Local<v8::Object> holder,
int index,
InternalFieldInfoBase* info) {
DCHECK_IS_SNAPSHOT_SLOT(index);
HandleScope scope(context->GetIsolate());
Realm* realm = Realm::GetCurrent(context);
BindingData* binding = realm->AddBindingData<BindingData>(holder);
CHECK_NOT_NULL(binding);
}
Local<Array> BindingData::PackageConfig::Serialize(Realm* realm) const {
auto has_manifest = !realm->env()->options()->experimental_policy.empty();
auto isolate = realm->isolate();
const auto ToString = [isolate](std::string_view input) -> Local<Primitive> {
return String::NewFromUtf8(
isolate, input.data(), NewStringType::kNormal, input.size())
.ToLocalChecked();
};
Local<Value> values[7] = {
name.has_value() ? ToString(*name) : Undefined(isolate),
main.has_value() ? ToString(*main) : Undefined(isolate),
ToString(type),
imports.has_value() ? ToString(*imports) : Undefined(isolate),
exports.has_value() ? ToString(*exports) : Undefined(isolate),
has_manifest ? ToString(raw_json) : Undefined(isolate),
ToString(file_path),
};
return Array::New(isolate, values, 7);
}
const BindingData::PackageConfig* BindingData::GetPackageJSON(
Realm* realm, std::string_view path, ErrorContext* error_context) {
auto binding_data = realm->GetBindingData<BindingData>();
auto cache_entry = binding_data->package_configs_.find(path.data());
if (cache_entry != binding_data->package_configs_.end()) {
return &cache_entry->second;
}
PackageConfig package_config{};
package_config.file_path = path;
// No need to exclude BOM since simdjson will skip it.
if (ReadFileSync(&package_config.raw_json, path.data()) < 0) {
return nullptr;
}
simdjson::ondemand::document document;
simdjson::ondemand::object main_object;
simdjson::error_code error =
binding_data->json_parser.iterate(package_config.raw_json).get(document);
const auto throw_invalid_package_config = [error_context, path, realm]() {
if (error_context == nullptr) {
THROW_ERR_INVALID_PACKAGE_CONFIG(
realm->isolate(), "Invalid package config %s.", path.data());
} else if (error_context->base.has_value()) {
auto file_url = ada::parse(error_context->base.value());
CHECK(file_url);
auto file_path = url::FileURLToPath(realm->env(), *file_url);
CHECK(file_path.has_value());
THROW_ERR_INVALID_PACKAGE_CONFIG(
realm->isolate(),
"Invalid package config %s while importing \"%s\" from %s.",
path.data(),
error_context->specifier.c_str(),
file_path->c_str());
} else {
THROW_ERR_INVALID_PACKAGE_CONFIG(
realm->isolate(), "Invalid package config %s.", path.data());
}
return nullptr;
};
if (error || document.get_object().get(main_object)) {
return throw_invalid_package_config();
}
simdjson::ondemand::raw_json_string key;
simdjson::ondemand::value value;
std::string_view field_value;
simdjson::ondemand::json_type field_type;
for (auto field : main_object) {
// Throw error if getting key or value fails.
if (field.key().get(key) || field.value().get(value)) {
return throw_invalid_package_config();
}
// based on coverity using key with == derefs the raw value
// avoid derefing if its null
if (key.raw() == nullptr) continue;
if (key == "name") {
// Though there is a key "name" with a corresponding value,
// the value may not be a string or could be an invalid JSON string
if (value.get_string(package_config.name)) {
return throw_invalid_package_config();
}
} else if (key == "main") {
// Omit all non-string values
USE(value.get_string(package_config.main));
} else if (key == "exports") {
if (value.type().get(field_type)) {
return throw_invalid_package_config();
}
switch (field_type) {
case simdjson::ondemand::json_type::object:
case simdjson::ondemand::json_type::array: {
if (value.raw_json().get(field_value)) {
return throw_invalid_package_config();
}
package_config.exports = field_value;
break;
}
case simdjson::ondemand::json_type::string: {
if (value.get_string(package_config.exports)) {
return throw_invalid_package_config();
}
break;
}
default:
break;
}
} else if (key == "imports") {
if (value.type().get(field_type)) {
return throw_invalid_package_config();
}
switch (field_type) {
case simdjson::ondemand::json_type::array:
case simdjson::ondemand::json_type::object: {
if (value.raw_json().get(field_value)) {
return throw_invalid_package_config();
}
package_config.imports = field_value;
break;
}
case simdjson::ondemand::json_type::string: {
if (value.get_string(package_config.imports)) {
return throw_invalid_package_config();
}
break;
}
default:
break;
}
} else if (key == "type") {
if (value.get_string().get(field_value)) {
return throw_invalid_package_config();
}
// Only update type if it is "commonjs" or "module"
// The default value is "none" for backward compatibility.
if (field_value == "commonjs" || field_value == "module") {
package_config.type = field_value;
}
}
}
// package_config could be quite large, so we should move it instead of
// copying it.
auto cached = binding_data->package_configs_.insert(
{std::string(path), std::move(package_config)});
return &cached.first->second;
}
void BindingData::ReadPackageJSON(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 1); // path, [is_esm, base, specifier]
CHECK(args[0]->IsString()); // path
Realm* realm = Realm::GetCurrent(args);
auto isolate = realm->isolate();
Utf8Value path(isolate, args[0]);
bool is_esm = args[1]->IsTrue();
auto error_context = ErrorContext();
if (is_esm) {
CHECK(args[2]->IsUndefined() || args[2]->IsString()); // base
CHECK(args[3]->IsString()); // specifier
if (args[2]->IsString()) {
Utf8Value base_value(isolate, args[2]);
error_context.base = base_value.ToString();
}
Utf8Value specifier(isolate, args[3]);
error_context.specifier = specifier.ToString();
}
THROW_IF_INSUFFICIENT_PERMISSIONS(
realm->env(),
permission::PermissionScope::kFileSystemRead,
path.ToStringView());
auto package_json =
GetPackageJSON(realm, path.ToString(), is_esm ? &error_context : nullptr);
if (package_json == nullptr) {
return;
}
args.GetReturnValue().Set(package_json->Serialize(realm));
}
const BindingData::PackageConfig* BindingData::TraverseParent(
Realm* realm, std::string_view check_path) {
auto env = realm->env();
auto root_separator_index = check_path.find_first_of(kPathSeparator);
size_t separator_index = 0;
const bool is_permissions_enabled = env->permission()->enabled();
do {
separator_index = check_path.find_last_of(kPathSeparator);
check_path = check_path.substr(0, separator_index);
// We don't need to try "/"
if (check_path.empty()) {
break;
}
// Stop the search when the process doesn't have permissions
// to walk upwards
if (UNLIKELY(is_permissions_enabled &&
!env->permission()->is_granted(
permission::PermissionScope::kFileSystemRead,
std::string(check_path) + kPathSeparator))) {
return nullptr;
}
// Check if the path ends with `/node_modules`
if (check_path.size() >= kNodeModules.size() &&
std::equal(check_path.end() - kNodeModules.size(),
check_path.end(),
kNodeModules.begin())) {
return nullptr;
}
auto package_json = GetPackageJSON(
realm,
std::string(check_path) + kPathSeparator + "package.json",
nullptr);
if (package_json != nullptr) {
return package_json;
}
} while (separator_index > root_separator_index);
return nullptr;
}
void BindingData::GetNearestParentPackageJSON(
const v8::FunctionCallbackInfo<v8::Value>& args) {
CHECK_GE(args.Length(), 1);
CHECK(args[0]->IsString());
Realm* realm = Realm::GetCurrent(args);
Utf8Value path_value(realm->isolate(), args[0]);
auto package_json = TraverseParent(realm, path_value.ToStringView());
if (package_json != nullptr) {
args.GetReturnValue().Set(package_json->Serialize(realm));
}
}
void BindingData::GetNearestParentPackageJSONType(
const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 1);
CHECK(args[0]->IsString());
Realm* realm = Realm::GetCurrent(args);
Utf8Value path(realm->isolate(), args[0]);
auto package_json = TraverseParent(realm, path.ToStringView());
if (package_json == nullptr) {
return;
}
Local<Value> values[3] = {
ToV8Value(realm->context(), package_json->type).ToLocalChecked(),
ToV8Value(realm->context(), package_json->file_path).ToLocalChecked(),
ToV8Value(realm->context(), package_json->raw_json).ToLocalChecked()};
args.GetReturnValue().Set(Array::New(realm->isolate(), values, 3));
}
void BindingData::GetPackageScopeConfig(
const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 1);
CHECK(args[0]->IsString());
Realm* realm = Realm::GetCurrent(args);
Utf8Value resolved(realm->isolate(), args[0]);
auto package_json_url_base = ada::parse(resolved.ToStringView());
if (!package_json_url_base) {
url::ThrowInvalidURL(realm->env(), resolved.ToStringView(), std::nullopt);
return;
}
auto package_json_url =
ada::parse("./package.json", &package_json_url_base.value());
if (!package_json_url) {
url::ThrowInvalidURL(realm->env(), "./package.json", resolved.ToString());
return;
}
std::string_view node_modules_package_path = "/node_modules/package.json";
auto error_context = ErrorContext();
error_context.is_esm = true;
// TODO(@anonrig): Rewrite this function and avoid calling URL parser.
while (true) {
auto pathname = package_json_url->get_pathname();
if (pathname.size() >= node_modules_package_path.size() &&
pathname.compare(pathname.size() - node_modules_package_path.size(),
node_modules_package_path.size(),
node_modules_package_path) == 0) {
break;
}
auto file_url = url::FileURLToPath(realm->env(), *package_json_url);
CHECK(file_url);
error_context.specifier = resolved.ToString();
auto package_json = GetPackageJSON(realm, *file_url, &error_context);
if (package_json != nullptr) {
return args.GetReturnValue().Set(package_json->Serialize(realm));
}
auto last_href = std::string(package_json_url->get_href());
auto last_pathname = std::string(package_json_url->get_pathname());
package_json_url = ada::parse("../package.json", &package_json_url.value());
if (!package_json_url) {
url::ThrowInvalidURL(realm->env(), "../package.json", last_href);
return;
}
// Terminates at root where ../package.json equals ../../package.json
// (can't just check "/package.json" for Windows support).
if (package_json_url->get_pathname() == last_pathname) {
break;
}
}
auto package_json_url_as_path =
url::FileURLToPath(realm->env(), *package_json_url);
CHECK(package_json_url_as_path);
return args.GetReturnValue().Set(
String::NewFromUtf8(realm->isolate(),
package_json_url_as_path->c_str(),
NewStringType::kNormal,
package_json_url_as_path->size())
.ToLocalChecked());
}
void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
Local<ObjectTemplate> target) {
Isolate* isolate = isolate_data->isolate();
SetMethod(isolate, target, "readPackageJSON", ReadPackageJSON);
SetMethod(isolate,
target,
"getNearestParentPackageJSONType",
GetNearestParentPackageJSONType);
SetMethod(isolate,
target,
"getNearestParentPackageJSON",
GetNearestParentPackageJSON);
SetMethod(isolate, target, "getPackageScopeConfig", GetPackageScopeConfig);
}
void BindingData::CreatePerContextProperties(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Realm* realm = Realm::GetCurrent(context);
realm->AddBindingData<BindingData>(target);
}
void BindingData::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(ReadPackageJSON);
registry->Register(GetNearestParentPackageJSONType);
registry->Register(GetNearestParentPackageJSON);
registry->Register(GetPackageScopeConfig);
}
} // namespace modules
} // namespace node
NODE_BINDING_CONTEXT_AWARE_INTERNAL(
modules, node::modules::BindingData::CreatePerContextProperties)
NODE_BINDING_PER_ISOLATE_INIT(
modules, node::modules::BindingData::CreatePerIsolateProperties)
NODE_BINDING_EXTERNAL_REFERENCE(
modules, node::modules::BindingData::RegisterExternalReferences)