import { readFileSync } from 'node:fs'; import { join } from 'node:path'; import { describe, expect, it } from 'vitest'; import { assertStorageKeyOwned, buildSignedStoragePathForCtx, readStorageKeyBranch } from '../src/services/storageObjectOwnership'; import { mapCustomerInvoiceListRow } from '../src/services/customerInvoiceWriteService'; import { mapSupplierInvoiceListRow } from '../src/services/supplierInvoiceWriteService'; /** T9 全链路归属:前缀授权、无 branchCode fail-closed、list mint 不跨租户、写入口有断言。 */ describe('T9 SEC-5 全链路归属', () => { it('前缀 key:同分支通过、跨分支 403、无 branchCode 403、super_admin 放行', async () => { await expect(assertStorageKeyOwned('invoices', 'BR001/supplier-invoices/a.pdf', { branchCode: 'BR001' })).resolves.toBe('BR001/supplier-invoices/a.pdf'); await expect(assertStorageKeyOwned('invoices', 'BR002/supplier-invoices/a.pdf', { branchCode: 'BR001' })).rejects.toMatchObject({ status: 403 }); await expect(assertStorageKeyOwned('invoices', 'BR001/a.pdf', { branchCode: null })).rejects.toMatchObject({ status: 403 }); await expect(assertStorageKeyOwned('invoices', 'BR002/a.pdf', { branchCode: 'BR001', isSuperAdmin: true })).resolves.toBe('BR002/a.pdf'); await expect(assertStorageKeyOwned('invoices', '../etc/passwd', { branchCode: 'BR001' })).rejects.toMatchObject({ status: 403 }); }); it('B1/B2:archive// 签名归属(同分支 200、跨分支 403)', () => { expect(buildSignedStoragePathForCtx({ branchCode: 'BR001' }, 'invoices', 'archive/BR001/2026-10.tar.gz')).toContain('/api/storage/invoices/public/'); expect(() => buildSignedStoragePathForCtx({ branchCode: 'BR001' }, 'invoices', 'archive/BR002/2026-10.tar.gz')).toThrow(); expect(() => buildSignedStoragePathForCtx({ branchCode: 'BR001' }, 'invoices', 'BR002/free-cost/x.jpg')).toThrow(); expect(() => buildSignedStoragePathForCtx({ branchCode: null }, 'invoices', 'BR001/x.jpg')).toThrow(); }); it('本系统 SUP… 分公司前缀同样参与归属判定(2026-10-21)', async () => { await expect( assertStorageKeyOwned('quality-reports', 'SUP35143228/abc123.pdf', { branchCode: 'SUP35143228' }) ).resolves.toBe('SUP35143228/abc123.pdf'); await expect( assertStorageKeyOwned('quality-reports', 'SUP37831443/abc123.pdf', { branchCode: 'SUP35143228' }) ).rejects.toMatchObject({ status: 403 }); expect( buildSignedStoragePathForCtx({ branchCode: 'SUP35143228' }, 'quality-reports', 'SUP35143228/abc123.pdf') ).toContain('/api/storage/quality-reports/'); expect(() => buildSignedStoragePathForCtx({ branchCode: 'SUP35143228' }, 'quality-reports', 'SUP37831443/abc123.pdf') ).toThrow(); expect( buildSignedStoragePathForCtx( { branchCode: 'SUP35143228' }, 'invoices', 'archive/SUP35143228/2026-10.tar.gz' ) ).toContain('/api/storage/invoices/public/'); }); it('小写业务目录不得被当成租户根(否则旧 key 反查/前缀断言全部走偏)', () => { for (const name of ['quality-reports', 'free-cost', 'uploads', 'releases', 'archive', 'other']) { expect(readStorageKeyBranch(`${name}/x.pdf`)).toBeNull(); } expect(readStorageKeyBranch('SUP35143228/quality-reports/x.pdf')).toBe('SUP35143228'); expect(readStorageKeyBranch('BR001/supplier-invoices/x.pdf')).toBe('BR001'); }); it('list mint 只为本分支 key 签 URL(跨租户 forged invoice_url 不返回签名)', () => { const base = { id: '1', invoice_url: 'BR002/supplier-invoices/a.pdf' }; expect(mapSupplierInvoiceListRow(base, 'BR001').signed_url).toBeNull(); expect(mapCustomerInvoiceListRow({ ...base, invoice_url: 'BR001/a.pdf' }, 'BR001').signed_url).not.toBeNull(); }); it('写入口/读图入口均有归属断言(源码契约)', () => { const finance = readFileSync(join(__dirname, '../src/modules/workflow/routes/financeRoutes.ts'), 'utf8'); expect(finance).toContain('assertOwnedStoragePath(ctx'); expect(finance).toContain('assertOwnedStoragePaths(ctx'); expect(finance).toContain("assertOwnedStoragePath(ctx, 'invoices', req.body?.invoice_url)"); expect(finance).toContain('req.body?.voucher_urls'); const fn = readFileSync(join(__dirname, '../src/routes/functions.ts'), 'utf8'); expect(fn).toContain("assertStorageKeyOwned('invoices', storedPath"); }); });