( packageDirectory: string, filePath: string )
| 130 | * Validates that package file ancestors and the file itself are safe. |
| 131 | */ |
| 132 | export async function getUnsafePackageFileReason( |
| 133 | packageDirectory: string, |
| 134 | filePath: string |
| 135 | ): Promise<string | null> { |
| 136 | const directoriesToCheck = getDirectoriesBetween( |
| 137 | packageDirectory, |
| 138 | path.dirname(filePath) |
| 139 | ); |
| 140 | |
| 141 | if (directoriesToCheck.length === 0) { |
| 142 | return `${filePath} resolves outside package`; |
| 143 | } |
| 144 | |
| 145 | for (const directory of directoriesToCheck) { |
| 146 | const reason = await getUnsafeDirectoryReason(directory); |
| 147 | |
| 148 | if (reason) { |
| 149 | return `${directory} is ${reason}`; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | const reason = await getUnsafeFileReason(filePath); |
| 154 | |
| 155 | return reason ? `${filePath} is ${reason}` : null; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Reports unsafe ownership or write permissions for a directory path. |
no test coverage detected