From 3a4d4540bbf277f55a2ccf4300e7040543d8ab05 Mon Sep 17 00:00:00 2001 From: Kevin Dangoor Date: Wed, 3 Dec 2014 09:24:01 -0500 Subject: [PATCH] Ensure that the baseDir for createNewItem is within the project. This is a fix for #9910. --- src/project/ProjectManager.js | 8 +------- src/project/ProjectModel.js | 24 ++++++++++++++++++++++++ test/spec/ProjectModel-test.js | 21 +++++++++++++++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/project/ProjectManager.js b/src/project/ProjectManager.js index 4baab1d5a77..ee83f1eb63d 100644 --- a/src/project/ProjectManager.js +++ b/src/project/ProjectManager.js @@ -1022,13 +1022,7 @@ define(function (require, exports, module) { * filename. */ function createNewItem(baseDir, initialName, skipRename, isFolder) { - if (typeof baseDir === "string") { - if (_.last(baseDir) !== "/") { - baseDir += "/"; - } - } else { - baseDir = baseDir.fullPath; - } + baseDir = model.getDirectoryInProject(baseDir); if (skipRename) { return model.createAtPath(baseDir + initialName, isFolder); diff --git a/src/project/ProjectModel.js b/src/project/ProjectModel.js index 3edebe012c6..a1402470cf6 100644 --- a/src/project/ProjectModel.js +++ b/src/project/ProjectModel.js @@ -373,6 +373,30 @@ define(function (require, exports, module) { } return absPath; }; + + /** + * Returns a valid directory within the project, either the path (or Directory object) + * provided or the project root. + * + * @param {string|Directory} path Directory path to verify against the project + * @return {string} A directory path within the project. + */ + ProjectModel.prototype.getDirectoryInProject = function (path) { + if (path && typeof path === "string") { + if (_.last(path) !== "/") { + path += "/"; + } + } else if (path && path.isDirectory) { + path = path.fullPath; + } else { + path = null; + } + + if (!path || (typeof path !== "string") || !this.isWithinProject(path)) { + path = this.projectRoot.fullPath; + } + return path; + }; /** * @private diff --git a/test/spec/ProjectModel-test.js b/test/spec/ProjectModel-test.js index 20dcd6da18f..1cb9d553bad 100644 --- a/test/spec/ProjectModel-test.js +++ b/test/spec/ProjectModel-test.js @@ -98,6 +98,27 @@ define(function (require, exports, module) { it("won't create a relative path to a file outside the project", function () { expect(pm.makeProjectRelativeIfPossible("/some/other/project/README.md")).toBe("/some/other/project/README.md"); }); + + it("will return a directory within the project", function () { + expect(pm.getDirectoryInProject("/foo/bar/project/baz/")).toBe("/foo/bar/project/baz/"); + expect(pm.getDirectoryInProject("/foo/bar/project/baz")).toBe("/foo/bar/project/baz/"); + expect(pm.getDirectoryInProject({ + fullPath: "/foo/bar/project/foo2/", + isDirectory: true + })).toBe("/foo/bar/project/foo2/"); + }); + + it("will default to project root when getDirectoryInProject", function () { + expect(pm.getDirectoryInProject()).toBe("/foo/bar/project/"); + expect(pm.getDirectoryInProject(null)).toBe("/foo/bar/project/"); + expect(pm.getDirectoryInProject("")).toBe("/foo/bar/project/"); + expect(pm.getDirectoryInProject({ + isFile: true, + isDirectory: false, + fullPath: "/foo/bar/project/README.txt" + })).toBe("/foo/bar/project/"); + expect(pm.getDirectoryInProject("/other/project/")).toBe("/foo/bar/project/"); + }); }); describe("All Files Cache", function () {