forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreal.js
More file actions
492 lines (430 loc) · 13.9 KB
/
real.js
File metadata and controls
492 lines (430 loc) · 13.9 KB
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
'use strict';
const {
ObjectDefineProperty,
Promise,
StringPrototypeStartsWith,
} = primordials;
const fs = require('fs');
const path = require('path');
const { VirtualProvider } = require('internal/vfs/provider');
const { VirtualFileHandle } = require('internal/vfs/file_handle');
const {
codes: {
ERR_INVALID_ARG_VALUE,
},
} = require('internal/errors');
const {
createEACCES,
createEBADF,
createENOENT,
} = require('internal/vfs/errors');
/**
* A file handle that wraps a real file descriptor.
*/
class RealFileHandle extends VirtualFileHandle {
#fd;
#realPath;
#checkClosed(syscall) {
if (this.closed) {
throw createEBADF(syscall);
}
}
/**
* @param {string} path The VFS path
* @param {string} flags The open flags
* @param {number} mode The file mode
* @param {number} fd The real file descriptor
* @param {string} realPath The real filesystem path
*/
constructor(path, flags, mode, fd, realPath) {
super(path, flags, mode);
this.#fd = fd;
this.#realPath = realPath;
}
/**
* Gets the real file descriptor.
* @returns {number}
*/
get fd() {
return this.#fd;
}
readSync(buffer, offset, length, position) {
this.#checkClosed('read');
return fs.readSync(this.#fd, buffer, offset, length, position);
}
async read(buffer, offset, length, position) {
this.#checkClosed('read');
return new Promise((resolve, reject) => {
fs.read(this.#fd, buffer, offset, length, position, (err, bytesRead) => {
if (err) reject(err);
else resolve({ __proto__: null, bytesRead, buffer });
});
});
}
writeSync(buffer, offset, length, position) {
this.#checkClosed('write');
return fs.writeSync(this.#fd, buffer, offset, length, position);
}
async write(buffer, offset, length, position) {
this.#checkClosed('write');
return new Promise((resolve, reject) => {
fs.write(this.#fd, buffer, offset, length, position, (err, bytesWritten) => {
if (err) reject(err);
else resolve({ __proto__: null, bytesWritten, buffer });
});
});
}
readFileSync(options) {
this.#checkClosed('read');
return fs.readFileSync(this.#realPath, options);
}
async readFile(options) {
this.#checkClosed('read');
return fs.promises.readFile(this.#realPath, options);
}
writeFileSync(data, options) {
this.#checkClosed('write');
fs.writeFileSync(this.#realPath, data, options);
}
async writeFile(data, options) {
this.#checkClosed('write');
return fs.promises.writeFile(this.#realPath, data, options);
}
statSync(options) {
this.#checkClosed('fstat');
return fs.fstatSync(this.#fd, options);
}
async stat(options) {
this.#checkClosed('fstat');
return new Promise((resolve, reject) => {
fs.fstat(this.#fd, options, (err, stats) => {
if (err) reject(err);
else resolve(stats);
});
});
}
truncateSync(len = 0) {
this.#checkClosed('ftruncate');
fs.ftruncateSync(this.#fd, len);
}
async truncate(len = 0) {
this.#checkClosed('ftruncate');
return new Promise((resolve, reject) => {
fs.ftruncate(this.#fd, len, (err) => {
if (err) reject(err);
else resolve();
});
});
}
closeSync() {
if (!this.closed) {
fs.closeSync(this.#fd);
super.closeSync();
}
}
async close() {
if (!this.closed) {
return new Promise((resolve, reject) => {
fs.close(this.#fd, (err) => {
if (err) reject(err);
else {
super.closeSync();
resolve();
}
});
});
}
}
}
/**
* A provider that wraps a real filesystem directory.
* Allows mounting a real directory at a different VFS path.
*/
class RealFSProvider extends VirtualProvider {
#rootPath;
/**
* @param {string} rootPath The real filesystem path to use as root
*/
constructor(rootPath) {
super();
if (typeof rootPath !== 'string' || rootPath === '') {
throw new ERR_INVALID_ARG_VALUE('rootPath', rootPath, 'must be a non-empty string');
}
// Resolve to absolute path and normalize
this.#rootPath = path.resolve(rootPath);
ObjectDefineProperty(this, 'readonly', { __proto__: null, value: false });
ObjectDefineProperty(this, 'supportsSymlinks', { __proto__: null, value: true });
}
/**
* Gets the root path of this provider.
* @returns {string}
*/
get rootPath() {
return this.#rootPath;
}
/**
* Resolves a VFS path to a real filesystem path.
* Ensures the path doesn't escape the root directory.
* @param {string} vfsPath The VFS path (relative to provider root)
* @returns {string} The real filesystem path
* @private
*/
#resolvePath(vfsPath, followSymlinks = true) {
// Normalize the VFS path (remove leading slash, handle . and ..)
let normalized = vfsPath;
if (normalized.startsWith('/')) {
normalized = normalized.slice(1);
}
// Join with root and resolve
const realPath = path.resolve(this.#rootPath, normalized);
// Security check: ensure the resolved path is within rootPath
const rootWithSep = this.#rootPath.endsWith(path.sep) ?
this.#rootPath :
this.#rootPath + path.sep;
if (realPath !== this.#rootPath && !StringPrototypeStartsWith(realPath, rootWithSep)) {
throw createENOENT('open', vfsPath);
}
// Resolve symlinks to prevent escape via symbolic links
if (followSymlinks) {
try {
const resolved = fs.realpathSync(realPath);
if (resolved !== this.#rootPath &&
!StringPrototypeStartsWith(resolved, rootWithSep)) {
throw createENOENT('open', vfsPath);
}
return resolved;
} catch (err) {
if (err?.code !== 'ENOENT') throw err;
// Path doesn't exist yet - verify deepest existing ancestor
this.#verifyAncestorInRoot(realPath, rootWithSep, vfsPath);
return realPath;
}
}
// For lstat/readlink (no final symlink follow), check parent only
this.#verifyAncestorInRoot(realPath, rootWithSep, vfsPath);
return realPath;
}
/**
* Verifies that the deepest existing ancestor of a path is within rootPath.
* @param {string} realPath The real filesystem path
* @param {string} rootWithSep The rootPath with trailing separator
* @param {string} vfsPath The original VFS path (for error messages)
*/
#verifyAncestorInRoot(realPath, rootWithSep, vfsPath) {
let current = path.dirname(realPath);
while (current.length >= this.#rootPath.length) {
try {
const resolved = fs.realpathSync(current);
if (resolved !== this.#rootPath &&
!StringPrototypeStartsWith(resolved, rootWithSep)) {
throw createENOENT('open', vfsPath);
}
return;
} catch (err) {
if (err?.code !== 'ENOENT') throw err;
current = path.dirname(current);
}
}
}
openSync(vfsPath, flags, mode) {
const realPath = this.#resolvePath(vfsPath);
const fd = fs.openSync(realPath, flags, mode);
return new RealFileHandle(vfsPath, flags, mode ?? 0o644, fd, realPath);
}
async open(vfsPath, flags, mode) {
const realPath = this.#resolvePath(vfsPath);
return new Promise((resolve, reject) => {
fs.open(realPath, flags, mode, (err, fd) => {
if (err) reject(err);
else resolve(new RealFileHandle(vfsPath, flags, mode ?? 0o644, fd, realPath));
});
});
}
statSync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.statSync(realPath, options);
}
async stat(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.stat(realPath, options);
}
lstatSync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath, false);
return fs.lstatSync(realPath, options);
}
async lstat(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath, false);
return fs.promises.lstat(realPath, options);
}
readdirSync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.readdirSync(realPath, options);
}
async readdir(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.readdir(realPath, options);
}
mkdirSync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.mkdirSync(realPath, options);
}
async mkdir(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.mkdir(realPath, options);
}
rmdirSync(vfsPath) {
const realPath = this.#resolvePath(vfsPath);
fs.rmdirSync(realPath);
}
async rmdir(vfsPath) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.rmdir(realPath);
}
unlinkSync(vfsPath) {
const realPath = this.#resolvePath(vfsPath);
fs.unlinkSync(realPath);
}
async unlink(vfsPath) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.unlink(realPath);
}
renameSync(oldVfsPath, newVfsPath) {
const oldRealPath = this.#resolvePath(oldVfsPath);
const newRealPath = this.#resolvePath(newVfsPath);
fs.renameSync(oldRealPath, newRealPath);
}
async rename(oldVfsPath, newVfsPath) {
const oldRealPath = this.#resolvePath(oldVfsPath);
const newRealPath = this.#resolvePath(newVfsPath);
return fs.promises.rename(oldRealPath, newRealPath);
}
readlinkSync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath, false);
const target = fs.readlinkSync(realPath, options);
// Translate absolute targets within rootPath to VFS-relative
if (path.isAbsolute(target)) {
const rootWithSep = this.#rootPath + path.sep;
if (target === this.#rootPath) {
return '/';
}
if (StringPrototypeStartsWith(target, rootWithSep)) {
return '/' + target.slice(rootWithSep.length).replace(/\\/g, '/');
}
}
return target;
}
async readlink(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath, false);
const target = await fs.promises.readlink(realPath, options);
// Translate absolute targets within rootPath to VFS-relative
if (path.isAbsolute(target)) {
const rootWithSep = this.#rootPath + path.sep;
if (target === this.#rootPath) {
return '/';
}
if (StringPrototypeStartsWith(target, rootWithSep)) {
return '/' + target.slice(rootWithSep.length).replace(/\\/g, '/');
}
}
return target;
}
symlinkSync(target, vfsPath, type) {
// Validate target resolves within rootPath
if (path.isAbsolute(target)) {
throw createEACCES('symlink', vfsPath);
}
const realPath = this.#resolvePath(vfsPath);
const resolvedTarget = path.resolve(path.dirname(realPath), target);
const rootWithSep = this.#rootPath.endsWith(path.sep) ?
this.#rootPath : this.#rootPath + path.sep;
if (resolvedTarget !== this.#rootPath &&
!StringPrototypeStartsWith(resolvedTarget, rootWithSep)) {
throw createEACCES('symlink', vfsPath);
}
fs.symlinkSync(target, realPath, type);
}
async symlink(target, vfsPath, type) {
// Validate target resolves within rootPath
if (path.isAbsolute(target)) {
throw createEACCES('symlink', vfsPath);
}
const realPath = this.#resolvePath(vfsPath);
const resolvedTarget = path.resolve(path.dirname(realPath), target);
const rootWithSep = this.#rootPath.endsWith(path.sep) ?
this.#rootPath : this.#rootPath + path.sep;
if (resolvedTarget !== this.#rootPath &&
!StringPrototypeStartsWith(resolvedTarget, rootWithSep)) {
throw createEACCES('symlink', vfsPath);
}
return fs.promises.symlink(target, realPath, type);
}
realpathSync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
const resolved = fs.realpathSync(realPath, options);
// Convert back to VFS path
if (resolved === this.#rootPath) {
return '/';
}
const rootWithSep = this.#rootPath + path.sep;
if (StringPrototypeStartsWith(resolved, rootWithSep)) {
return '/' + resolved.slice(rootWithSep.length).replace(/\\/g, '/');
}
// Path escaped root via symlink — deny access
throw createEACCES('realpath', vfsPath);
}
async realpath(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
const resolved = await fs.promises.realpath(realPath, options);
// Convert back to VFS path
if (resolved === this.#rootPath) {
return '/';
}
const rootWithSep = this.#rootPath + path.sep;
if (StringPrototypeStartsWith(resolved, rootWithSep)) {
return '/' + resolved.slice(rootWithSep.length).replace(/\\/g, '/');
}
// Path escaped root via symlink — deny access
throw createEACCES('realpath', vfsPath);
}
accessSync(vfsPath, mode) {
const realPath = this.#resolvePath(vfsPath);
fs.accessSync(realPath, mode);
}
async access(vfsPath, mode) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.access(realPath, mode);
}
copyFileSync(srcVfsPath, destVfsPath, mode) {
const srcRealPath = this.#resolvePath(srcVfsPath);
const destRealPath = this.#resolvePath(destVfsPath);
fs.copyFileSync(srcRealPath, destRealPath, mode);
}
async copyFile(srcVfsPath, destVfsPath, mode) {
const srcRealPath = this.#resolvePath(srcVfsPath);
const destRealPath = this.#resolvePath(destVfsPath);
return fs.promises.copyFile(srcRealPath, destRealPath, mode);
}
get supportsWatch() {
return true;
}
watch(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.watch(realPath, options);
}
watchAsync(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.promises.watch(realPath, options);
}
watchFile(vfsPath, options) {
const realPath = this.#resolvePath(vfsPath);
return fs.watchFile(realPath, options, () => {});
}
unwatchFile(vfsPath, listener) {
const realPath = this.#resolvePath(vfsPath);
fs.unwatchFile(realPath, listener);
}
}
module.exports = {
RealFSProvider,
RealFileHandle,
};