(filePath: string, userAllowList?: string[])
| 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. |
| 79 | export 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 |
| 98 | export const secureReadFile = async (filePath: string): Promise<string> => { |
no test coverage detected