From 7bcc585465cd637d8cec502677338f99f90c0b3a Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 30 Nov 2021 11:27:53 +0100 Subject: [PATCH] Merge pull request #5949 from overleaf/tm-doc-rev-nan-errors Add error for if doc revision is NaN when we check revs GitOrigin-RevId: 22149c506c9fe1604c76e92b40ac23aca6c40f81 --- services/docstore/app/js/Errors.js | 3 +++ services/docstore/app/js/MongoManager.js | 9 +++++++ .../test/unit/js/MongoManagerTests.js | 25 +++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/services/docstore/app/js/Errors.js b/services/docstore/app/js/Errors.js index 3cf5ad74a4..580ce9df5d 100644 --- a/services/docstore/app/js/Errors.js +++ b/services/docstore/app/js/Errors.js @@ -6,8 +6,11 @@ class Md5MismatchError extends OError {} class DocModifiedError extends OError {} +class DocRevValueError extends OError {} + module.exports = { Md5MismatchError, DocModifiedError, + DocRevValueError, ...Errors, } diff --git a/services/docstore/app/js/MongoManager.js b/services/docstore/app/js/MongoManager.js index c8ec93cb9b..a26da468ad 100644 --- a/services/docstore/app/js/MongoManager.js +++ b/services/docstore/app/js/MongoManager.js @@ -203,6 +203,15 @@ module.exports = MongoManager = { if (err) return callback(err) MongoManager.getDocRev(doc._id, function (err, currentRev) { if (err) return callback(err) + if (isNaN(currentRev) || isNaN(doc.rev)) { + return callback( + new Errors.DocRevValueError('doc rev is NaN', { + doc_id: doc._id, + rev: doc.rev, + currentRev, + }) + ) + } if (doc.rev !== currentRev) { return callback( new Errors.DocModifiedError('doc rev has changed', { diff --git a/services/docstore/test/unit/js/MongoManagerTests.js b/services/docstore/test/unit/js/MongoManagerTests.js index 871bdf53e4..40ff99f151 100644 --- a/services/docstore/test/unit/js/MongoManagerTests.js +++ b/services/docstore/test/unit/js/MongoManagerTests.js @@ -371,5 +371,30 @@ describe('MongoManager', function () { } ) }) + + it('should return a value error if incoming rev is NaN', function (done) { + this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: 2 }) + this.doc = { _id: ObjectId(), name: 'mock-doc', rev: NaN } + this.MongoManager.withRevCheck( + this.doc, + this.testFunction, + (err, result) => { + err.should.be.instanceof(Errors.DocRevValueError) + done() + } + ) + }) + + it('should return a value error if checked doc rev is NaN', function (done) { + this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: NaN }) + this.MongoManager.withRevCheck( + this.doc, + this.testFunction, + (err, result) => { + err.should.be.instanceof(Errors.DocRevValueError) + done() + } + ) + }) }) })