import { describe, expect, it } from 'vitest'; import { assertTableAccess } from '../src/services/accessPolicy'; import type { TenantContext } from '../src/services/tenantScope'; import { buildSignedStoragePathForCtx, buildSignedStoragePathForCtxChecked, tryBuildSignedStoragePathForCtx, } from '../src/services/storageObjectOwnership'; import type { QuerySpec } from '../src/services/queryExecutor'; /** * T9F 残余收口(2026-10-19,dev 补测): * ① `/api/data` 敏感存储列守卫要**递归**命中嵌套 JSON 字段(不只一层 Object.keys); * ② T9F-5:`buildSignedStoragePathForCtx` 对无前缀 legacy key **不再同步放行**; * 需要旧 key 时改走 `buildSignedStoragePathForCtxChecked`(异步 DB 归属反查)。 */ const ADMIN_CTX: TenantContext = { userId: 'u-admin', roles: ['admin'], permissions: ['inspection_manage', 'picking_manage', 'order_manage', 'purchase_manage', 'quality_report_manage'], branchCode: 'BR001', supplierCode: null, purchaserCode: null, isSuperAdmin: false, isBranchAdmin: true, isInternalStaff: true, isSupplierAccount: false, }; const SUPER_CTX: TenantContext = { ...ADMIN_CTX, userId: 'u-super', roles: ['super_admin'], isSuperAdmin: true, isBranchAdmin: false, }; function updateSpec(updateData: Record, table = 'inspections'): QuerySpec { return { table, action: 'update', updateData, filters: [{ op: 'eq', column: 'id', value: '00000000-0000-0000-0000-000000000000' }], }; } describe('T9F-1a:/api/data 敏感存储列(含嵌套 JSON)', () => { it('顶层 inspection_photos ⇒ 403', () => { expect(() => assertTableAccess(updateSpec({ inspection_photos: ['archive/BR002/2026-10.tar.gz'] }), ADMIN_CTX) ).toThrow(/不允许通过数据接口写入/); }); it('大小写变体 Inspection_Photos ⇒ 403(SQL 未加引号会折叠为小写)', () => { expect(() => assertTableAccess(updateSpec({ Inspection_Photos: ['archive/BR002/2026-10.tar.gz'] }), ADMIN_CTX) ).toThrow(/不允许通过数据接口写入/); expect(() => assertTableAccess(updateSpec({ ' PHOTO_URL ': 'BR002/x.jpg' }), ADMIN_CTX) ).toThrow(/不允许通过数据接口写入/); }); it('嵌套 JSON:processing_details.inspection_photos ⇒ 403', () => { expect(() => assertTableAccess( updateSpec({ processing_details: { inspection_photos: ['archive/BR002/2026-10.tar.gz'] } }), ADMIN_CTX ) ).toThrow(/不允许通过数据接口写入/); }); it('数组行内的嵌套 invoice_url/photo_url ⇒ 403', () => { const spec: QuerySpec = { table: 'processing_tasks', action: 'update', updateData: [{ remark: 'ok' }, { metadata: { photo_url: 'BR002/x.jpg' } }], filters: [{ op: 'eq', column: 'id', value: '00000000-0000-0000-0000-000000000000' }], }; expect(() => assertTableAccess(spec, ADMIN_CTX)).toThrow(/不允许通过数据接口写入/); }); it('insert picking_items.outbound_photos ⇒ 403', () => { expect(() => assertTableAccess( { table: 'picking_items', action: 'insert', insertData: { branch_code: 'BR001', outbound_photos: ['BR002/x.jpg'] } }, ADMIN_CTX ) ).toThrow(/不允许通过数据接口写入/); }); it('super_admin 同样拒绝(必须走业务接口)', () => { expect(() => assertTableAccess(updateSpec({ inspection_photos: ['archive/BR002/x'] }), SUPER_CTX) ).toThrow(/不允许通过数据接口写入/); }); it('select / 非敏感列 update 不被该守卫误伤', () => { expect(() => assertTableAccess({ table: 'inspections', action: 'select', select: 'inspection_photos' }, SUPER_CTX) ).not.toThrow(); expect(() => assertTableAccess(updateSpec({ compliance_status: 10 }), SUPER_CTX)).not.toThrow(); }); }); describe('T9F-5:无前缀 legacy key 的 mint fail-closed', () => { it('有 branchCode 但 key 无前缀 ⇒ 403(不再同步签名)', () => { expect(() => buildSignedStoragePathForCtx({ branchCode: 'BR001' }, 'invoices', 'free-cost/legacy.pdf') ).toThrow(/旧存储文件名缺少分公司前缀/); }); it('null ctx / 无 branchCode + 无前缀 key ⇒ 403', () => { expect(() => buildSignedStoragePathForCtx(null, 'invoices', 'free-cost/legacy.pdf')).toThrow(); expect(() => buildSignedStoragePathForCtx({ branchCode: null }, 'quality-reports', 'a.pdf')).toThrow(); }); it('本分支前缀 key 正常签名;跨分支 403', () => { expect( buildSignedStoragePathForCtx({ branchCode: 'BR001' }, 'invoices', 'BR001/free-cost/x.pdf') ).toContain('/api/storage/invoices/public/'); expect(() => buildSignedStoragePathForCtx({ branchCode: 'BR001' }, 'invoices', 'BR002/free-cost/x.pdf') ).toThrow(/不属于当前租户/); }); it('archive/<本分支> 仍可签(归档下载);跨分支 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(/归档对象不属于当前租户/); }); it('super_admin 是设计上的跨租户运维口,旧 key 仍可签', () => { expect( buildSignedStoragePathForCtx({ branchCode: null, isSuperAdmin: true }, 'invoices', 'free-cost/legacy.pdf') ).toContain('/api/storage/invoices/public/'); }); it('宽容版 tryBuild 返回 null(列表展示不 500)', () => { expect(tryBuildSignedStoragePathForCtx({ branchCode: 'BR001' }, 'invoices', 'free-cost/legacy.pdf')).toBeNull(); expect( tryBuildSignedStoragePathForCtx({ branchCode: 'BR001' }, 'invoices', 'BR001/free-cost/x.pdf') ).not.toBeNull(); }); it('异步反查版:本分支 DB 无引用的旧 key ⇒ 403(fail-closed,不签发)', async () => { await expect( buildSignedStoragePathForCtxChecked( { branchCode: 'BR001' }, 'invoices', `free-cost/legacy-${Date.now()}-${Math.random().toString(16).slice(2)}.pdf` ) ).rejects.toMatchObject({ status: 403 }); }); });