Merge pull request #322 from sharelatex/bg-validate-filename-length-client

validate filename length client
This commit is contained in:
Brian Gough 2018-02-12 10:08:03 +00:00 committed by GitHub
commit 3c9eb07848
2 changed files with 34 additions and 3 deletions

View file

@ -631,6 +631,9 @@ module.exports = ProjectEntityHandler =
newPath =
fileSystem: "#{path.fileSystem}/#{element.name}"
mongo: path.mongo
# check if the path would be too long
if not SafePath.isAllowedLength newPath.fileSystem
return callback new Errors.InvalidNameError("path too long")
ProjectEntityHandler.checkValidElementName folder, element.name, (err) =>
return callback(err) if err?
id = element._id+''
@ -648,9 +651,6 @@ module.exports = ProjectEntityHandler =
checkValidElementName: (folder, name, callback = (err) ->) ->
# # # check if the path would be too long
# # if not SafePath.isAllowedLength "#{folder}/#{name}"
# # return callback new Error.InvalidNameError("path too long")
# check if the name is already taken by a doc, file or
# folder. If so, return an error "file already exists".
err = new Errors.InvalidNameError("file already exists")

View file

@ -1399,6 +1399,37 @@ describe 'ProjectEntityHandler', ->
@ProjectModel.findOneAndUpdate.called.should.equal false
done()
it "should error if element name contains invalid characters", (done)->
doc =
_id: ObjectId()
name: "something*bad"
@ProjectEntityHandler._putElement @project, @folder._id, doc, "doc", (err)=>
@ProjectModel.findOneAndUpdate.called.should.equal false
err.should.deep.equal new Errors.InvalidNameError("invalid element name")
done()
it "should error if element name is too long", (done)->
doc =
_id: ObjectId()
name: new Array(200).join("long-") + "something"
@ProjectEntityHandler._putElement @project, @folder._id, doc, "doc", (err)=>
@ProjectModel.findOneAndUpdate.called.should.equal false
err.should.deep.equal new Errors.InvalidNameError("invalid element name")
done()
it "should error if the folder name is too long", (done)->
@path =
mongo: "mongo.path",
fileSystem: new Array(200).join("subdir/") + "foo"
@projectLocator.findElement.callsArgWith(1, null, @folder, @path)
doc =
_id: ObjectId()
name: "something"
@ProjectEntityHandler._putElement @project, @folder._id, doc, "doc", (err)=>
@ProjectModel.findOneAndUpdate.called.should.equal false
err.should.deep.equal new Errors.InvalidNameError("path too long")
done()
it "should error if a document already exists with the same name", (done)->
doc =
_id: ObjectId()