From c90d89754a5bc4bb198502eb67c811ae48f96bf9 Mon Sep 17 00:00:00 2001 From: "Aaron K. Clark" Date: Tue, 19 May 2026 05:36:04 -0500 Subject: [PATCH] =?UTF-8?q?fix(purchaseorderline):=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 the seven prior secure-404 fixes (#174 / #188 / #192 / #196 / #200 / #204 / #210), applied to the two-level cascade-scoped PurchaseOrderLine entity: polpoh → PurchaseOrderHeader.pohPovId → PurchaseOrderVendor.povCompId Collapse 403 "exists but not yours" into 404 "Not found." so scoped callers can't enumerate `polId` populations across the whole tenant table by status code. Pinned in `tests/api/purchaseorderline.test.js` with three controller-level unit tests using stubbed `PurchaseOrderLine.findByPk` + spied `auth.isMaster` / `auth.getCompanyId` / `auth.getCompanyIdByPohId` (the helper that internally walks the header → vendor → company chain). Co-Authored-By: Claude Opus 4.7 (1M context) --- .../purchaseorderlinecontroller.js | 13 ++- tests/api/purchaseorderline.test.js | 90 +++++++++++++++++++ 2 files changed, 100 insertions(+), 3 deletions(-) diff --git a/app/controllers/purchaseorderlinecontroller.js b/app/controllers/purchaseorderlinecontroller.js index eb4d6fc..5a44c58 100644 --- a/app/controllers/purchaseorderlinecontroller.js +++ b/app/controllers/purchaseorderlinecontroller.js @@ -79,8 +79,13 @@ exports.getById = async (req, res) => { if (!isMaster) { const authCompanyId = await GetCompanyId(authKey); const headerCompanyId = await GetCompanyIdByPohId(line.polpoh); + // Cross-tenant access is reported as 404, not 403 — otherwise + // a scoped caller can enumerate which PurchaseOrderLine ids + // are populated across the whole tenant table by status code. + // Same secure-404 pattern as the prior 7 entities (#174 / #188 + // / #192 / #196 / #200 / #204 / #210). if (authCompanyId === -1 || headerCompanyId === -1 || authCompanyId !== headerCompanyId) { - return res.status(403).json({ message: "Invalid Authorization Key." }); + return res.status(404).json({ message: "Not found." }); } } return res.status(200).json({ message: "Found.", purchaseOrderLine: line }); @@ -152,8 +157,9 @@ exports.update = async (req, res) => { if (!isMaster) { const authCompanyId = await GetCompanyId(authKey); const headerCompanyId = await GetCompanyIdByPohId(line.polpoh); + // Secure-404 on PATCH for the same reason as GET. if (authCompanyId === -1 || headerCompanyId === -1 || authCompanyId !== headerCompanyId) { - return res.status(403).json({ message: "Invalid Authorization Key." }); + return res.status(404).json({ message: "Not found." }); } } @@ -196,8 +202,9 @@ exports.remove = async (req, res) => { if (!isMaster) { const authCompanyId = await GetCompanyId(authKey); const headerCompanyId = await GetCompanyIdByPohId(line.polpoh); + // Secure-404 on DELETE for the same reason as GET / PATCH. if (authCompanyId === -1 || headerCompanyId === -1 || authCompanyId !== headerCompanyId) { - return res.status(403).json({ message: "Invalid Authorization Key." }); + return res.status(404).json({ message: "Not found." }); } } diff --git a/tests/api/purchaseorderline.test.js b/tests/api/purchaseorderline.test.js index bbd86f8..792c201 100644 --- a/tests/api/purchaseorderline.test.js +++ b/tests/api/purchaseorderline.test.js @@ -102,3 +102,93 @@ describe('PurchaseOrderLine body validation', () => { expect(res.status).toBe(400); }); }); + +describe('PurchaseOrderLine tenant-enumeration defense (secure 404)', () => { + // Two-level cascade: polpoh → header.pohPovId → vendor.povCompId. + // Spy on getCompanyIdByPohId (which itself walks header→vendor) + // so the cascade resolves to a different company than the caller. + 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/purchaseorderlinecontroller.js'); + const isMasterSpy = vi.spyOn(auth, 'isMaster').mockResolvedValue(false); + const getCompanyIdSpy = vi.spyOn(auth, 'getCompanyId').mockResolvedValue(7); + const getCompanyIdByPohIdSpy = vi.spyOn(auth, 'getCompanyIdByPohId').mockResolvedValue(99); + try { + const db = require('../../app/config/db.config.js'); + db.PurchaseOrderLine.findByPk = vi.fn().mockResolvedValue({ + polId: 42, polpoh: 13, polArch: false, + }); + const req = { get: (h) => (h === 'authKey' ? 'scoped-to-7' : undefined), params: { id: 42 } }; + 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(); + getCompanyIdByPohIdSpy.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/purchaseorderlinecontroller.js'); + const isMasterSpy = vi.spyOn(auth, 'isMaster').mockResolvedValue(false); + const getCompanyIdSpy = vi.spyOn(auth, 'getCompanyId').mockResolvedValue(7); + const getCompanyIdByPohIdSpy = vi.spyOn(auth, 'getCompanyIdByPohId').mockResolvedValue(99); + try { + const db = require('../../app/config/db.config.js'); + db.PurchaseOrderLine.findByPk = vi.fn().mockResolvedValue({ + polId: 42, polpoh: 13, polArch: false, update: vi.fn(), + }); + const req = { + get: (h) => (h === 'authKey' ? 'scoped-to-7' : undefined), + params: { id: 42 }, + body: { polItemDesc: '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(); + getCompanyIdByPohIdSpy.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/purchaseorderlinecontroller.js'); + const isMasterSpy = vi.spyOn(auth, 'isMaster').mockResolvedValue(false); + const getCompanyIdSpy = vi.spyOn(auth, 'getCompanyId').mockResolvedValue(7); + const getCompanyIdByPohIdSpy = vi.spyOn(auth, 'getCompanyIdByPohId').mockResolvedValue(99); + try { + const db = require('../../app/config/db.config.js'); + db.PurchaseOrderLine.findByPk = vi.fn().mockResolvedValue({ + polId: 42, polpoh: 13, polArch: false, update: vi.fn(), + }); + const req = { get: (h) => (h === 'authKey' ? 'scoped-to-7' : undefined), params: { id: 42 } }; + 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(); + getCompanyIdByPohIdSpy.mockRestore(); + } + }); +});