From f9edc02aff49c664780b7fc554fefb64c19f8775 Mon Sep 17 00:00:00 2001 From: "Aaron K. Clark" Date: Tue, 19 May 2026 04:55:38 -0500 Subject: [PATCH] =?UTF-8?q?fix(purchaseordervendor):=20return=20404=20(not?= =?UTF-8?q?=20403)=20on=20cross-tenant=20access=20=E2=80=94=20close=20tena?= =?UTF-8?q?nt-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), applied to PurchaseOrderVendor's getById / update / remove handlers. Collapse "exists but not yours" into 404 so a scoped caller can't enumerate `povId` populations by status code. Pinned in `tests/api/purchaseordervendor.test.js` with three controller-level unit tests using stubbed `findByPk` + spied auth helpers (auth.isMaster, auth.getCompanyId). Co-Authored-By: Claude Opus 4.7 (1M context) --- .../purchaseordervendorcontroller.js | 13 ++- tests/api/purchaseordervendor.test.js | 84 +++++++++++++++++++ 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/app/controllers/purchaseordervendorcontroller.js b/app/controllers/purchaseordervendorcontroller.js index 59f5517..0052c2a 100644 --- a/app/controllers/purchaseordervendorcontroller.js +++ b/app/controllers/purchaseordervendorcontroller.js @@ -106,8 +106,13 @@ 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 PurchaseOrderVendor ids + // are populated across the whole tenant table by status code. + // Same secure-404 pattern as #174 (company), #188 (billingtype), + // #192 (worker), #196 (inventoryitem). if (companyId === -1 || vendor.povCompId !== 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.", purchaseOrderVendor: vendor }); @@ -179,8 +184,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 || vendor.povCompId !== companyId) { - return res.status(403).json({ message: "Invalid Authorization Key." }); + return res.status(404).json({ message: "Not found." }); } } @@ -222,8 +228,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 || vendor.povCompId !== companyId) { - return res.status(403).json({ message: "Invalid Authorization Key." }); + return res.status(404).json({ message: "Not found." }); } } diff --git a/tests/api/purchaseordervendor.test.js b/tests/api/purchaseordervendor.test.js index f0117e4..e721f46 100644 --- a/tests/api/purchaseordervendor.test.js +++ b/tests/api/purchaseordervendor.test.js @@ -64,3 +64,87 @@ describe('PurchaseOrderVendor body validation', () => { expect(res.status).toBe(400); }); }); + +describe('PurchaseOrderVendor tenant-enumeration defense (secure 404)', () => { + // Same pattern as worker/billingtype/inventoryitem/company tests: + // drive the controller directly with stubbed Model + spied auth + // helpers so we don't have to wire every upstream middleware. + 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/purchaseordervendorcontroller.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.PurchaseOrderVendor.findByPk = vi.fn().mockResolvedValue({ + povId: 99, povCompId: 99, povArch: 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/purchaseordervendorcontroller.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.PurchaseOrderVendor.findByPk = vi.fn().mockResolvedValue({ + povId: 99, povCompId: 99, povArch: false, update: vi.fn(), + }); + const req = { + get: (h) => (h === 'authKey' ? 'scoped-to-7' : undefined), + params: { id: 99 }, + body: { povName: 'X' }, + }; + 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/purchaseordervendorcontroller.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.PurchaseOrderVendor.findByPk = vi.fn().mockResolvedValue({ + povId: 99, povCompId: 99, povArch: 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(); + } + }); +});