From b25b30824cb7a100ce865fd8084617756bffa3e7 Mon Sep 17 00:00:00 2001 From: "Aaron K. Clark" Date: Tue, 19 May 2026 05:07:31 -0500 Subject: [PATCH] =?UTF-8?q?fix(inventorytransaction):=20return=20404=20(no?= =?UTF-8?q?t=20403)=20on=20cross-tenant=20access=20=E2=80=94=20close=20ten?= =?UTF-8?q?ant-enumeration=20leak?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Same class as #174 (company), #188 (billingtype), #192 (worker), #196 (inventoryitem), #200 (purchaseordervendor) — applied to the InventoryTransaction controller's getById / update / remove handlers. Scoped callers were getting 403 for existing-but-not-yours and 404 for absent ids, which let them enumerate `invtId` populations across the whole tenant table by status code. Collapse both cases into 404. Master + own-tenant paths unchanged. Pinned in `tests/api/inventorytransaction.test.js` with three controller-level unit tests using stubbed `findByPk` + spied auth helpers. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../inventorytransactioncontroller.js | 14 +++- tests/api/inventorytransaction.test.js | 84 +++++++++++++++++++ 2 files changed, 95 insertions(+), 3 deletions(-) diff --git a/app/controllers/inventorytransactioncontroller.js b/app/controllers/inventorytransactioncontroller.js index 6a5af87..3ac955b 100644 --- a/app/controllers/inventorytransactioncontroller.js +++ b/app/controllers/inventorytransactioncontroller.js @@ -93,8 +93,14 @@ exports.getById = async (req, res) => { const isMaster = await IsMaster(authKey); if (!isMaster) { const companyId = await GetCompanyId(authKey); + // Cross-tenant access is reported as 404, not 403 — otherwise + // a scoped caller can enumerate which InventoryTransaction + // ids are populated across the whole tenant table by status + // code. Same secure-404 pattern as #174 (company), #188 + // (billingtype), #192 (worker), #196 (inventoryitem), + // #200 (purchaseordervendor). if (companyId === -1 || txn.invtCompanyId !== companyId) { - return res.status(403).json({ message: "Invalid Authorization Key." }); + return res.status(404).json({ message: "Not found." }); } } return res.status(200).json({ message: "Found.", inventoryTransaction: txn }); @@ -164,8 +170,9 @@ exports.update = async (req, res) => { const isMaster = await IsMaster(authKey); if (!isMaster) { const companyId = await GetCompanyId(authKey); + // Secure-404 on PATCH for the same reason as GET. if (companyId === -1 || txn.invtCompanyId !== companyId) { - return res.status(403).json({ message: "Invalid Authorization Key." }); + return res.status(404).json({ message: "Not found." }); } } @@ -207,8 +214,9 @@ exports.remove = async (req, res) => { const isMaster = await IsMaster(authKey); if (!isMaster) { const companyId = await GetCompanyId(authKey); + // Secure-404 on DELETE for the same reason as GET / PATCH. if (companyId === -1 || txn.invtCompanyId !== companyId) { - return res.status(403).json({ message: "Invalid Authorization Key." }); + return res.status(404).json({ message: "Not found." }); } } diff --git a/tests/api/inventorytransaction.test.js b/tests/api/inventorytransaction.test.js index ef41116..90fc21f 100644 --- a/tests/api/inventorytransaction.test.js +++ b/tests/api/inventorytransaction.test.js @@ -63,3 +63,87 @@ describe('InventoryTransaction body validation', () => { expect(res.status).toBe(400); }); }); + +describe('InventoryTransaction tenant-enumeration defense (secure 404)', () => { + // Same pattern as the company/billingtype/worker/inventoryitem/ + // purchaseordervendor secure-404 tests: drive the controller + // directly with stubbed Model + spied auth helpers. + test('controller getById: existing-but-not-yours returns 404 to non-master', async () => { + const auth = require('../../app/middleware/auth.js'); + const controller = require('../../app/controllers/inventorytransactioncontroller.js'); + const isMasterSpy = vi.spyOn(auth, 'isMaster').mockResolvedValue(false); + const getCompanyIdSpy = vi.spyOn(auth, 'getCompanyId').mockResolvedValue(7); + try { + const db = require('../../app/config/db.config.js'); + db.InventoryTransaction.findByPk = vi.fn().mockResolvedValue({ + invtId: 99, invtCompanyId: 99, invtArch: false, + }); + const req = { get: (h) => (h === 'authKey' ? 'scoped-to-7' : undefined), params: { id: 99 } }; + let captured = null; + const res = { + status(code) { this._code = code; return this; }, + json(body) { captured = { code: this._code, body }; return this; }, + }; + await controller.getById(req, res); + expect(captured.code).toBe(404); + expect(captured.body.message).toMatch(/not found/i); + } finally { + isMasterSpy.mockRestore(); + getCompanyIdSpy.mockRestore(); + } + }); + + test('controller update: existing-but-not-yours returns 404 to non-master', async () => { + const auth = require('../../app/middleware/auth.js'); + const controller = require('../../app/controllers/inventorytransactioncontroller.js'); + const isMasterSpy = vi.spyOn(auth, 'isMaster').mockResolvedValue(false); + const getCompanyIdSpy = vi.spyOn(auth, 'getCompanyId').mockResolvedValue(7); + try { + const db = require('../../app/config/db.config.js'); + db.InventoryTransaction.findByPk = vi.fn().mockResolvedValue({ + invtId: 99, invtCompanyId: 99, invtArch: false, update: vi.fn(), + }); + const req = { + get: (h) => (h === 'authKey' ? 'scoped-to-7' : undefined), + params: { id: 99 }, + body: { invtDirection: 1 }, + }; + let captured = null; + const res = { + status(code) { this._code = code; return this; }, + json(body) { captured = { code: this._code, body }; return this; }, + }; + await controller.update(req, res); + expect(captured.code).toBe(404); + expect(captured.body.message).toMatch(/not found/i); + } finally { + isMasterSpy.mockRestore(); + getCompanyIdSpy.mockRestore(); + } + }); + + test('controller remove: existing-but-not-yours returns 404 to non-master', async () => { + const auth = require('../../app/middleware/auth.js'); + const controller = require('../../app/controllers/inventorytransactioncontroller.js'); + const isMasterSpy = vi.spyOn(auth, 'isMaster').mockResolvedValue(false); + const getCompanyIdSpy = vi.spyOn(auth, 'getCompanyId').mockResolvedValue(7); + try { + const db = require('../../app/config/db.config.js'); + db.InventoryTransaction.findByPk = vi.fn().mockResolvedValue({ + invtId: 99, invtCompanyId: 99, invtArch: false, update: vi.fn(), + }); + const req = { get: (h) => (h === 'authKey' ? 'scoped-to-7' : undefined), params: { id: 99 } }; + let captured = null; + const res = { + status(code) { this._code = code; return this; }, + json(body) { captured = { code: this._code, body }; return this; }, + }; + await controller.remove(req, res); + expect(captured.code).toBe(404); + expect(captured.body.message).toMatch(/not found/i); + } finally { + isMasterSpy.mockRestore(); + getCompanyIdSpy.mockRestore(); + } + }); +});