From bfc7060d6b219f053a468a839aa534b9c219f280 Mon Sep 17 00:00:00 2001 From: alxndrsn Date: Fri, 12 Apr 2024 12:02:40 +0000 Subject: [PATCH] db.post(), db.bulkDocs(): throw INVALID_REV consistently Follow-up to https://github.com/pouchdb/pouchdb/pull/8931 --- .../node_modules/pouchdb-core/src/adapter.js | 6 +- tests/integration/test.basics.js | 65 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/packages/node_modules/pouchdb-core/src/adapter.js b/packages/node_modules/pouchdb-core/src/adapter.js index d8b7fbe37a..b3d204125a 100644 --- a/packages/node_modules/pouchdb-core/src/adapter.js +++ b/packages/node_modules/pouchdb-core/src/adapter.js @@ -785,9 +785,13 @@ class AbstractPouchDB extends EventEmitter { } for (var i = 0; i < req.docs.length; ++i) { - if (typeof req.docs[i] !== 'object' || Array.isArray(req.docs[i])) { + const doc = req.docs[i]; + if (typeof doc !== 'object' || Array.isArray(doc)) { return callback(createError(NOT_AN_OBJECT)); } + if ('_rev' in doc && !isValidRev(doc._rev)) { + return callback(createError(INVALID_REV)); + } } var attachmentError; diff --git a/tests/integration/test.basics.js b/tests/integration/test.basics.js index b86187c322..088a89b573 100644 --- a/tests/integration/test.basics.js +++ b/tests/integration/test.basics.js @@ -293,6 +293,25 @@ adapters.forEach(function (adapter) { ({ rev }) => ({ toString:() => rev, indexOf:() => 12, substring:'hi' }), ({ rev }) => ({ toString:() => rev, indexOf:() => 12, substring:() => 'hi' }), ].forEach((generateRev, idx) => { + it(`post doc with illegal rev value #${idx}`, async () => { + const db = new PouchDB(dbs.name); + + let threw; + try { + await db.post({ + _rev: generateRev({ rev:'1-valid' }), + another: 'test' + }); + } catch (err) { + threw = true; + err.message.should.equal('Invalid rev format'); // TODO should be err.reason? + } + + if (!threw) { + throw new Error('db.put() should have thrown.'); + } + }); + it(`Modify a doc with illegal rev value #${idx}`, async () => { const db = new PouchDB(dbs.name); @@ -314,6 +333,52 @@ adapters.forEach(function (adapter) { throw new Error('db.put() should have thrown.'); } }); + + it(`bulkDocs with illegal rev value #${idx} (existing doc)`, async () => { + const db = new PouchDB(dbs.name); + + const info = await db.post({ test: 'somestuff' }); + + let threw; + try { + await db.bulkDocs({ + docs: [ { + _id: info.id, + _rev: generateRev(info), + another: 'test' + } ], + }); + } catch (err) { + threw = true; + err.message.should.equal('Invalid rev format'); // TODO should be err.reason? + } + + if (!threw) { + throw new Error('db.put() should have thrown.'); + } + }); + + it(`bulkDocs with illegal rev value #${idx} (new doc)`, async () => { + const db = new PouchDB(dbs.name); + + let threw; + try { + await db.bulkDocs({ + docs: [ { + _id: '1', + _rev: generateRev({ rev:'1_valid' }), + another: 'test' + } ], + }); + } catch (err) { + threw = true; + err.message.should.equal('Invalid rev format'); // TODO should be err.reason? + } + + if (!threw) { + throw new Error('db.put() should have thrown.'); + } + }); }); it('Remove doc', function (done) {