Skip to content

Commit f2c0632

Browse files
authored
refactor: remove unreachable fs patch code (#2511)
I can't find any way to hit this code path. ### Changes are visible to end-users: no ### Test plan - Covered by existing test cases - New test cases added
1 parent e10d6d0 commit f2c0632

File tree

9 files changed

+126
-30
lines changed

9 files changed

+126
-30
lines changed

js/private/node-patches/fs.cjs

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/private/node-patches/src/fs.cts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -848,19 +848,7 @@ export function patcher(roots: string[]): () => void {
848848
function oneHop(loc: string, cb: ErrPathCallback) {
849849
nextHop(loc, function guardedRealPathHopCb(next) {
850850
if (!next) {
851-
// we're no longer hopping but we haven't escaped
852-
return fs.exists(
853-
loc,
854-
function guardedRealPathExistsCb(e: boolean) {
855-
if (e) {
856-
// we hit a real file within the guard and can go no further
857-
return cb(null, loc)
858-
} else {
859-
// something funky happened in the filesystem
860-
return cb(enoent('realpath', start), undefined)
861-
}
862-
}
863-
)
851+
return cb(enoent('realpath', start), undefined)
864852
}
865853
if (
866854
escapedRoot
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
7fed2b2750ce4ce3711365b67b729585f363c4f6796e7cbc981b84e468872ebe js/private/test/image/cksum_node
1+
3fd4581c0ae8774a7d6d4b174802d91d0fcfc1b99d7b438abeac409636114a11 js/private/test/image/cksum_node
22
052600f3a82ab6a4cc12cab7384971c960f9c589fdbfcf21bca563c36ff7d16e js/private/test/image/cksum_package_store_3p
33
971f291232f3ab63aff37fb66c96fbf0eddc05ea9564b9673d0d2c9bfe958994 js/private/test/image/cksum_package_store_1p
44
febf95a6d554c9bda3f0515bfd5ef273ac67d31c231d8162beaef8c4b7bc72f3 js/private/test/image/cksum_node_modules

js/private/test/image/custom_layers_nomatch_test_node.listing

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.
88
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/
99
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/
1010
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/
11-
-r-xr-xr-x 0 0 0 34941 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
11+
-r-xr-xr-x 0 0 0 34430 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
1212
-r-xr-xr-x 0 0 0 1460 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
1313
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
1414
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/

js/private/test/image/custom_owner_test_node.listing

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runf
77
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/
88
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/
99
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/
10-
-r-xr-xr-x 0 100 0 34941 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
10+
-r-xr-xr-x 0 100 0 34430 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
1111
-r-xr-xr-x 0 100 0 1460 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
1212
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
1313
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/

js/private/test/image/default_test_node.listing

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runf
77
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/
88
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/
99
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/
10-
-r-xr-xr-x 0 0 0 34941 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
10+
-r-xr-xr-x 0 0 0 34430 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
1111
-r-xr-xr-x 0 0 0 1460 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
1212
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
1313
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/

js/private/test/image/non_ascii/custom_layer_groups_test_just_the_fs_patch.listing

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_
99
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/
1010
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/
1111
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/node-patches/
12-
-r-xr-xr-x 0 0 0 34941 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/node-patches/fs.cjs
12+
-r-xr-xr-x 0 0 0 34430 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/node-patches/fs.cjs

js/private/test/image/regex_edge_cases_test_node.listing

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.
88
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/
99
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/
1010
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/
11-
-r-xr-xr-x 0 0 0 34941 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
11+
-r-xr-xr-x 0 0 0 34430 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
1212
-r-xr-xr-x 0 0 0 1460 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
1313
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
1414
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/

js/private/test/node-patches/realpath.mjs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,124 @@ describe('testing realpath', async () => {
352352
)
353353
})
354354

355+
await it('can resolve a symlink to a non-existing path', async () => {
356+
await withFixtures(
357+
{
358+
sandbox: {},
359+
execroot: {},
360+
otherroot: { file: 'contents' },
361+
},
362+
async (fixturesDir) => {
363+
fixturesDir = fs.realpathSync(fixturesDir)
364+
365+
const revertPatches = patcher([
366+
path.join(fixturesDir, 'sandbox'),
367+
])
368+
369+
let brokenLinkPath = path.join(
370+
fixturesDir,
371+
'sandbox',
372+
'broken-link'
373+
)
374+
fs.symlinkSync(
375+
path.join(fixturesDir, 'doesnt-exist'),
376+
brokenLinkPath
377+
)
378+
379+
assert.throws(
380+
() => fs.realpathSync(brokenLinkPath),
381+
'should throw because link is broken'
382+
)
383+
384+
let thrown
385+
try {
386+
await util.promisify(fs.realpath.native)(brokenLinkPath)
387+
} catch (e) {
388+
thrown = e
389+
} finally {
390+
if (!thrown)
391+
assert.fail('should throw if empty string is passed')
392+
}
393+
394+
try {
395+
await fs.promises.realpath(brokenLinkPath)
396+
} catch (e) {
397+
thrown = e
398+
} finally {
399+
if (!thrown)
400+
assert.fail('should throw if empty string is passed')
401+
}
402+
403+
revertPatches()
404+
}
405+
)
406+
})
407+
408+
await it('can resolve a symlink to a non-existing path after escaping', async () => {
409+
await withFixtures(
410+
{
411+
sandbox: {},
412+
execroot: {},
413+
otherroot: { file: 'contents' },
414+
},
415+
async (fixturesDir) => {
416+
fixturesDir = fs.realpathSync(fixturesDir)
417+
418+
const nonSandboxedBrokenLink = path.join(
419+
fixturesDir,
420+
'broken-link'
421+
)
422+
423+
fs.symlinkSync(
424+
path.join(fixturesDir, 'doesnt-exist'),
425+
nonSandboxedBrokenLink
426+
)
427+
428+
const revertPatches = patcher([
429+
path.join(fixturesDir, 'sandbox'),
430+
])
431+
432+
let sandboxedLinkToBrokenLink = path.join(
433+
fixturesDir,
434+
'sandbox',
435+
'indirect-link'
436+
)
437+
fs.symlinkSync(
438+
nonSandboxedBrokenLink,
439+
sandboxedLinkToBrokenLink
440+
)
441+
442+
assert.throws(
443+
() => fs.realpathSync(sandboxedLinkToBrokenLink),
444+
'should throw because link is broken'
445+
)
446+
447+
let thrown
448+
try {
449+
await util.promisify(fs.realpath.native)(
450+
sandboxedLinkToBrokenLink
451+
)
452+
} catch (e) {
453+
thrown = e
454+
} finally {
455+
if (!thrown)
456+
assert.fail('should throw if empty string is passed')
457+
}
458+
459+
try {
460+
await fs.promises.realpath(sandboxedLinkToBrokenLink)
461+
} catch (e) {
462+
thrown = e
463+
} finally {
464+
if (!thrown)
465+
assert.fail('should throw if empty string is passed')
466+
}
467+
468+
revertPatches()
469+
}
470+
)
471+
})
472+
355473
await it('can resolve symlink to a symlink in the sandbox if there is no corresponding location in the sandbox but is a realpath outside', async () => {
356474
await withFixtures(
357475
{

0 commit comments

Comments
 (0)