Skip to content

Commit dd02fde

Browse files
authored
fix: node fs.readlinkSync patch should maintain relative links (#2661)
Fix #2340 ### Changes are visible to end-users: no ### Test plan - Covered by existing test cases - New test cases added
1 parent f62a25e commit dd02fde

11 files changed

Lines changed: 92 additions & 40 deletions

js/private/devserver/js_run_devserver.mjs

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/private/devserver/src/js_run_devserver.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,8 @@ async function syncSymlink(file, src, dst, sandbox, exists) {
143143
let symlinkMeta = ''
144144
if (isNodeModulePath(file)) {
145145
let linkPath = await fs.promises.readlink(src)
146-
if (path.isAbsolute(linkPath)) {
147-
linkPath = path.relative(src, linkPath)
148-
}
146+
const linkAbs = path.resolve(path.dirname(src), linkPath)
147+
linkPath = path.relative(src, linkAbs) || '.'
149148
// Special case for 1p node_modules symlinks
150149
const maybe1pSync = path.join(sandbox, file, linkPath)
151150
if (fs.existsSync(maybe1pSync)) {

js/private/node-patches/fs.cjs

Lines changed: 22 additions & 12 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: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,12 @@ export function patcher(roots: string[]): () => void {
301301
) {
302302
if (err) return cb(err)
303303
const resolved = resolvePathLike(args[0])
304-
const str = path.resolve(path.dirname(resolved), p!)
305-
const escapedRoot: string | false = isEscape(resolved, str)
304+
const linkTarget = p!
305+
const targetAbs = path.resolve(path.dirname(resolved), linkTarget)
306+
const escapedRoot: string | false = isEscape(resolved, targetAbs)
306307
if (escapedRoot) {
307308
const escapedRoots = [escapedRoot]
308-
return nextHop(str, readlinkNextHopCb)
309+
return nextHop(targetAbs, readlinkNextHopCb)
309310

310311
function readlinkNextHopCb(next: string | undefined | false) {
311312
if (!next) {
@@ -319,16 +320,20 @@ export function patcher(roots: string[]): () => void {
319320
}
320321
const r = path.resolve(
321322
path.dirname(resolved),
322-
path.relative(path.dirname(str), next)
323+
path.relative(path.dirname(targetAbs), next)
323324
)
324325
if (r != resolved && !isEscape(resolved, r, escapedRoots)) {
325-
return cb(null, r)
326+
if (path.isAbsolute(linkTarget)) {
327+
return cb(null, r)
328+
}
329+
const rel = path.relative(path.dirname(resolved), r)
330+
return cb(null, rel || '.')
326331
}
327332
// The escape from the root is not mappable back into the root; throw EINVAL
328333
return cb(einval('readlink', args[0]))
329334
}
330335
} else {
331-
return cb(null, str)
336+
return cb(null, linkTarget)
332337
}
333338
}
334339

@@ -339,15 +344,12 @@ export function patcher(roots: string[]): () => void {
339344
...args: Parameters<typeof origReadlinkSync>
340345
) {
341346
const resolved = resolvePathLike(args[0])
347+
const linkTarget = origReadlinkSync(...args)
348+
const targetAbs = path.resolve(path.dirname(resolved), linkTarget)
342349

343-
const str = path.resolve(
344-
path.dirname(resolved),
345-
origReadlinkSync(...args)
346-
)
347-
348-
const escapedRoot: string | false = isEscape(resolved, str)
350+
const escapedRoot: string | false = isEscape(resolved, targetAbs)
349351
if (escapedRoot) {
350-
const next = nextHopSync(str)
352+
const next = nextHopSync(targetAbs)
351353
if (!next) {
352354
if (next == undefined) {
353355
// The escape from the root is not mappable back into the root; throw EINVAL
@@ -359,15 +361,19 @@ export function patcher(roots: string[]): () => void {
359361
}
360362
const r = path.resolve(
361363
path.dirname(resolved),
362-
path.relative(path.dirname(str), next)
364+
path.relative(path.dirname(targetAbs), next)
363365
)
364366
if (r != resolved && !isEscape(resolved, r, [escapedRoot])) {
365-
return r
367+
if (path.isAbsolute(linkTarget)) {
368+
return r
369+
}
370+
const rel = path.relative(path.dirname(resolved), r)
371+
return rel || '.'
366372
}
367373
// The escape from the root is not mappable back into the root; throw EINVAL
368374
throw einval('readlink', args[0])
369375
}
370-
return str
376+
return linkTarget
371377
}
372378

373379
// =========================================================================
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
8fc69600f1f334af341785b642217395b09edd6b4f0e8032fa09693b03d21439 js/private/test/image/cksum_node.tar
1+
c95723d23b1d038ea1f7319f4c68fde5f81812dff389b988cd403e2e20f164a8 js/private/test/image/cksum_node.tar
22
c6c899aac772f0dd98117f2a469af76d8fb6ad27ccc7bfbc68f5e4ed274bf0ea js/private/test/image/cksum_package_store_3p.tar
33
bcd3826edb788a083e88ae3dbfb7fbd86bb1de8d8ef1db881b6488d63d715245 js/private/test/image/cksum_package_store_1p.tar
44
febf95a6d554c9bda3f0515bfd5ef273ac67d31c231d8162beaef8c4b7bc72f3 js/private/test/image/cksum_node_modules.tar

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 34729 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 35271 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 34729 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
10+
-r-xr-xr-x 0 100 0 35271 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 34729 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
10+
-r-xr-xr-x 0 0 0 35271 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 34729 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 35271 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 34729 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 35271 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/

0 commit comments

Comments
 (0)