Files
SupplierSystem/server/tests/t9fStorageGuardResiduals.test.ts
T
Ubuntu 5fa2bbfd55
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(audit-20261019): T1-T9 审计修复、存储归属与审计日志列宽
- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14)
- T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9)
- T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4)
- T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8)
- T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15)
- T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8)
- T7 前后端测试与 lint 门禁清零
- T8 应付卡片按关联订单日(口径 A)
- T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
2026-10-19 16:16:51 +08:00

159 lines
6.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<string, unknown>, 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 });
});
});