From b6930bf36846b22cb6a0cf4f697000f80762b675 Mon Sep 17 00:00:00 2001 From: ziqiang <1694392889@qq.com> Date: Wed, 27 Mar 2024 22:32:44 +0800 Subject: [PATCH 01/12] fix: relative link error --- lib/filesystem.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 27b00a1..4aa082d 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -101,7 +101,7 @@ class Filesystem { insertLink (p) { const symlink = fs.readlinkSync(p) const parentPath = path.dirname(p) - const link = path.relative(fs.realpathSync(this.src), path.join(parentPath, symlink)) + const link = path.relative(fs.realpathSync(this.src), fs.realpathSync(path.join(parentPath, symlink))) if (link.substr(0, 2) === '..') { throw new Error(`${p}: file "${link}" links out of the package`) } From 44a7cdaac37e23872edb123b0c4f32a162dacd47 Mon Sep 17 00:00:00 2001 From: ziqiang <1694392889@qq.com> Date: Wed, 27 Mar 2024 22:59:13 +0800 Subject: [PATCH 02/12] feat: use resolve method --- lib/filesystem.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 4aa082d..8981a7d 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -101,7 +101,7 @@ class Filesystem { insertLink (p) { const symlink = fs.readlinkSync(p) const parentPath = path.dirname(p) - const link = path.relative(fs.realpathSync(this.src), fs.realpathSync(path.join(parentPath, symlink))) + const link = path.relative(fs.realpathSync(this.src), path.resolve(parentPath, symlink)) if (link.substr(0, 2) === '..') { throw new Error(`${p}: file "${link}" links out of the package`) } From 18b6e3f9cf9c69e00c61d1c738f70e4bb3bdd086 Mon Sep 17 00:00:00 2001 From: ziqiang <1694392889@qq.com> Date: Wed, 27 Mar 2024 23:04:54 +0800 Subject: [PATCH 03/12] feat: use resolve method --- lib/filesystem.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 8981a7d..2709491 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -101,12 +101,12 @@ class Filesystem { insertLink (p) { const symlink = fs.readlinkSync(p) const parentPath = path.dirname(p) - const link = path.relative(fs.realpathSync(this.src), path.resolve(parentPath, symlink)) + const link = path.resolve(parentPath, symlink); if (link.substr(0, 2) === '..') { throw new Error(`${p}: file "${link}" links out of the package`) } const node = this.searchNodeFromPath(p) - node.link = link + node.link = path.relative(fs.realpathSync(this.src), link); return link } From a173528524a3f7794ae892732765a552482ae053 Mon Sep 17 00:00:00 2001 From: ziqiang <1694392889@qq.com> Date: Wed, 27 Mar 2024 23:10:59 +0800 Subject: [PATCH 04/12] fix: lint error --- lib/filesystem.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 2709491..9cfcdd9 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -101,7 +101,7 @@ class Filesystem { insertLink (p) { const symlink = fs.readlinkSync(p) const parentPath = path.dirname(p) - const link = path.resolve(parentPath, symlink); + const link= path.resolve(parentPath, symlink); if (link.substr(0, 2) === '..') { throw new Error(`${p}: file "${link}" links out of the package`) } From 4738c3f85f3e0d00f9a98d15cf8730a24c1b7e46 Mon Sep 17 00:00:00 2001 From: ziqiang <1694392889@qq.com> Date: Wed, 27 Mar 2024 23:12:53 +0800 Subject: [PATCH 05/12] fix: lint error --- lib/filesystem.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 9cfcdd9..2033626 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -101,12 +101,12 @@ class Filesystem { insertLink (p) { const symlink = fs.readlinkSync(p) const parentPath = path.dirname(p) - const link= path.resolve(parentPath, symlink); + const link = path.resolve(parentPath, symlink) if (link.substr(0, 2) === '..') { throw new Error(`${p}: file "${link}" links out of the package`) } const node = this.searchNodeFromPath(p) - node.link = path.relative(fs.realpathSync(this.src), link); + node.link = path.relative(fs.realpathSync(this.src), link) return link } From b05b255728d257499272749c464038e7be24b335 Mon Sep 17 00:00:00 2001 From: ziqiang <1694392889@qq.com> Date: Thu, 28 Mar 2024 15:15:26 +0800 Subject: [PATCH 06/12] fix: new implement handle link --- lib/filesystem.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 2033626..8ef421d 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -99,14 +99,15 @@ class Filesystem { } insertLink (p) { - const symlink = fs.readlinkSync(p) - const parentPath = path.dirname(p) - const link = path.resolve(parentPath, symlink) - if (link.substr(0, 2) === '..') { - throw new Error(`${p}: file "${link}" links out of the package`) + const realSrcPath = fs.realpathSync(this.src) + const target = path.join(path.dirname(p), fs.readlinkSync(p)) + const relativePath = path.relative(realSrcPath, fs.realpathSync(target)) + if (relativePath.startsWith('..')) { + throw new Error(`${p}: file "${relativePath}" links out of the package`) } + const link = path.relative(realSrcPath, target) const node = this.searchNodeFromPath(p) - node.link = path.relative(fs.realpathSync(this.src), link) + node.link = link return link } From d278f71e05b3520b0223c5aba14a30b96fa8508e Mon Sep 17 00:00:00 2001 From: Kevin Cui Date: Thu, 28 Mar 2024 18:20:30 +0800 Subject: [PATCH 07/12] test(filesystem): add insertLink test Signed-off-by: Kevin Cui --- test/filesystem-spec.js | 16 ++++++++++++++++ .../private/var/app/real.txt | 0 .../private/var/app/symbol/real.txt | 1 + test/input/srcpath-include-symlink/var | 1 + 4 files changed, 18 insertions(+) create mode 100644 test/filesystem-spec.js create mode 100644 test/input/srcpath-include-symlink/private/var/app/real.txt create mode 120000 test/input/srcpath-include-symlink/private/var/app/symbol/real.txt create mode 120000 test/input/srcpath-include-symlink/var diff --git a/test/filesystem-spec.js b/test/filesystem-spec.js new file mode 100644 index 0000000..316e6b5 --- /dev/null +++ b/test/filesystem-spec.js @@ -0,0 +1,16 @@ +'use strict' + +const assert = require('assert') +const path = require('path') + +const Filesystem = require('../lib/filesystem') + +describe('filesystem', function () { + it('should does not throw error when src path include symbol link', async () => { + const src = path.join(__dirname, 'input', 'srcpath-include-symlink', 'var', 'app') + const filesystem = new Filesystem(src) + assert.doesNotThrow(() => { + filesystem.insertLink(path.join(src, 'symbol', 'real.txt')) + }) + }) +}) diff --git a/test/input/srcpath-include-symlink/private/var/app/real.txt b/test/input/srcpath-include-symlink/private/var/app/real.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/input/srcpath-include-symlink/private/var/app/symbol/real.txt b/test/input/srcpath-include-symlink/private/var/app/symbol/real.txt new file mode 120000 index 0000000..5099602 --- /dev/null +++ b/test/input/srcpath-include-symlink/private/var/app/symbol/real.txt @@ -0,0 +1 @@ +../real.txt \ No newline at end of file diff --git a/test/input/srcpath-include-symlink/var b/test/input/srcpath-include-symlink/var new file mode 120000 index 0000000..3ec7299 --- /dev/null +++ b/test/input/srcpath-include-symlink/var @@ -0,0 +1 @@ +private/var \ No newline at end of file From 880d020d7aab91742aa55fdcb5e975dfd06fc446 Mon Sep 17 00:00:00 2001 From: Kevin Cui Date: Thu, 28 Mar 2024 19:14:26 +0800 Subject: [PATCH 08/12] fix test Signed-off-by: Kevin Cui --- test/filesystem-spec.js | 32 +++++++++++++++++-- .../private/var/app/real.txt | 0 .../private/var/app/symbol/real.txt | 1 - test/input/srcpath-include-symlink/var | 1 - 4 files changed, 29 insertions(+), 5 deletions(-) delete mode 100644 test/input/srcpath-include-symlink/private/var/app/real.txt delete mode 120000 test/input/srcpath-include-symlink/private/var/app/symbol/real.txt delete mode 120000 test/input/srcpath-include-symlink/var diff --git a/test/filesystem-spec.js b/test/filesystem-spec.js index 316e6b5..a2b4c7a 100644 --- a/test/filesystem-spec.js +++ b/test/filesystem-spec.js @@ -1,16 +1,42 @@ 'use strict' const assert = require('assert') +const fs = require('../lib/wrapped-fs') const path = require('path') +const rimraf = require('rimraf') const Filesystem = require('../lib/filesystem') describe('filesystem', function () { + beforeEach(() => { rimraf.sync(path.join(__dirname, '..', 'tmp'), fs) }) + it('should does not throw error when src path include symbol link', async () => { - const src = path.join(__dirname, 'input', 'srcpath-include-symlink', 'var', 'app') - const filesystem = new Filesystem(src) + /* eslint-disable no-irregular-whitespace */ + /** + * Directory structure: + * tmp + * ├── private + * │   └── var + * │   ├── app + * │   │   └── file.txt -> ../file.txt + * │   └── file.txt + * └── var -> private/var + */ + const tmpPath = path.join(__dirname, '..', 'tmp') + const privateVarPath = path.join(tmpPath, 'private', 'var') + const varPath = path.join(tmpPath, 'var') + fs.mkdirSync(privateVarPath, { recursive: true }) + fs.symlinkSync(path.relative(tmpPath, privateVarPath), varPath) + + const originFilePath = path.join(varPath, 'file.txt') + fs.writeFileSync(originFilePath, 'hello world') + const appPath = path.join(varPath, 'app') + fs.mkdirpSync(appPath) + fs.symlinkSync('../file.txt', path.join(appPath, 'file.txt')) + + const filesystem = new Filesystem(varPath) assert.doesNotThrow(() => { - filesystem.insertLink(path.join(src, 'symbol', 'real.txt')) + filesystem.insertLink(path.join(appPath, 'file.txt')) }) }) }) diff --git a/test/input/srcpath-include-symlink/private/var/app/real.txt b/test/input/srcpath-include-symlink/private/var/app/real.txt deleted file mode 100644 index e69de29..0000000 diff --git a/test/input/srcpath-include-symlink/private/var/app/symbol/real.txt b/test/input/srcpath-include-symlink/private/var/app/symbol/real.txt deleted file mode 120000 index 5099602..0000000 --- a/test/input/srcpath-include-symlink/private/var/app/symbol/real.txt +++ /dev/null @@ -1 +0,0 @@ -../real.txt \ No newline at end of file diff --git a/test/input/srcpath-include-symlink/var b/test/input/srcpath-include-symlink/var deleted file mode 120000 index 3ec7299..0000000 --- a/test/input/srcpath-include-symlink/var +++ /dev/null @@ -1 +0,0 @@ -private/var \ No newline at end of file From ad7d2d5da309d586212041a88fcd1f519f5220c4 Mon Sep 17 00:00:00 2001 From: Kevin Cui Date: Thu, 28 Mar 2024 21:31:56 +0800 Subject: [PATCH 09/12] refactor: improve link path In previous implementations, the result obtained from `path.relative(realSrcPath, target)` is likely to be `../../../../../../../../../var/`. Although it has been tested and does not affect the program's execution, it looks very messy. To avoid this issue, modify `realSrcPath` to `this.src`. Signed-off-by: Kevin Cui --- lib/filesystem.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 8ef421d..106035a 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -105,7 +105,7 @@ class Filesystem { if (relativePath.startsWith('..')) { throw new Error(`${p}: file "${relativePath}" links out of the package`) } - const link = path.relative(realSrcPath, target) + const link = path.relative(this.src, target) const node = this.searchNodeFromPath(p) node.link = link return link From eaf970cc1fd1d128dcdf8a014fa8a0608f681b01 Mon Sep 17 00:00:00 2001 From: Kevin Cui Date: Sun, 28 Apr 2024 16:24:35 +0800 Subject: [PATCH 10/12] re-write logic Signed-off-by: Kevin Cui --- lib/filesystem.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 106035a..d921c50 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -99,13 +99,13 @@ class Filesystem { } insertLink (p) { - const realSrcPath = fs.realpathSync(this.src) - const target = path.join(path.dirname(p), fs.readlinkSync(p)) - const relativePath = path.relative(realSrcPath, fs.realpathSync(target)) - if (relativePath.startsWith('..')) { - throw new Error(`${p}: file "${relativePath}" links out of the package`) + const symlink = fs.readlinkSync(p) + // /var => /private/var + const parentPath = fs.realpathSync(path.dirname(p)) + const link = path.relative(fs.realpathSync(this.src), path.join(parentPath, symlink)) + if (link.startsWith('..')) { + throw new Error(`${p}: file "${link}" links out of the package`) } - const link = path.relative(this.src, target) const node = this.searchNodeFromPath(p) node.link = link return link From c23fb27d8ebc726b392b167ece69d5cc4e93d806 Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Thu, 2 May 2024 14:27:14 -0700 Subject: [PATCH 11/12] Update test/filesystem-spec.js --- test/filesystem-spec.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/filesystem-spec.js b/test/filesystem-spec.js index a2b4c7a..9f29dbe 100644 --- a/test/filesystem-spec.js +++ b/test/filesystem-spec.js @@ -11,15 +11,14 @@ describe('filesystem', function () { beforeEach(() => { rimraf.sync(path.join(__dirname, '..', 'tmp'), fs) }) it('should does not throw error when src path include symbol link', async () => { - /* eslint-disable no-irregular-whitespace */ /** * Directory structure: * tmp * ├── private - * │   └── var - * │   ├── app - * │   │   └── file.txt -> ../file.txt - * │   └── file.txt + * │ └── var + * │ ├── app + * │ │ └── file.txt -> ../file.txt + * │ └── file.txt * └── var -> private/var */ const tmpPath = path.join(__dirname, '..', 'tmp') From e901f4f75339e5ff6da093f39e5556818460ccbd Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Thu, 2 May 2024 14:27:20 -0700 Subject: [PATCH 12/12] Update test/filesystem-spec.js --- test/filesystem-spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/filesystem-spec.js b/test/filesystem-spec.js index 9f29dbe..8ac357d 100644 --- a/test/filesystem-spec.js +++ b/test/filesystem-spec.js @@ -10,7 +10,7 @@ const Filesystem = require('../lib/filesystem') describe('filesystem', function () { beforeEach(() => { rimraf.sync(path.join(__dirname, '..', 'tmp'), fs) }) - it('should does not throw error when src path include symbol link', async () => { + it('should does not throw an error when the src path includes a symbol link', async () => { /** * Directory structure: * tmp