From 9e3176e76e28a38be8f7c8e5dd7de23de2702c0b Mon Sep 17 00:00:00 2001 From: mikiher Date: Wed, 25 Nov 2026 05:54:07 +0200 Subject: [PATCH] Add isStrictSubPath and fix Windows namespaced and dot-prefix path checks --- server/utils/fileUtils.js | 35 +++++++++++------- test/server/utils/fileUtils.test.js | 56 +++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 12 deletions(-) diff --git a/server/utils/fileUtils.js b/server/utils/fileUtils.js index 9a349bd54..96276e4b5 100644 --- a/server/utils/fileUtils.js +++ b/server/utils/fileUtils.js @@ -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) diff --git a/test/server/utils/fileUtils.test.js b/test/server/utils/fileUtils.test.js index b57a6fb86..dd071c7e0 100644 --- a/test/server/utils/fileUtils.test.js +++ b/test/server/utils/fileUtils.test.js @@ -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) + }) + }) })