resolveSafePath method

String resolveSafePath({
  1. required String projectRootPath,
  2. required String relativePath,
})

Resolves relativePath against projectRootPath and enforces root bounds.

Throws FileUtilsException if the resolved path escapes the project root.

Implementation

String resolveSafePath({
  required String projectRootPath,
  required String relativePath,
}) {
  final normalizedRoot = p.normalize(p.absolute(projectRootPath));
  final candidate = p.normalize(p.join(normalizedRoot, relativePath));

  if (!_isWithinRoot(normalizedRoot, candidate)) {
    throw FileUtilsException(
      'Path traversal detected: "$relativePath" resolves outside project root.',
    );
  }
  return candidate;
}