devToolsFileFromPath method

File? devToolsFileFromPath(
  1. String relativePath
)

Returns a DevTools file from the given path.

Only files within ~/.flutter-devtools/ can be accessed.

Implementation

File? devToolsFileFromPath(String relativePath) {
  if (relativePath.contains('..') || path.isAbsolute(relativePath)) {
    // The passed in path should not be able to walk up the directory tree
    // outside of the `~/.flutter-devtools/` directory. It must also not be an
    // absolute path: `path.join()` discards the base directory when its
    // second argument is absolute, which would otherwise allow reading an
    // arbitrary file on disk (e.g. an absolute path to a credentials `.json`
    // file).
    return null;
  }

  _ensureDevToolsDirectory();
  final targetFile = file(path.join(devToolsDir, relativePath));
  // Defense in depth: ensure the resolved path is actually contained within
  // the DevTools directory.
  if (!path.isWithin(devToolsDir, targetFile.path)) return null;
  if (!targetFile.existsSync()) return null;
  return targetFile;
}