MCPcopy Index your code
hub / github.com/microsoft/vscode / validatePathWithinProject

Function validatePathWithinProject

test/unit/electron/index.js:272–288  ·  view source on GitHub ↗

* Validates that a file path is within the project root for security purposes. * @param {string} filePath - The file path to validate * @throws {Error} If the path is outside the project root

(filePath)

Source from the content-addressed store, hash-verified

270 * @throws {Error} If the path is outside the project root
271 */
272 function validatePathWithinProject(filePath) {
273 const projectRoot = path.join(__dirname, '../../..');
274 const resolvedPath = path.resolve(filePath);
275 const normalizedRoot = path.resolve(projectRoot);
276
277 // On Windows, paths are case-insensitive
278 const isWindows = process.platform === 'win32';
279 const rel = path.relative(
280 isWindows ? normalizedRoot.toLowerCase() : normalizedRoot,
281 isWindows ? resolvedPath.toLowerCase() : resolvedPath
282 );
283 if (rel.startsWith('..') || path.isAbsolute(rel)) {
284 const error = new Error(`Access denied: Path '${filePath}' is outside the project root`);
285 console.error(error.message);
286 throw error;
287 }
288 }
289
290 // Handle file reading for tests
291 ipcMain.handle('vscode:readFile', async (event, filePath) => {

Callers 1

index.jsFile · 0.85

Calls 6

startsWithMethod · 0.80
joinMethod · 0.65
resolveMethod · 0.65
relativeMethod · 0.65
isAbsoluteMethod · 0.65
errorMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…