From ab38258654c91cb33b5c1748c9138a7db5ff49bd Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 5 Nov 2021 14:59:28 +0000 Subject: [PATCH] Updating usage quota middleware to fix issue presented in #3258 where anything with an _id and a _rev is considered to exist in CouchDB, which won't always be the case. Handle the scenario of an external database ID and don't error. --- packages/server/src/middleware/usageQuota.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/server/src/middleware/usageQuota.js b/packages/server/src/middleware/usageQuota.js index c62f0078cd..2b189b8660 100644 --- a/packages/server/src/middleware/usageQuota.js +++ b/packages/server/src/middleware/usageQuota.js @@ -2,6 +2,10 @@ const CouchDB = require("../db") const usageQuota = require("../utilities/usageQuota") const env = require("../environment") const { getTenantId } = require("@budibase/auth/tenancy") +const { + isExternalTable, + isRowId: isExternalRowId, +} = require("../integrations/utils") // tenants without limits const EXCLUDED_TENANTS = ["bb", "default", "bbtest", "bbstaging"] @@ -46,14 +50,24 @@ module.exports = async (ctx, next) => { } // post request could be a save of a pre-existing entry if (ctx.request.body && ctx.request.body._id && ctx.request.body._rev) { + const usageId = ctx.request.body._id try { if (ctx.appId) { const db = new CouchDB(ctx.appId) - await db.get(ctx.request.body._id) + await db.get(usageId) } return next() } catch (err) { - ctx.throw(404, `${ctx.request.body._id} does not exist`) + if ( + isExternalTable(usageId) || + (ctx.request.body.tableId && + isExternalTable(ctx.request.body.tableId)) || + isExternalRowId(usageId) + ) { + return next() + } else { + ctx.throw(404, `${usageId} does not exist`) + } } }