( nodeModulesDirectory: string, packageDirectory: string )
| 103 | * Validates that each package directory segment is safe and under node_modules. |
| 104 | */ |
| 105 | export async function getUnsafePackageDirectoryReason( |
| 106 | nodeModulesDirectory: string, |
| 107 | packageDirectory: string |
| 108 | ): Promise<string | null> { |
| 109 | const directoriesToCheck = getDirectoriesBetween( |
| 110 | nodeModulesDirectory, |
| 111 | packageDirectory |
| 112 | ); |
| 113 | |
| 114 | if (directoriesToCheck.length === 0) { |
| 115 | return `${packageDirectory} resolves outside local node_modules`; |
| 116 | } |
| 117 | |
| 118 | for (const directory of directoriesToCheck) { |
| 119 | const reason = await getUnsafeDirectoryReason(directory); |
| 120 | |
| 121 | if (reason) { |
| 122 | return `${directory} is ${reason}`; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Validates that package file ancestors and the file itself are safe. |
no test coverage detected