From 2d17af02f6f64b296071f172ce0b8cf848907270 Mon Sep 17 00:00:00 2001 From: "Aaron K. Clark" Date: Tue, 19 May 2026 06:33:49 -0500 Subject: [PATCH] =?UTF-8?q?fix(invoicejob):=20return=20404=20(not=20403)?= =?UTF-8?q?=20on=20cross-tenant=20access=20=E2=80=94=20close=20tenant-enum?= =?UTF-8?q?eration=20leak?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Same class as the 12 prior secure-404 fixes, applied to the job-cascade-scoped InvoiceJob: injbJobId → Job.jobCustId → Customer.custCompId Collapse 403 "exists but not yours" into 404 "Not found." so scoped callers can't enumerate `injbId` populations across the whole tenant table by status code. Pinned in `tests/api/invoicejob.test.js` with three controller- level unit tests spying on `auth.getCompanyIdByJobId`. Co-Authored-By: Claude Opus 4.7 (1M context) --- app/controllers/invoicejobcontroller.js | 13 +++- tests/api/invoicejob.test.js | 89 +++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 3 deletions(-) diff --git a/app/controllers/invoicejobcontroller.js b/app/controllers/invoicejobcontroller.js index 25ce49c..e159fb0 100644 --- a/app/controllers/invoicejobcontroller.js +++ b/app/controllers/invoicejobcontroller.js @@ -79,8 +79,13 @@ exports.getById = async (req, res) => { if (!isMaster) { const authCompanyId = await GetCompanyId(authKey); const jobCompanyId = await GetCompanyIdByJobId(invoiceJob.injbJobId); + // Cross-tenant access is reported as 404, not 403 — otherwise + // a scoped caller can enumerate which InvoiceJob ids are + // populated across the whole tenant table by status code. + // Same secure-404 pattern as the prior 12 entities (#174 + // through #230). if (authCompanyId === -1 || jobCompanyId === -1 || authCompanyId !== jobCompanyId) { - return res.status(403).json({ message: "Invalid Authorization Key." }); + return res.status(404).json({ message: "Not found." }); } } return res.status(200).json({ message: "Found.", invoiceJob }); @@ -171,8 +176,9 @@ exports.update = async (req, res) => { if (!isMaster) { const authCompanyId = await GetCompanyId(authKey); const jobCompanyId = await GetCompanyIdByJobId(invoiceJob.injbJobId); + // Secure-404 on PATCH for the same reason as GET. if (authCompanyId === -1 || jobCompanyId === -1 || authCompanyId !== jobCompanyId) { - return res.status(403).json({ message: "Invalid Authorization Key." }); + return res.status(404).json({ message: "Not found." }); } } @@ -215,8 +221,9 @@ exports.remove = async (req, res) => { if (!isMaster) { const authCompanyId = await GetCompanyId(authKey); const jobCompanyId = await GetCompanyIdByJobId(invoiceJob.injbJobId); + // Secure-404 on DELETE for the same reason as GET / PATCH. if (authCompanyId === -1 || jobCompanyId === -1 || authCompanyId !== jobCompanyId) { - return res.status(403).json({ message: "Invalid Authorization Key." }); + return res.status(404).json({ message: "Not found." }); } } diff --git a/tests/api/invoicejob.test.js b/tests/api/invoicejob.test.js index 623db8c..57cdf9d 100644 --- a/tests/api/invoicejob.test.js +++ b/tests/api/invoicejob.test.js @@ -87,3 +87,92 @@ describe('InvoiceJob body validation', () => { expect(res.status).toBe(400); }); }); + +describe('InvoiceJob tenant-enumeration defense (secure 404)', () => { + // Job-cascade-scoped: injbJobId → Job.jobCustId → Customer.custCompId. + // Spy on getCompanyIdByJobId (the helper walks job→customer→company). + 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/invoicejobcontroller.js'); + const isMasterSpy = vi.spyOn(auth, 'isMaster').mockResolvedValue(false); + const getCompanyIdSpy = vi.spyOn(auth, 'getCompanyId').mockResolvedValue(7); + const getCompanyIdByJobIdSpy = vi.spyOn(auth, 'getCompanyIdByJobId').mockResolvedValue(99); + try { + const db = require('../../app/config/db.config.js'); + db.InvoiceJob.findByPk = vi.fn().mockResolvedValue({ + injbId: 42, injbJobId: 13, injbArch: 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(); + getCompanyIdByJobIdSpy.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/invoicejobcontroller.js'); + const isMasterSpy = vi.spyOn(auth, 'isMaster').mockResolvedValue(false); + const getCompanyIdSpy = vi.spyOn(auth, 'getCompanyId').mockResolvedValue(7); + const getCompanyIdByJobIdSpy = vi.spyOn(auth, 'getCompanyIdByJobId').mockResolvedValue(99); + try { + const db = require('../../app/config/db.config.js'); + db.InvoiceJob.findByPk = vi.fn().mockResolvedValue({ + injbId: 42, injbJobId: 13, injbArch: false, update: vi.fn(), + }); + const req = { + get: (h) => (h === 'authKey' ? 'scoped-to-7' : undefined), + params: { id: 42 }, + body: { injbAmount: 50 }, + }; + 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(); + getCompanyIdByJobIdSpy.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/invoicejobcontroller.js'); + const isMasterSpy = vi.spyOn(auth, 'isMaster').mockResolvedValue(false); + const getCompanyIdSpy = vi.spyOn(auth, 'getCompanyId').mockResolvedValue(7); + const getCompanyIdByJobIdSpy = vi.spyOn(auth, 'getCompanyIdByJobId').mockResolvedValue(99); + try { + const db = require('../../app/config/db.config.js'); + db.InvoiceJob.findByPk = vi.fn().mockResolvedValue({ + injbId: 42, injbJobId: 13, injbArch: 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(); + getCompanyIdByJobIdSpy.mockRestore(); + } + }); +});