From 1f6abd4e6941543b235a0d80f59a6860da688d47 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 25 Sep 2018 11:15:32 +0100 Subject: [PATCH] fix invalid project names when opening templates --- .../Features/Project/ProjectDetailsHandler.coffee | 10 ++++++++++ .../Features/Templates/TemplatesController.coffee | 5 ++++- .../Project/ProjectDetailsHandlerTests.coffee | 14 ++++++++++++++ .../Templates/TemplatesControllerTests.coffee | 2 ++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/services/web/app/coffee/Features/Project/ProjectDetailsHandler.coffee b/services/web/app/coffee/Features/Project/ProjectDetailsHandler.coffee index 4946d8126e..4f3f9b6b1e 100644 --- a/services/web/app/coffee/Features/Project/ProjectDetailsHandler.coffee +++ b/services/web/app/coffee/Features/Project/ProjectDetailsHandler.coffee @@ -98,6 +98,16 @@ module.exports = ProjectDetailsHandler = return callback(null, candidateName, true) # we couldn't make the name unique, something is wrong return callback new Errors.InvalidNameError("Project name could not be made unique") + + fixProjectName: (name) -> + if name == "" + name = "Untitled" + if name.indexOf('/') > -1 + # v2 does not allow / in a project name + name = name.replace(/\//g, '-') + if name.length > @MAX_PROJECT_NAME_LENGTH + name = name.substr(0, @MAX_PROJECT_NAME_LENGTH) + return name setPublicAccessLevel : (project_id, newAccessLevel, callback = ->)-> logger.log project_id: project_id, level: newAccessLevel, "set public access level" diff --git a/services/web/app/coffee/Features/Templates/TemplatesController.coffee b/services/web/app/coffee/Features/Templates/TemplatesController.coffee index 724abd2766..742ee4a7d1 100644 --- a/services/web/app/coffee/Features/Templates/TemplatesController.coffee +++ b/services/web/app/coffee/Features/Templates/TemplatesController.coffee @@ -3,6 +3,7 @@ Project = require('../../../js/models/Project').Project ProjectUploadManager = require('../../../js/Features/Uploads/ProjectUploadManager') ProjectOptionsHandler = require('../../../js/Features/Project/ProjectOptionsHandler') ProjectRootDocManager = require('../../../js/Features/Project/ProjectRootDocManager') +ProjectDetailsHandler = require('../../../js/Features/Project/ProjectDetailsHandler') AuthenticationController = require('../../../js/Features/Authentication/AuthenticationController') settings = require('settings-sharelatex') fs = require('fs') @@ -55,6 +56,8 @@ module.exports = TemplatesController = ) createFromZip: (zipReq, options, req, res)-> + # remove any invalid characters from template name + projectName = ProjectDetailsHandler.fixProjectName(options.templateName) dumpPath = "#{settings.path.dumpFolder}/#{uuid.v4()}" writeStream = fs.createWriteStream(dumpPath) @@ -62,7 +65,7 @@ module.exports = TemplatesController = logger.error err: error, "error getting zip from template API" zipReq.pipe(writeStream) writeStream.on 'close', -> - ProjectUploadManager.createProjectFromZipArchive options.currentUserId, options.templateName, dumpPath, (err, project)-> + ProjectUploadManager.createProjectFromZipArchive options.currentUserId, projectName, dumpPath, (err, project)-> if err? logger.err err:err, zipReq:zipReq, "problem building project from zip" return res.sendStatus 500 diff --git a/services/web/test/unit/coffee/Project/ProjectDetailsHandlerTests.coffee b/services/web/test/unit/coffee/Project/ProjectDetailsHandlerTests.coffee index 7fadf0b12c..0dd7d11ac8 100644 --- a/services/web/test/unit/coffee/Project/ProjectDetailsHandlerTests.coffee +++ b/services/web/test/unit/coffee/Project/ProjectDetailsHandlerTests.coffee @@ -196,6 +196,20 @@ describe 'ProjectDetailsHandler', -> expect(error).to.eql new Errors.InvalidNameError("Project name could not be made unique") done() + describe "fixProjectName", -> + + it "should change empty names to Untitled", () -> + expect(@handler.fixProjectName "").to.equal "Untitled" + + it "should replace / with -", () -> + expect(@handler.fixProjectName "foo/bar").to.equal "foo-bar" + + it "should truncate long names", () -> + expect(@handler.fixProjectName new Array(1000).join("a")).to.equal "a".repeat(150) + + it "should accept normal names", () -> + expect(@handler.fixProjectName "foobar").to.equal "foobar" + describe "setPublicAccessLevel", -> beforeEach -> diff --git a/services/web/test/unit/coffee/Templates/TemplatesControllerTests.coffee b/services/web/test/unit/coffee/Templates/TemplatesControllerTests.coffee index 776e177244..a08789ede9 100644 --- a/services/web/test/unit/coffee/Templates/TemplatesControllerTests.coffee +++ b/services/web/test/unit/coffee/Templates/TemplatesControllerTests.coffee @@ -32,12 +32,14 @@ describe 'TemplatesController', -> } @ProjectDetailsHandler = getProjectDescription:sinon.stub() + fixProjectName: sinon.stub().returns(@templateName) @Project = update: sinon.stub().callsArgWith(3, null) @controller = SandboxedModule.require modulePath, requires: '../../../js/Features/Uploads/ProjectUploadManager':@ProjectUploadManager '../../../js/Features/Project/ProjectOptionsHandler':@ProjectOptionsHandler '../../../js/Features/Project/ProjectRootDocManager':@ProjectRootDocManager + '../../../js/Features/Project/ProjectDetailsHandler':@ProjectDetailsHandler '../../../js/Features/Authentication/AuthenticationController': @AuthenticationController = {getLoggedInUserId: sinon.stub()} './TemplatesPublisher':@TemplatesPublisher "logger-sharelatex":