Просмотр исходного кода

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
Thomas 4 лет назад
Родитель
Сommit
7bcc585465

+ 3 - 0
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,
 }

+ 9 - 0
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', {

+ 25 - 0
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()
+        }
+      )
+    })
   })
 })