Files
SupplierSystem/server/tests/storageOwnershipEnforcement.test.ts
T
雨天 8628874187
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(存储): 租户分支根放行 SUP 编码,修上传报 Invalid file name
私有桶 key 形状白名单把租户分支根写死成主系统的 /^BR…$/,而本系统的
分公司编码是 SUP35143228 这类形状,于是 buildStorageObjectKey 直接抛
Invalid file name(库存→上传报告点「上传并解析」即弹这句英文)。

同一份 BR… 正则还重复写在归属判定与 stripStorageBranchPrefix 里,只改
一处会出现「上传成功但读/签名 403」。现收敛为唯一定义
TENANT_BRANCH_ROOT_RE = /^[A-Z0-9][A-Z0-9_-]{1,29}$/(branch_code 创建
时统一大写),三处使用点同口径;未知小写目录仍一律拒绝,层级上限、
穿越拒收、<branch>/ 前缀策略与存储布局均未动。
2026-10-21 15:51:55 +08:00

71 lines
4.4 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 { 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/<branch>/ 签名归属(同分支 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");
});
});