From ded9f31fe85054a02e47a9147ff991830a8ca6a4 Mon Sep 17 00:00:00 2001 From: "Aaron K. Clark" Date: Tue, 19 May 2026 04:32:38 -0500 Subject: [PATCH] =?UTF-8?q?fix(worker):=20return=20404=20(not=20403)=20on?= =?UTF-8?q?=20cross-tenant=20access=20=E2=80=94=20close=20tenant-enumerati?= =?UTF-8?q?on=20leak?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Same class of bug as /v1/company (#174) and /v1/billingtype (#188), applied to the Worker controller's getById/update/remove handlers. A scoped (non-master) caller asking about a `workerId` belonging to another tenant got 403, while a non-existent id returned 404 — letting them iterate workerId values and learn which ids are populated across the whole tenant table by status code alone. Collapse both cases into 404 with the same `"Not found."` body. Master- key callers continue to see every worker; own-tenant 200 path unchanged. Pinned in `tests/api/worker.test.js` as three controller-level unit tests (one per handler), driving the controller directly with stubbed `Worker.findByPk` returning a row in tenant 99 while spying on `auth.isMaster` / `auth.getCompanyId` to make the caller appear scoped to tenant 7. Follow-ups for the other entities (customer, invoice, job, …) will land in separate PRs — one entity per PR for focused diffs. Co-Authored-By: Claude Opus 4.7 (1M context) --- app/controllers/workercontroller.js | 13 +++-- tests/api/worker.test.js | 84 +++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/app/controllers/workercontroller.js b/app/controllers/workercontroller.js index ee3602b..dc3ff7a 100644 --- a/app/controllers/workercontroller.js +++ b/app/controllers/workercontroller.js @@ -112,8 +112,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 Worker ids are + // populated across the whole tenant table by status code. + // Same secure-404 pattern landed for /v1/company in #174 and + // /v1/billingtype in #188. if (companyId === -1 || worker.workerCompId !== 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.", worker }); @@ -199,8 +204,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 || worker.workerCompId !== companyId) { - return res.status(403).json({ message: "Invalid Authorization Key." }); + return res.status(404).json({ message: "Not found." }); } } @@ -247,8 +253,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 || worker.workerCompId !== companyId) { - return res.status(403).json({ message: "Invalid Authorization Key." }); + return res.status(404).json({ message: "Not found." }); } } diff --git a/tests/api/worker.test.js b/tests/api/worker.test.js index 0d48533..140fcee 100644 --- a/tests/api/worker.test.js +++ b/tests/api/worker.test.js @@ -144,3 +144,87 @@ describe('Worker body validation', () => { expect(res.status).toBe(400); }); }); + +describe('Worker tenant-enumeration defense (secure 404)', () => { + // Same pattern as the billingtype/company secure-404 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/workercontroller.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.Worker.findByPk = vi.fn().mockResolvedValue({ + workerId: 99, workerCompId: 99, workerArch: 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/workercontroller.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.Worker.findByPk = vi.fn().mockResolvedValue({ + workerId: 99, workerCompId: 99, workerArch: false, update: vi.fn(), + }); + const req = { + get: (h) => (h === 'authKey' ? 'scoped-to-7' : undefined), + params: { id: 99 }, + body: { workerFName: '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/workercontroller.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.Worker.findByPk = vi.fn().mockResolvedValue({ + workerId: 99, workerCompId: 99, workerArch: 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(); + } + }); +});