Skip to content

Commit d8111fc

Browse files
ziqiangBlackHole1
ziqiang
andauthored
fix: relative link error (#308)
Signed-off-by: Kevin Cui <[email protected]> Co-authored-by: Kevin Cui <[email protected]>
1 parent dd18c0c commit d8111fc

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

lib/filesystem.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ class Filesystem {
100100

101101
insertLink (p) {
102102
const symlink = fs.readlinkSync(p)
103-
const parentPath = path.dirname(p)
103+
// /var => /private/var
104+
const parentPath = fs.realpathSync(path.dirname(p))
104105
const link = path.relative(fs.realpathSync(this.src), path.join(parentPath, symlink))
105-
if (link.substr(0, 2) === '..') {
106+
if (link.startsWith('..')) {
106107
throw new Error(`${p}: file "${link}" links out of the package`)
107108
}
108109
const node = this.searchNodeFromPath(p)

test/filesystem-spec.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict'
2+
3+
const assert = require('assert')
4+
const fs = require('../lib/wrapped-fs')
5+
const path = require('path')
6+
const rimraf = require('rimraf')
7+
8+
const Filesystem = require('../lib/filesystem')
9+
10+
describe('filesystem', function () {
11+
beforeEach(() => { rimraf.sync(path.join(__dirname, '..', 'tmp'), fs) })
12+
13+
it('should does not throw an error when the src path includes a symbol link', async () => {
14+
/**
15+
* Directory structure:
16+
* tmp
17+
* ├── private
18+
* │ └── var
19+
* │ ├── app
20+
* │ │ └── file.txt -> ../file.txt
21+
* │ └── file.txt
22+
* └── var -> private/var
23+
*/
24+
const tmpPath = path.join(__dirname, '..', 'tmp')
25+
const privateVarPath = path.join(tmpPath, 'private', 'var')
26+
const varPath = path.join(tmpPath, 'var')
27+
fs.mkdirSync(privateVarPath, { recursive: true })
28+
fs.symlinkSync(path.relative(tmpPath, privateVarPath), varPath)
29+
30+
const originFilePath = path.join(varPath, 'file.txt')
31+
fs.writeFileSync(originFilePath, 'hello world')
32+
const appPath = path.join(varPath, 'app')
33+
fs.mkdirpSync(appPath)
34+
fs.symlinkSync('../file.txt', path.join(appPath, 'file.txt'))
35+
36+
const filesystem = new Filesystem(varPath)
37+
assert.doesNotThrow(() => {
38+
filesystem.insertLink(path.join(appPath, 'file.txt'))
39+
})
40+
})
41+
})

0 commit comments

Comments
 (0)