diff --git a/lib/util/fs/index.spec.ts b/lib/util/fs/index.spec.ts index 71aa1257a763052f877d63eae2d820ccf31e512a..0f221e33e12eeee775c3b1303097a5b7d93179bf 100644 --- a/lib/util/fs/index.spec.ts +++ b/lib/util/fs/index.spec.ts @@ -1,7 +1,7 @@ import _findUp from 'find-up'; import fs from 'fs-extra'; import tmp, { DirectoryResult } from 'tmp-promise'; -import { join } from 'upath'; +import { join, resolve } from 'upath'; import { mockedFunction } from '../../../test/util'; import { GlobalConfig } from '../../config/global'; import { @@ -246,7 +246,7 @@ describe('util/fs/index', () => { expect(result).toMatchSnapshot(); await writeLocalFile('Cargo.lock', ''); - await writeLocalFile('/test/subdir/Cargo.lock', ''); + await writeLocalFile('test/subdir/Cargo.lock', ''); const resultWithAdditionalFiles = await readLocalDirectory('test'); expect(resultWithAdditionalFiles).not.toBeNull(); @@ -291,13 +291,13 @@ describe('util/fs/index', () => { }); it('returns false for directory', async () => { - const path = `${localDir}/foobar`; + const path = resolve(`${localDir}/foobar`); await fs.mkdir(path); expect(await localPathIsFile(path)).toBeFalse(); }); it('returns false for non-existing path', async () => { - expect(await localPathIsFile(`${localDir}/foobar`)).toBeFalse(); + expect(await localPathIsFile(resolve(`${localDir}/foobar`))).toBeFalse(); }); }); diff --git a/lib/util/fs/util.spec.ts b/lib/util/fs/util.spec.ts index ae8c7d4c5b9b7062e7fd8a1d7f02e4c11aad2066..f62fff16254565936879965e2873fd0bd1cb0be2 100644 --- a/lib/util/fs/util.spec.ts +++ b/lib/util/fs/util.spec.ts @@ -1,20 +1,20 @@ +import upath from 'upath'; import { GlobalConfig } from '../../config/global'; import { FILE_ACCESS_VIOLATION_ERROR } from '../../constants/error-messages'; import { ensureCachePath, ensureLocalPath } from './util'; describe('util/fs/util', () => { - const localDir = '/foo'; - const cacheDir = '/bar'; + const localDir = upath.resolve('/foo'); + const cacheDir = upath.resolve('/bar'); beforeAll(() => { GlobalConfig.set({ localDir, cacheDir }); }); test.each` - path | fullPath - ${''} | ${`${localDir}`} - ${'baz'} | ${`${localDir}/baz`} - ${'/baz'} | ${`${localDir}/baz`} + path | fullPath + ${''} | ${`${localDir}`} + ${'baz'} | ${`${localDir}/baz`} `(`ensureLocalPath('$path', '$fullPath')`, ({ path, fullPath }) => { expect(ensureLocalPath(path)).toBe(fullPath); }); @@ -25,15 +25,15 @@ describe('util/fs/util', () => { ${'../etc/passwd'} ${'/foo/../bar'} ${'/foo/../../etc/passwd'} + ${'/baz'} `(`ensureLocalPath('$path', '${localDir}') - throws`, ({ path }) => { expect(() => ensureLocalPath(path)).toThrow(FILE_ACCESS_VIOLATION_ERROR); }); test.each` - path | fullPath - ${''} | ${`${cacheDir}`} - ${'baz'} | ${`${cacheDir}/baz`} - ${'/baz'} | ${`${cacheDir}/baz`} + path | fullPath + ${''} | ${`${cacheDir}`} + ${'baz'} | ${`${cacheDir}/baz`} `(`ensureCachePath('$path', '$fullPath')`, ({ path, fullPath }) => { expect(ensureCachePath(path)).toBe(fullPath); }); @@ -44,6 +44,7 @@ describe('util/fs/util', () => { ${'../etc/passwd'} ${'/bar/../foo'} ${'/bar/../../etc/passwd'} + ${'/baz'} `(`ensureCachePath('$path', '${cacheDir}') - throws`, ({ path }) => { expect(() => ensureCachePath(path)).toThrow(FILE_ACCESS_VIOLATION_ERROR); }); diff --git a/lib/util/fs/util.ts b/lib/util/fs/util.ts index f63226bf37176c890007f687da819f5488819d0d..72844a7025216a0b744c9556c879373d270e037a 100644 --- a/lib/util/fs/util.ts +++ b/lib/util/fs/util.ts @@ -15,11 +15,9 @@ function assertBaseDir(path: string, baseDir: string): void { function ensurePath(path: string, key: 'localDir' | 'cacheDir'): string { const baseDir = upath.resolve(GlobalConfig.get(key)!); - let fullPath = path; - if (fullPath.startsWith(baseDir)) { - fullPath = fullPath.replace(baseDir, ''); - } - fullPath = upath.resolve(upath.join(baseDir, fullPath)); + const fullPath = upath.resolve( + upath.isAbsolute(path) ? path : upath.join(baseDir, path) + ); assertBaseDir(fullPath, baseDir); return fullPath; }