|
| 1 | +"use strict"; |
| 2 | +// Patches Node's internal FS bindings, right before they would call into C++. |
| 3 | +// See full context in: https://github.com/aspect-build/rules_js/issues/362. |
| 4 | +// This is to ensure ESM imports don't escape accidentally via `realpathSync`. |
| 5 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 | +exports.FsInternalStatPatcher = void 0; |
| 7 | +/// <reference path="./fs_stat_types.d.cts" /> |
| 8 | +const binding_1 = require("internal/test/binding"); |
| 9 | +const utils_1 = require("internal/fs/utils"); |
| 10 | +const fs_cjs_1 = require("./fs.cjs"); |
| 11 | +const internalFs = (0, binding_1.internalBinding)('fs'); |
| 12 | +class FsInternalStatPatcher { |
| 13 | + constructor(escapeFns, guardedReadLink, guardedReadLinkSync, unguardedRealPath, unguardedRealPathSync) { |
| 14 | + this.escapeFns = escapeFns; |
| 15 | + this.guardedReadLink = guardedReadLink; |
| 16 | + this.guardedReadLinkSync = guardedReadLinkSync; |
| 17 | + this.unguardedRealPath = unguardedRealPath; |
| 18 | + this.unguardedRealPathSync = unguardedRealPathSync; |
| 19 | + this._originalFsLstat = internalFs.lstat; |
| 20 | + this.originalSyncRequested = false; |
| 21 | + } |
| 22 | + revert() { |
| 23 | + internalFs.lstat = this._originalFsLstat; |
| 24 | + } |
| 25 | + patch() { |
| 26 | + const statPatcher = this; |
| 27 | + internalFs.lstat = function (path, bigint, reqCallback, throwIfNoEntry) { |
| 28 | + if (this.originalSyncRequested) { |
| 29 | + return statPatcher._originalFsLstat.call(internalFs, path, bigint, reqCallback, throwIfNoEntry); |
| 30 | + } |
| 31 | + if (reqCallback === internalFs.kUsePromises) { |
| 32 | + return statPatcher._originalFsLstat.call(internalFs, path, bigint, reqCallback, throwIfNoEntry).then((stats) => { |
| 33 | + return new Promise((resolve, reject) => { |
| 34 | + statPatcher.eeguardStats(path, bigint, stats, throwIfNoEntry, (err, guardedStats) => { |
| 35 | + err || !guardedStats ? reject(err) : resolve(guardedStats); |
| 36 | + }); |
| 37 | + }); |
| 38 | + }); |
| 39 | + } |
| 40 | + else if (reqCallback === undefined) { |
| 41 | + const stats = statPatcher._originalFsLstat.call(internalFs, path, bigint, undefined, throwIfNoEntry); |
| 42 | + if (!stats) { |
| 43 | + return stats; |
| 44 | + } |
| 45 | + return statPatcher.eeguardStatsSync(path, bigint, throwIfNoEntry, stats); |
| 46 | + } |
| 47 | + else { |
| 48 | + // Just re-use the promise path from above. |
| 49 | + internalFs.lstat(path, bigint, internalFs.kUsePromises, throwIfNoEntry) |
| 50 | + .then((stats) => reqCallback.oncomplete(null, stats)) |
| 51 | + .catch((err) => reqCallback.oncomplete(err)); |
| 52 | + } |
| 53 | + }; |
| 54 | + } |
| 55 | + eeguardStats(path, bigint, stats, throwIfNotFound, cb) { |
| 56 | + const statsObj = (0, utils_1.getStatsFromBinding)(stats); |
| 57 | + if (!statsObj.isSymbolicLink()) { |
| 58 | + // the file is not a symbolic link so there is nothing more to do |
| 59 | + return cb(null, stats); |
| 60 | + } |
| 61 | + path = (0, fs_cjs_1.resolvePathLike)(path); |
| 62 | + if (!this.escapeFns.canEscape(path)) { |
| 63 | + // the file can not escaped the sandbox so there is nothing more to do |
| 64 | + return cb(null, stats); |
| 65 | + } |
| 66 | + return this.guardedReadLink(path, (str) => { |
| 67 | + if (str != path) { |
| 68 | + // there are one or more hops within the guards so there is nothing more to do |
| 69 | + return cb(null, stats); |
| 70 | + } |
| 71 | + // there are no hops so lets report the stats of the real file; |
| 72 | + // we can't use origRealPath here since that function calls lstat internally |
| 73 | + // which can result in an infinite loop |
| 74 | + return this.unguardedRealPath(path, (err, str) => { |
| 75 | + if (err) { |
| 76 | + if (err.code === 'ENOENT') { |
| 77 | + // broken link so there is nothing more to do |
| 78 | + return cb(null, stats); |
| 79 | + } |
| 80 | + return cb(err); |
| 81 | + } |
| 82 | + // Forward request to original callback. |
| 83 | + const req2 = new internalFs.FSReqCallback(bigint); |
| 84 | + req2.oncomplete = (err, realStats) => cb(err, realStats); |
| 85 | + return this._originalFsLstat.call(internalFs, str, bigint, req2, throwIfNotFound); |
| 86 | + }); |
| 87 | + }); |
| 88 | + } |
| 89 | + eeguardStatsSync(path, bigint, throwIfNoEntry, stats) { |
| 90 | + // No stats available. |
| 91 | + if (!stats) { |
| 92 | + return stats; |
| 93 | + } |
| 94 | + const statsObj = (0, utils_1.getStatsFromBinding)(stats); |
| 95 | + if (!statsObj.isSymbolicLink()) { |
| 96 | + // the file is not a symbolic link so there is nothing more to do |
| 97 | + return stats; |
| 98 | + } |
| 99 | + path = (0, fs_cjs_1.resolvePathLike)(path); |
| 100 | + if (!this.escapeFns.canEscape(path)) { |
| 101 | + // the file can not escaped the sandbox so there is nothing more to do |
| 102 | + return stats; |
| 103 | + } |
| 104 | + const guardedReadLink = this.guardedReadLinkSync(path); |
| 105 | + if (guardedReadLink != path) { |
| 106 | + // there are one or more hops within the guards so there is nothing more to do |
| 107 | + return stats; |
| 108 | + } |
| 109 | + try { |
| 110 | + path = this.unguardedRealPathSync(path); |
| 111 | + // there are no hops so lets report the stats of the real file; |
| 112 | + // we can't use origRealPathSync here since that function calls lstat internally |
| 113 | + // which can result in an infinite loop |
| 114 | + return this._originalFsLstat.call(internalFs, path, bigint, undefined, throwIfNoEntry); |
| 115 | + } |
| 116 | + catch (err) { |
| 117 | + if (err.code === 'ENOENT') { |
| 118 | + // broken link so there is nothing more to do |
| 119 | + return stats; |
| 120 | + } |
| 121 | + throw err; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | +exports.FsInternalStatPatcher = FsInternalStatPatcher; |
0 commit comments