MCPcopy Create free account
hub / github.com/Kong/insomnia / assertPathAllowed

Function assertPathAllowed

packages/insomnia/src/main/secure-read-file.ts:79–95  ·  view source on GitHub ↗
(filePath: string, userAllowList?: string[])

Source from the content-addressed store, hash-verified

77// Allowlist + realpath + reserved-database-file enforcement for script/template/plugin-steerable
78// file reads. Returns the resolved real path to read from, so what gets read is what was checked.
79export const assertPathAllowed = async (filePath: string, userAllowList?: string[]): Promise<string> => {
80 const allowList = userAllowList ?? (await services.settings.getOrCreate()).dataFolders;
81 const { isAllowed, securedPath } = isPathAllowed(filePath, allowList);
82 invariant(isAllowed && !isReservedDatabaseFile(securedPath), cannotAccessFileError(securedPath));
83
84 // Re-check against the resolved real path (of both the target file and the allowed roots,
85 // since e.g. macOS symlinks /var to /private/var) so a symlink can't be used to point an
86 // allowed directory at a reserved database file or a location outside the allowlist.
87 const realPath = await resolveRealPath(securedPath);
88 const realAllowList = await Promise.all(getSecuredFolderAllowList(allowList).map(resolveRealPath));
89 const { isAllowed: isRealPathAllowed } = isPathAllowed(realPath, realAllowList);
90 invariant(
91 isRealPathAllowed && !isReservedDatabaseFile(realPath) && !(await isDatabaseFileAlias(realPath)),
92 cannotAccessFileError(securedPath),
93 );
94 return realPath;
95};
96
97// For reading files specified by plugins, environment variables, and scripts which could come from an imported collection
98export const secureReadFile = async (filePath: string): Promise<string> => {

Callers 2

parseRequestBodyPathFunction · 0.90
secureReadFileFunction · 0.85

Calls 9

invariantFunction · 0.90
isPathAllowedFunction · 0.85
isReservedDatabaseFileFunction · 0.85
cannotAccessFileErrorFunction · 0.85
resolveRealPathFunction · 0.85
isDatabaseFileAliasFunction · 0.85
mapMethod · 0.80
allMethod · 0.65

Tested by

no test coverage detected