Skip to content

Commit

Permalink
fix: fix symlink pointing error in filesystem.insertLink (#301)
Browse files Browse the repository at this point in the history
Co-authored-by: jrainliu <jrainliu@tencent.com>
  • Loading branch information
jrainlau and jrainliu committed Mar 5, 2024
1 parent 896ed4a commit a9b4e7b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/filesystem.js
Expand Up @@ -99,7 +99,9 @@ class Filesystem {
}

insertLink (p) {
const link = path.relative(fs.realpathSync(this.src), fs.realpathSync(p))
const symlink = fs.readlinkSync(p)
const parentPath = path.dirname(p)
const link = path.relative(fs.realpathSync(this.src), path.join(parentPath, symlink))
if (link.substr(0, 2) === '..') {
throw new Error(`${p}: file "${link}" links out of the package`)
}
Expand Down
5 changes: 5 additions & 0 deletions test/api-spec.js
Expand Up @@ -81,6 +81,11 @@ describe('api', function () {
asar.extractAll('test/input/extractthis-unpack-dir.asar', 'tmp/extractthis-unpack-dir-api/')
return compDirs('tmp/extractthis-unpack-dir-api/', 'test/expected/extractthis')
})
it('should extract an archive with symlink', async () => {
await asar.createPackageWithOptions('test/input/packthis-with-symlink/', 'tmp/packthis-with-symlink.asar', { dot: false })
asar.extractAll('tmp/packthis-with-symlink.asar', 'tmp/packthis-with-symlink/')
return compFiles('tmp/packthis-with-symlink/real.txt', 'test/input/packthis-with-symlink/real.txt')
})
it('should handle multibyte characters in paths', async () => {
await asar.createPackageWithOptions('test/input/packthis-unicode-path/', 'tmp/packthis-unicode-path.asar', {
globOptions: {
Expand Down
1 change: 1 addition & 0 deletions test/input/packthis-with-symlink/A/real.txt
@@ -0,0 +1 @@
I AM REAL TXT FILE
1 change: 1 addition & 0 deletions test/input/packthis-with-symlink/Current
1 change: 1 addition & 0 deletions test/input/packthis-with-symlink/real.txt
17 changes: 15 additions & 2 deletions test/util/compareFiles.js
Expand Up @@ -7,6 +7,19 @@ module.exports = async function (actualFilePath, expectedFilePath) {
if (process.env.ELECTRON_ASAR_SPEC_UPDATE) {
await fs.writeFile(expectedFilePath, await fs.readFile(actualFilePath))
}
const [actual, expected] = await Promise.all([fs.readFile(actualFilePath, 'utf8'), fs.readFile(expectedFilePath, 'utf8')])
assert.strictEqual(actual, expected)
const [actualFileContent, expectedFileContent] = await Promise.all([fs.readFile(actualFilePath, 'utf8'), fs.readFile(expectedFilePath, 'utf8')])
assert.strictEqual(actualFileContent, expectedFileContent)

const [actualIsSymlink, expectedIsSymlink] = [isSymbolicLinkSync(actualFilePath), isSymbolicLinkSync(expectedFilePath)]
assert.strictEqual(actualIsSymlink, expectedIsSymlink)

if (actualIsSymlink && expectedIsSymlink) {
const [actualSymlinkPointer, expectedSymlinkPointer] = [fs.readlinkSync(actualFilePath), fs.readlinkSync(expectedFilePath)]
assert.strictEqual(actualSymlinkPointer, expectedSymlinkPointer)
}
}

function isSymbolicLinkSync (path) {
const stats = fs.lstatSync(path)
return stats.isSymbolicLink()
}

0 comments on commit a9b4e7b

Please sign in to comment.