- 分公司类型模板:超管配置隐藏路由/菜单 + 中文文案覆盖,仅对挂类型的分公司生效(后端 service/routes、前端 Provider/守卫/配置页) - 超管模板 iframe 真还原预览:28 个业务页注册、白名单点选编辑、空/示例数据 fixture、白名单漂移检测工具 - 商品/SKU 下拉远程搜索:服务端关键词搜索 + 前端 useRemoteOptionSearch/SearchableSelect 接入(订单/采购/商品等页) - 主系统联调:子系统主动解绑主系统授权(幂等重试/本地兜底)、追加修改 price_amend 视图代理与 amend-price 转发 - 修复:仓内加工重复建单幂等(已完成需求单 guard、order 任务一商品一单 guard) - 修复:出入库记录只取前 100 条 → 服务端分页 + 真实总数 - 其它:库存/订单若干修复、分公司管理响应式布局、i18n 文案、设计文档与回归测试
332 lines
12 KiB
TypeScript
332 lines
12 KiB
TypeScript
/**
|
||
* `/api/data` 网关拒写 `branch_ui_*`(设计 v2 §6.3 / AC-31)。
|
||
*
|
||
* 背景:网关允许分公司 admin 写自己 branch_code 的 system_configs(tenantScope.ts 作用域函数),
|
||
* 若不拦,分公司管理员可自行改文字覆盖(违反 D5/D7:文字只由超管配),超管也能绕过领域接口的
|
||
* 校验/审计/缓存失效。这里直接调 `applyTenantScope`(纯函数)验证两条路径都被拒。
|
||
*/
|
||
import { describe, expect, it } from 'vitest';
|
||
import { applyTenantScope, type TenantContext } from '../src/services/tenantScope';
|
||
import type { QuerySpec } from '../src/services/queryExecutor';
|
||
import { applyRetiredSystemConfigPolicy } from '../src/services/retiredSystemConfigKeys';
|
||
|
||
function ctx(overrides: Partial<TenantContext> = {}): TenantContext {
|
||
return {
|
||
userId: 'U1',
|
||
roles: ['admin'],
|
||
permissions: ['system_manage', 'system_config'],
|
||
branchCode: 'SUP0001',
|
||
supplierCode: null,
|
||
purchaserCode: null,
|
||
isSuperAdmin: false,
|
||
isBranchAdmin: true,
|
||
isInternalStaff: true,
|
||
isSupplierAccount: false,
|
||
...overrides,
|
||
};
|
||
}
|
||
|
||
const SUPER = ctx({ userId: 'U-SUPER', roles: ['super_admin'], branchCode: null, isSuperAdmin: true, isBranchAdmin: false });
|
||
const BLOCKED = /只能由平台接口维护/;
|
||
|
||
describe('分公司 admin 路径:/api/data 拒写 branch_ui_*', () => {
|
||
it('insert branch_ui_types / branch_ui_branch 被拒', () => {
|
||
for (const configKey of ['branch_ui_types', 'branch_ui_branch', 'branch_ui_template.retail']) {
|
||
const spec: QuerySpec = {
|
||
table: 'system_configs',
|
||
action: 'insert',
|
||
insertData: { config_key: configKey, config_value: {}, branch_code: 'SUP0001' },
|
||
};
|
||
expect(() => applyTenantScope(spec, ctx()), `${configKey} insert 应被拒`).toThrow(BLOCKED);
|
||
}
|
||
});
|
||
|
||
it('update / delete 指定 config_key=branch_ui_* 被拒', () => {
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{
|
||
table: 'system_configs',
|
||
action: 'update',
|
||
filters: [{ op: 'eq', column: 'config_key', value: 'branch_ui_branch' }],
|
||
updateData: { config_value: { text_overrides: { 'orders.title': '被分公司改掉' } } },
|
||
},
|
||
ctx()
|
||
)
|
||
).toThrow(BLOCKED);
|
||
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{
|
||
table: 'system_configs',
|
||
action: 'delete',
|
||
filters: [{ op: 'eq', column: 'config_key', value: 'branch_ui_branch' }],
|
||
},
|
||
ctx()
|
||
)
|
||
).toThrow(BLOCKED);
|
||
});
|
||
|
||
it('update payload 里带 config_key=branch_ui_* 被拒(按 id 更新的既有写法)', () => {
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{
|
||
table: 'system_configs',
|
||
action: 'update',
|
||
filters: [{ op: 'eq', column: 'id', value: 'row-id' }],
|
||
updateData: { config_key: 'branch_ui_branch', config_value: {} },
|
||
},
|
||
ctx()
|
||
)
|
||
).toThrow(BLOCKED);
|
||
});
|
||
|
||
it('按行 id 更新(不带 config_key 条件)追加 NOT IN 兜底过滤,无法改自建覆盖行', () => {
|
||
const spec: QuerySpec = {
|
||
table: 'system_configs',
|
||
action: 'update',
|
||
filters: [{ op: 'eq', column: 'id', value: 'row-id' }],
|
||
updateData: { config_value: { days: 7 } },
|
||
};
|
||
const scoped = applyTenantScope(spec, ctx());
|
||
const configKeyFilters = (scoped.filters || []).filter((filter) => filter.column === 'config_key');
|
||
expect(configKeyFilters).toEqual(
|
||
expect.arrayContaining([
|
||
{ op: 'neq', column: 'config_key', value: 'branch_ui_types' },
|
||
{ op: 'neq', column: 'config_key', value: 'branch_ui_branch' },
|
||
])
|
||
);
|
||
});
|
||
|
||
it('真实网关顺序:applyRetiredSystemConfigPolicy 先加 config_key neq,仍会补 branch_ui 兜底过滤', () => {
|
||
// 复现线上网关 prepareSpec 的真实顺序(dataGatewayService.ts:171-172):
|
||
// retired 配置策略会给所有 system_configs 写请求加 config_key neq 过滤 —— 之前按
|
||
// 「是否出现 config_key 过滤」判断会漏掉兜底,导致分公司 admin 可按 id 直改覆盖行(已修)。
|
||
const raw: QuerySpec = {
|
||
table: 'system_configs',
|
||
action: 'update',
|
||
filters: [{ op: 'eq', column: 'id', value: 'row-id' }],
|
||
updateData: { config_value: { text_overrides: { 'orders.title': '被分公司改掉' } } },
|
||
};
|
||
const withRetiredPolicy = applyRetiredSystemConfigPolicy(raw);
|
||
const scoped = applyTenantScope(withRetiredPolicy, ctx());
|
||
const branchUiNeq = (scoped.filters || []).filter(
|
||
(filter) =>
|
||
filter.column === 'config_key' &&
|
||
filter.op === 'neq' &&
|
||
['branch_ui_types', 'branch_ui_branch'].includes(String(filter.value))
|
||
);
|
||
expect(branchUiNeq).toHaveLength(2);
|
||
});
|
||
|
||
it('select branch_ui_* 仍放行(只拒写,不拒读)', () => {
|
||
const spec: QuerySpec = {
|
||
table: 'system_configs',
|
||
action: 'select',
|
||
filters: [{ op: 'eq', column: 'config_key', value: 'branch_ui_branch' }],
|
||
};
|
||
expect(() => applyTenantScope(spec, ctx())).not.toThrow();
|
||
});
|
||
});
|
||
|
||
describe('超管路径:/api/data 同样拒写 branch_ui_*', () => {
|
||
it('insert / update / delete 全部被拒', () => {
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{
|
||
table: 'system_configs',
|
||
action: 'insert',
|
||
insertData: { config_key: 'branch_ui_types', config_value: {} },
|
||
},
|
||
SUPER
|
||
)
|
||
).toThrow(BLOCKED);
|
||
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{
|
||
table: 'system_configs',
|
||
action: 'update',
|
||
filters: [{ op: 'eq', column: 'config_key', value: 'branch_ui_types' }],
|
||
updateData: { config_value: {} },
|
||
},
|
||
SUPER
|
||
)
|
||
).toThrow(BLOCKED);
|
||
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{
|
||
table: 'system_configs',
|
||
action: 'delete',
|
||
filters: [{ op: 'eq', column: 'config_key', value: 'branch_ui_template.retail' }],
|
||
},
|
||
SUPER
|
||
)
|
||
).toThrow(BLOCKED);
|
||
|
||
// in / ilike 形态也拦
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{
|
||
table: 'system_configs',
|
||
action: 'update',
|
||
filters: [{ op: 'in', column: 'config_key', value: ['nav_menu_layout', 'branch_ui_branch'] }],
|
||
updateData: { config_value: {} },
|
||
},
|
||
SUPER
|
||
)
|
||
).toThrow(BLOCKED);
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{
|
||
table: 'system_configs',
|
||
action: 'delete',
|
||
filters: [{ op: 'ilike', column: 'config_key', value: 'branch_ui_%' }],
|
||
},
|
||
SUPER
|
||
)
|
||
).toThrow(BLOCKED);
|
||
});
|
||
|
||
it('普通 system_configs 写不受影响(审计/其它配置页回归)', () => {
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{
|
||
table: 'system_configs',
|
||
action: 'update',
|
||
filters: [{ op: 'eq', column: 'id', value: 'row-id' }],
|
||
updateData: { config_value: { minutes: 30 } },
|
||
},
|
||
SUPER
|
||
)
|
||
).not.toThrow();
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{
|
||
table: 'system_configs',
|
||
action: 'insert',
|
||
insertData: { config_key: 'nav_menu_layout', config_value: {}, branch_code: null },
|
||
},
|
||
SUPER
|
||
)
|
||
).not.toThrow();
|
||
});
|
||
});
|
||
|
||
describe('回归:既有 nav_menu_layout 网关写路径不受影响', () => {
|
||
it('分公司 admin insert nav_menu_layout 仍被强制本公司 branch_code', () => {
|
||
const spec: QuerySpec = {
|
||
table: 'system_configs',
|
||
action: 'insert',
|
||
insertData: { config_key: 'nav_menu_layout', config_value: {}, branch_code: 'SUP9999' },
|
||
};
|
||
const scoped = applyTenantScope(spec, ctx());
|
||
const rows = Array.isArray(scoped.insertData) ? scoped.insertData : [scoped.insertData];
|
||
expect((rows[0] as Record<string, unknown>).branch_code).toBe('SUP0001');
|
||
});
|
||
|
||
it('分公司 admin 按 id 更新 nav_menu_layout 不被 NOT IN 兜底误伤(不变更既有语义)', () => {
|
||
const spec: QuerySpec = {
|
||
table: 'system_configs',
|
||
action: 'update',
|
||
filters: [{ op: 'eq', column: 'id', value: 'row-id' }],
|
||
updateData: { config_value: {} },
|
||
};
|
||
expect(() => applyTenantScope(spec, ctx())).not.toThrow();
|
||
});
|
||
});
|
||
|
||
|
||
/**
|
||
* F11(R2):LIKE/ILIKE 通配、IN 数组通配、OR 形态绕过防护。
|
||
* 背景:分公司 admin 用 `config_key ilike 'branch_ui%'` 或 `'%branch_ui_branch%'` 可命中
|
||
* 自己的 branch_ui_branch 行并改文字覆盖/额外隐藏(实测 effective 生效)⇒ 一律 400。
|
||
* batch 形态由 `runDataBatch` 对每个 spec 调 `applyTenantScope`,同一道门覆盖。
|
||
*/
|
||
describe('F11:config_key 的 like/ilike/通配 in/or 一律拒绝', () => {
|
||
const writeSpec = (filters: QuerySpec['filters']): QuerySpec => ({
|
||
table: 'system_configs',
|
||
action: 'update',
|
||
filters,
|
||
updateData: { config_value: { text_overrides: { 'orders.list.kpiTotal': '被改' } } },
|
||
});
|
||
|
||
const blockedSpecs: Array<[string, QuerySpec['filters']]> = [
|
||
['ilike branch_ui%', [{ op: 'ilike', column: 'config_key', value: 'branch_ui%' }]],
|
||
['ilike %branch_ui_branch%', [{ op: 'ilike', column: 'config_key', value: '%branch_ui_branch%' }]],
|
||
['ilike 任意通配(无 branch_ui 字面)', [{ op: 'ilike', column: 'config_key', value: '%ui%' }]],
|
||
['like branch_ui%(历史/伪客户端形态)', [{ op: 'like' as never, column: 'config_key', value: 'branch_ui%' }]],
|
||
['in 含 branch_ui 通配', [{ op: 'in', column: 'config_key', value: ['branch_ui%'] }]],
|
||
['in 含 %branch_ui_branch%', [{ op: 'in', column: 'config_key', value: ['nav_menu_layout', '%branch_ui_branch%'] }]],
|
||
['in 含任意通配', [{ op: 'in', column: 'config_key', value: ['nav_%'] }]],
|
||
['or 形态 config_key.ilike', [{ op: 'or', raw: 'config_key.ilike.branch_ui%,status.eq.1' }]],
|
||
['or 形态 config_key.like', [{ op: 'or', raw: 'config_key.like.%branch_ui%,status.eq.1' }]],
|
||
];
|
||
|
||
it.each(blockedSpecs)('%s ⇒ 分公司 admin 被拒(400 语义)', (_label, filters) => {
|
||
expect(() => applyTenantScope(writeSpec(filters), ctx())).toThrow(BLOCKED);
|
||
});
|
||
|
||
it.each(blockedSpecs)('%s ⇒ 超管同样被拒(网关统一口径)', (_label, filters) => {
|
||
expect(() => applyTenantScope(writeSpec(filters), SUPER)).toThrow(BLOCKED);
|
||
});
|
||
|
||
it('delete 走同样拦截', () => {
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{ table: 'system_configs', action: 'delete', filters: [{ op: 'ilike', column: 'config_key', value: 'branch_ui%' }] },
|
||
ctx()
|
||
)
|
||
).toThrow(BLOCKED);
|
||
});
|
||
|
||
it('batch 形态:同一批里任一 spec 命中即整批失败(runDataBatch 逐 spec 过同一道门)', () => {
|
||
const batch = [
|
||
{ table: 'system_configs', action: 'update', filters: [{ op: 'eq', column: 'id', value: 'row-1' }], updateData: { config_value: { days: 7 } } },
|
||
writeSpec([{ op: 'ilike', column: 'config_key', value: 'branch_ui%' }]),
|
||
] satisfies QuerySpec[];
|
||
expect(() => {
|
||
for (const spec of batch) applyTenantScope(spec, ctx());
|
||
}).toThrow(BLOCKED);
|
||
});
|
||
|
||
it('正常写不受影响:eq config_key / 非 config_key 列的 ilike / in 精确值 仍放行', () => {
|
||
// eq 精确 key(普通配置页写法)
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{
|
||
table: 'system_configs',
|
||
action: 'update',
|
||
filters: [{ op: 'eq', column: 'config_key', value: 'nav_menu_layout' }],
|
||
updateData: { config_value: {} },
|
||
},
|
||
ctx()
|
||
)
|
||
).not.toThrow();
|
||
// ilike 用在其它列(branch_code)不应误伤
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{
|
||
table: 'system_configs',
|
||
action: 'update',
|
||
filters: [{ op: 'ilike', column: 'branch_code', value: 'SUP%' }],
|
||
updateData: { config_value: {} },
|
||
},
|
||
ctx()
|
||
)
|
||
).not.toThrow();
|
||
// in 精确值(无通配、无 branch_ui)
|
||
expect(() =>
|
||
applyTenantScope(
|
||
{
|
||
table: 'system_configs',
|
||
action: 'update',
|
||
filters: [{ op: 'in', column: 'config_key', value: ['nav_menu_layout', 'print_templates'] }],
|
||
updateData: { config_value: {} },
|
||
},
|
||
ctx()
|
||
)
|
||
).not.toThrow();
|
||
});
|
||
});
|