From 240dc2e319b8390847852c8b3cc31913787b5701 Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 16 May 2014 10:52:31 +0100 Subject: [PATCH 1/3] Use crypto.randomBytes --- .../Features/PasswordReset/PasswordResetTokenHandler.coffee | 4 ++-- .../PasswordReset/PasswordResetTokenHandlerTests.coffee | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/services/web/app/coffee/Features/PasswordReset/PasswordResetTokenHandler.coffee b/services/web/app/coffee/Features/PasswordReset/PasswordResetTokenHandler.coffee index 0a58a0cd52..3f51fce291 100644 --- a/services/web/app/coffee/Features/PasswordReset/PasswordResetTokenHandler.coffee +++ b/services/web/app/coffee/Features/PasswordReset/PasswordResetTokenHandler.coffee @@ -2,7 +2,7 @@ Settings = require('settings-sharelatex') redis = require('redis') rclient = redis.createClient(Settings.redis.web.port, Settings.redis.web.host) rclient.auth(Settings.redis.web.password) -uuid = require("node-uuid") +crypto = require("crypto") logger = require("logger-sharelatex") ONE_HOUR_IN_S = 60 * 60 @@ -13,7 +13,7 @@ module.exports = getNewToken: (user_id, callback)-> logger.log user_id:user_id, "generating token for password reset" - token = uuid.v4() + token = crypto.randomBytes(32).toString("hex") multi = rclient.multi() multi.set buildKey(token), user_id multi.expire buildKey(token), ONE_HOUR_IN_S diff --git a/services/web/test/UnitTests/coffee/PasswordReset/PasswordResetTokenHandlerTests.coffee b/services/web/test/UnitTests/coffee/PasswordReset/PasswordResetTokenHandlerTests.coffee index 49d94c1a0f..3d74cf2d58 100644 --- a/services/web/test/UnitTests/coffee/PasswordReset/PasswordResetTokenHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/PasswordReset/PasswordResetTokenHandlerTests.coffee @@ -10,7 +10,7 @@ describe "PasswordResetTokenHandler", -> beforeEach -> @user_id = "user id here" - @stubbedToken = "dsajdiojlklksda" + @stubbedToken = require("crypto").randomBytes(32) @settings = redis: From e9c164dc7317ae86480ca1b705e2f0342201f2c7 Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 16 May 2014 11:01:36 +0100 Subject: [PATCH 2/3] Fix test that could never fail --- .../PasswordReset/PasswordResetTokenHandlerTests.coffee | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/services/web/test/UnitTests/coffee/PasswordReset/PasswordResetTokenHandlerTests.coffee b/services/web/test/UnitTests/coffee/PasswordReset/PasswordResetTokenHandlerTests.coffee index 3d74cf2d58..1cbbe7938c 100644 --- a/services/web/test/UnitTests/coffee/PasswordReset/PasswordResetTokenHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/PasswordReset/PasswordResetTokenHandlerTests.coffee @@ -21,7 +21,6 @@ describe "PasswordResetTokenHandler", -> del:sinon.stub() expire:sinon.stub() exec:sinon.stub() - @uuid = v4 : -> return @stubbedToken self = @ @PasswordResetTokenHandler = SandboxedModule.require modulePath, requires: "redis" : @@ -31,16 +30,16 @@ describe "PasswordResetTokenHandler", -> "settings-sharelatex":@settings "logger-sharelatex": log:-> - "node-uuid":@uuid + "crypto": randomBytes: () => @stubbedToken describe "getNewToken", -> it "should set a new token into redis with a ttl", (done)-> @redisMulti.exec.callsArgWith(0) - @PasswordResetTokenHandler.getNewToken @user_id, (err, token)=> - @redisMulti.set "password_token:#{@stubbedToken}", @user_id - @redisMulti.expire "password_token:#{@stubbedToken}", 60 * 60 + @PasswordResetTokenHandler.getNewToken @user_id, (err, token) => + @redisMulti.set.calledWith("password_token:#{@stubbedToken.toString("hex")}", @user_id).should.equal true + @redisMulti.expire.calledWith("password_token:#{@stubbedToken.toString("hex")}", 60 * 60).should.equal true done() it "should return if there was an error", (done)-> From 2104c69cb9475d52842d87530dd8439c08d7bbe5 Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 16 May 2014 11:26:58 +0100 Subject: [PATCH 3/3] Stream files into zip in series --- .../Features/Downloads/ProjectZipStreamManager.coffee | 3 ++- .../coffee/Downloads/ProjectZipStreamManagerTests.coffee | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/services/web/app/coffee/Features/Downloads/ProjectZipStreamManager.coffee b/services/web/app/coffee/Features/Downloads/ProjectZipStreamManager.coffee index 579fdc7b41..d7f5c4054c 100644 --- a/services/web/app/coffee/Features/Downloads/ProjectZipStreamManager.coffee +++ b/services/web/app/coffee/Features/Downloads/ProjectZipStreamManager.coffee @@ -46,5 +46,6 @@ module.exports = ProjectZipStreamManager = return callback(err) path = path.slice(1) if path[0] == "/" archive.append stream, name: path - callback() + stream.on "end", () -> + callback() async.series jobs, callback diff --git a/services/web/test/UnitTests/coffee/Downloads/ProjectZipStreamManagerTests.coffee b/services/web/test/UnitTests/coffee/Downloads/ProjectZipStreamManagerTests.coffee index 61c6c40936..ba03d7cbc4 100644 --- a/services/web/test/UnitTests/coffee/Downloads/ProjectZipStreamManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/Downloads/ProjectZipStreamManagerTests.coffee @@ -4,6 +4,7 @@ should = chai.should() expect = chai.expect modulePath = "../../../../app/js/Features/Downloads/ProjectZipStreamManager.js" SandboxedModule = require('sandboxed-module') +EventEmitter = require("events").EventEmitter describe "ProjectZipStreamManager", -> beforeEach -> @@ -112,14 +113,16 @@ describe "ProjectZipStreamManager", -> "/folder/picture.png": _id: "file-id-2" @streams = - "file-id-1" : "stream-mock-1" - "file-id-2" : "stream-mock-2" + "file-id-1" : new EventEmitter() + "file-id-2" : new EventEmitter() @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files) @archive.append = sinon.stub() @FileStoreHandler.getFileStream = (project_id, file_id, {}, callback) => callback null, @streams[file_id] sinon.spy @FileStoreHandler, "getFileStream" @ProjectZipStreamManager.addAllFilesToArchive @project_id, @archive, @callback + for path, stream of @streams + stream.emit "end" it "should get the files for the project", -> @ProjectEntityHandler.getAllFiles.calledWith(@project_id).should.equal true