文件
mynode/packages/lib/utils/resolvePathWithinDir.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

41 行
1.2 KiB
TypeScript

import * as path from 'path';
// Returns `null` if `relativePath` is not within `baseDir` and `relativePath`
// resolved to an absolute path otherwise.
//
// `relativePath` can be either relative or absolute.
// If relative, it is assumed to be relative to `baseDir`.
//
// It is expected that baseDir is a safe path (not user-provided).
const resolvePathWithinDir = (
baseDir: string, relativePath: string,
// For testing
pathModule = path,
) => {
let resolvedBaseDir = pathModule.resolve(baseDir);
const resolvedPath = pathModule.resolve(baseDir, relativePath);
// Handles the case where resolvedBaseDir doesn't end with a
// path separator. For example, if
// resolvedBaseDir="/foo/bar"
// then we could have
// resolvedPath="/foo/bar2"
// which is not within the "/foo/bar" directory.
//
// We can't do this if the two paths are already equal as (as this would cause
// resolvedPath to no longer start with resolvedBaseDir).
if (!resolvedBaseDir.endsWith(pathModule.sep) && resolvedBaseDir !== resolvedPath) {
resolvedBaseDir += pathModule.sep;
}
if (!resolvedPath.startsWith(resolvedBaseDir)) {
return null;
}
return resolvedPath;
};
export default resolvePathWithinDir;