Add isStrictSubPath and fix Windows namespaced and dot-prefix path checks

This commit is contained in:
mikiher committed 2026-11-25 05:54:07 +02:00
1 parent 5de038dafd
commit 9e3176e76e
2 files changed
+79 -12

No files matched your search

+23 -12
View File
@@ -19,25 +19,36 @@ const filePathToPOSIX = (path) => {
}
module.exports.filePathToPOSIX = filePathToPOSIX
/**
* Check whether childPath is inside parentPath (or the same directory when allowSame is true).
*/
function checkSubPath(parentPath, childPath, allowSame) {
const pathImpl = global.isWin === undefined ? Path : global.isWin ? Path.win32 : Path.posix
parentPath = pathImpl.toNamespacedPath(parentPath)
childPath = pathImpl.toNamespacedPath(childPath)
if (parentPath === childPath) return allowSame
const relativePath = pathImpl.relative(parentPath, childPath)
if (relativePath === '') return allowSame // Same path (e.g. parentPath = '/a/b/', childPath = '/a/b')
return relativePath !== '..' && !relativePath.startsWith('..' + pathImpl.sep) && !pathImpl.isAbsolute(relativePath)
}
/**
* Check path is a child of or equal to another path
*
* @param {string} parentPath
* @param {string} childPath
* @returns {boolean}
*/
function isSameOrSubPath(parentPath, childPath) {
parentPath = filePathToPOSIX(parentPath)
childPath = filePathToPOSIX(childPath)
if (parentPath === childPath) return true
const relativePath = Path.relative(parentPath, childPath)
return (
relativePath === '' || // Same path (e.g. parentPath = '/a/b/', childPath = '/a/b')
(!relativePath.startsWith('..') && !Path.isAbsolute(relativePath)) // Sub path
)
return checkSubPath(parentPath, childPath, true)
}
module.exports.isSameOrSubPath = isSameOrSubPath
/**
* True when childPath is inside parentPath and is not the same directory.
* Trailing separators still count as the same directory.
*/
function isStrictSubPath(parentPath, childPath) {
return checkSubPath(parentPath, childPath, false)
}
module.exports.isStrictSubPath = isStrictSubPath
function getFileStat(path) {
try {
return fs.stat(path)
+56
View File
@@ -134,4 +134,60 @@ describe('fileUtils', () => {
})
})
})
describe('isSameOrSubPath and isStrictSubPath', () => {
let prevIsWin
beforeEach(() => {
prevIsWin = global.isWin
})
afterEach(() => {
global.isWin = prevIsWin
})
it('identifies POSIX same paths, subpaths, and paths outside the parent', () => {
global.isWin = false
expect(fileUtils.isSameOrSubPath('/a/b', '/a/b')).to.equal(true)
expect(fileUtils.isSameOrSubPath('/a/b/', '/a/b')).to.equal(true)
expect(fileUtils.isSameOrSubPath('/a/b', '/a/b/')).to.equal(true)
expect(fileUtils.isSameOrSubPath('/a/b', '/a/b/c')).to.equal(true)
expect(fileUtils.isSameOrSubPath('/a/b', '/a/b/..foo')).to.equal(true)
expect(fileUtils.isSameOrSubPath('/a/b', '/a/bc')).to.equal(false)
expect(fileUtils.isSameOrSubPath('/a/b', '/a')).to.equal(false)
expect(fileUtils.isSameOrSubPath('/a/b', '/a/b/../c')).to.equal(false)
expect(fileUtils.isStrictSubPath('/a/b', '/a/b')).to.equal(false)
expect(fileUtils.isStrictSubPath('/a/b/', '/a/b')).to.equal(false)
expect(fileUtils.isStrictSubPath('/a/b', '/a/b/')).to.equal(false)
expect(fileUtils.isStrictSubPath('/a/b', '/a/b/c')).to.equal(true)
expect(fileUtils.isStrictSubPath('/a/b', '/a/b/..foo')).to.equal(true)
expect(fileUtils.isStrictSubPath('/a/b', '/a/bc')).to.equal(false)
})
it('matches standard and Windows namespaced drive and UNC paths', () => {
global.isWin = true
expect(fileUtils.isSameOrSubPath('C:\\lib\\Author', '\\\\?\\C:\\lib\\Author')).to.equal(true)
expect(fileUtils.isSameOrSubPath('\\\\?\\C:\\lib\\Author', 'C:\\lib\\Author\\Book')).to.equal(true)
expect(fileUtils.isSameOrSubPath('C:/lib', '\\\\?\\C:\\lib\\Author')).to.equal(true)
expect(fileUtils.isSameOrSubPath('C:\\lib', 'C:\\lib\\..foo')).to.equal(true)
expect(fileUtils.isSameOrSubPath('\\\\server\\share\\lib', '\\\\?\\UNC\\server\\share\\lib\\Author')).to.equal(true)
expect(fileUtils.isSameOrSubPath('\\\\server/share/lib', '\\\\?\\UNC\\server\\share\\lib\\Author')).to.equal(true)
expect(fileUtils.isSameOrSubPath('\\\\?\\UNC\\server\\share\\lib', '\\\\server\\share\\lib\\Author')).to.equal(true)
expect(fileUtils.isSameOrSubPath('\\\\?\\Volume{1234}\\lib', '\\\\?\\Volume{1234}\\lib\\Author')).to.equal(true)
expect(fileUtils.isSameOrSubPath('C:\\lib', '\\\\?\\D:\\lib\\Author')).to.equal(false)
expect(fileUtils.isSameOrSubPath('C:\\lib', 'C:\\lib2\\Author')).to.equal(false)
expect(fileUtils.isSameOrSubPath('\\\\server\\share\\lib', '\\\\other\\share\\lib\\Author')).to.equal(false)
expect(fileUtils.isSameOrSubPath('\\\\?\\UNC\\server\\share\\lib', '\\\\other\\share\\lib\\Author')).to.equal(false)
expect(fileUtils.isSameOrSubPath('\\\\?\\C:\\lib', '\\\\?\\UNC\\server\\share\\lib')).to.equal(false)
expect(fileUtils.isStrictSubPath('\\\\?\\C:\\lib\\Author', 'C:\\lib\\Author')).to.equal(false)
expect(fileUtils.isStrictSubPath('\\\\?\\C:\\lib\\Author', 'C:\\lib\\Author\\Book')).to.equal(true)
expect(fileUtils.isStrictSubPath('\\\\server\\share\\lib', '\\\\?\\UNC\\server\\share\\lib\\Author')).to.equal(true)
})
})
})