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 超长修复
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import { Router, Response } from 'express';
|
||||
import {
|
||||
assertOwnedStoragePaths as assertOwnedStoragePathsShared,
|
||||
assertStorageKeyOwned,
|
||||
} from '../../../services/storageObjectOwnership';
|
||||
import { pool } from '../../../db';
|
||||
import { authMiddleware, type AuthRequest } from '../../../middleware/auth';
|
||||
import { readOperatingBranchCode, getEffectiveBranchCode } from '../../../services/tenantScope';
|
||||
@@ -246,10 +250,22 @@ router.get('/finance/customer-invoices', authMiddleware, async (req: AuthRequest
|
||||
}
|
||||
});
|
||||
|
||||
async function assertOwnedStoragePath(ctx: any, bucket: string, raw: unknown): Promise<void> {
|
||||
const path = String(raw || '').trim();
|
||||
if (!path) return;
|
||||
await assertStorageKeyOwned(bucket, path, ctx);
|
||||
}
|
||||
|
||||
async function assertOwnedStoragePaths(ctx: any, bucket: string, raws: unknown): Promise<void> {
|
||||
// T9:批量写入口统一走公用断言(外链/data: 跳过;任一桶内 key 不属于本租户 ⇒ 403)
|
||||
await assertOwnedStoragePathsShared(ctx, bucket, raws);
|
||||
}
|
||||
|
||||
router.post('/finance/customer-invoices', authMiddleware, async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
requireWorkflowPermission(ctx, ['finance_manage', 'statement_manage', 'invoice_manage']);
|
||||
await assertOwnedStoragePath(ctx, 'invoices', req.body?.invoice_url);
|
||||
const data = await insertCustomerInvoice(
|
||||
{
|
||||
invoice_no: req.body?.invoice_no,
|
||||
@@ -278,6 +294,7 @@ router.post('/finance/supplier-invoices', authMiddleware, async (req: AuthReques
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
requireWorkflowPermission(ctx, ['finance_manage', 'statement_manage', 'invoice_manage']);
|
||||
await assertOwnedStoragePath(ctx, 'invoices', req.body?.invoice_url);
|
||||
const data = await insertSupplierInvoice(
|
||||
{
|
||||
invoice_no: req.body?.invoice_no,
|
||||
@@ -907,6 +924,8 @@ router.post('/cost/free-cost', authMiddleware, async (req: AuthRequest, res: Res
|
||||
const description = String(req.body?.description || req.body?.remark || '');
|
||||
const voucherUrls = req.body?.voucher_urls ?? req.body?.voucherUrls;
|
||||
const branchCode = getEffectiveBranchCode(ctx, readOperatingBranchCode(req));
|
||||
// T9:写库前校验 voucher_url 归属(跨分支 key ⇒ 403,不落库)
|
||||
await assertOwnedStoragePaths(ctx, 'invoices', voucherUrls);
|
||||
let status = 20;
|
||||
let requireVoucher = false;
|
||||
if (mpFreeCost) {
|
||||
@@ -931,6 +950,7 @@ router.post('/cost/free-cost', authMiddleware, async (req: AuthRequest, res: Res
|
||||
invoicePhoto: mpFreeCost && (req.body?.invoice_photo === true || req.body?.invoice_photo === 'true'),
|
||||
status,
|
||||
source: mpFreeCost ? 'mp' : 'web',
|
||||
ownerCtx: ctx,
|
||||
});
|
||||
return res.json({ data });
|
||||
} catch (error: any) {
|
||||
@@ -992,6 +1012,7 @@ router.post('/cost/free-cost/extract-invoice', authMiddleware, async (req: AuthR
|
||||
if (!req.userId) {
|
||||
return res.status(401).json({ error: 'unauthorized', error_description: '未登录' });
|
||||
}
|
||||
await assertOwnedStoragePath(ctx, 'invoices', req.body?.invoice_path || req.body?.path);
|
||||
const data = await extractFreeCostInvoice({
|
||||
invoicePath: String(req.body?.invoice_path || req.body?.path || ''),
|
||||
ctx,
|
||||
@@ -1014,6 +1035,7 @@ router.post('/cost/free-cost/invoices', authMiddleware, async (req: AuthRequest,
|
||||
if (!req.userId) {
|
||||
return res.status(401).json({ error: 'unauthorized', error_description: '未登录' });
|
||||
}
|
||||
await assertOwnedStoragePath(ctx, 'invoices', req.body?.invoice_path || req.body?.path);
|
||||
const data = await saveFreeCostInvoice({
|
||||
invoicePath: String(req.body?.invoice_path || req.body?.path || ''),
|
||||
costDetailIds: req.body?.cost_detail_ids ?? req.body?.costDetailIds,
|
||||
@@ -1073,8 +1095,15 @@ router.post(
|
||||
if (!req.userId) {
|
||||
return res.status(401).json({ error: 'unauthorized', error_description: '未登录' });
|
||||
}
|
||||
const items = Array.isArray(req.body?.items) ? req.body.items : [];
|
||||
// T9:写库前校验 storage_path 归属(跨分支 key ⇒ 403,事务内 ROLLBACK,不新增行)
|
||||
await assertOwnedStoragePaths(
|
||||
ctx,
|
||||
'invoices',
|
||||
items.map((item: any) => item?.path)
|
||||
);
|
||||
const data = await saveFreeCostPaymentVouchers({
|
||||
items: Array.isArray(req.body?.items) ? req.body.items : [],
|
||||
items,
|
||||
ctx,
|
||||
uploaderId: req.userId,
|
||||
branchCode: getEffectiveBranchCode(ctx, readOperatingBranchCode(req)),
|
||||
@@ -1246,6 +1275,7 @@ router.post('/cost/free-cost/voucher-amount', authMiddleware, async (req: AuthRe
|
||||
requireWorkflowPermission(ctx, ['cost_manage', 'finance_manage']);
|
||||
}
|
||||
const voucherPath = String(req.body?.voucher_path || req.body?.path || '').trim();
|
||||
await assertOwnedStoragePath(ctx, 'invoices', voucherPath);
|
||||
let imageBase64 = String(req.body?.image_base64 || req.body?.content || '');
|
||||
// 真机大图 base64 易超 express.json 2mb;优先用已上传 path 服务端读图
|
||||
if (voucherPath) {
|
||||
@@ -1599,6 +1629,9 @@ router.post('/finance/fixed-costs', authMiddleware, async (req: AuthRequest, res
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
requireWorkflowPermission(ctx, ['cost_manage', 'finance_manage']);
|
||||
const voucherUrls = Array.isArray(req.body?.voucher_urls) ? req.body.voucher_urls.map(String) : undefined;
|
||||
// T9:写库前校验 voucher_urls 归属(跨分支 key ⇒ 403,不落库)
|
||||
await assertOwnedStoragePaths(ctx, 'invoices', voucherUrls);
|
||||
const data = await createFixedCost({
|
||||
cycleCode: String(req.body?.cycle_code || ''),
|
||||
costType: String(req.body?.cost_type || 'other'),
|
||||
@@ -1606,10 +1639,11 @@ router.post('/finance/fixed-costs', authMiddleware, async (req: AuthRequest, res
|
||||
actualAmount: Number(req.body?.actual_amount) || 0,
|
||||
allocationMethod: String(req.body?.allocation_method || 'amount'),
|
||||
remark: req.body?.remark ? String(req.body.remark) : null,
|
||||
voucherUrls: Array.isArray(req.body?.voucher_urls) ? req.body.voucher_urls.map(String) : undefined,
|
||||
voucherUrls,
|
||||
branchCode: getEffectiveBranchCode(ctx, readOperatingBranchCode(req)),
|
||||
inputterId: req.userId,
|
||||
allocate: req.body?.allocate !== false && req.body?.allocate !== 'false',
|
||||
ownerCtx: ctx,
|
||||
});
|
||||
return res.json({ data });
|
||||
} catch (error: any) {
|
||||
@@ -1693,6 +1727,8 @@ router.patch('/finance/fixed-costs/:id/vouchers', authMiddleware, async (req: Au
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
requireWorkflowPermission(ctx, ['cost_manage', 'finance_manage']);
|
||||
const urls = Array.isArray(req.body?.voucher_urls) ? req.body.voucher_urls.map(String).filter(Boolean) : [];
|
||||
// T9:UPDATE 前校验 voucher_urls 归属(跨分支 key ⇒ 403,原值不变)
|
||||
await assertOwnedStoragePaths(ctx, 'invoices', urls);
|
||||
const data = await updateFixedCostVouchers(String(req.params.id), urls, ctx);
|
||||
return res.json({ data });
|
||||
} catch (error: any) {
|
||||
|
||||
@@ -129,7 +129,9 @@ export function mountMainSystemBidWorkflowRoutes(router: Router): void {
|
||||
requireAdminRole(ctx, '仅管理员可删除竞价邀约');
|
||||
const branchCode = branchOf(req, ctx) || ctx?.branchCode || '';
|
||||
const roundId = String(req.params?.roundId || '');
|
||||
const supplierCode = req.body?.supplier_code ?? req.query?.supplier_code ?? null;
|
||||
// 前端(workflowApi)按详情接口同款字段名传 `?supplier=`;旧调用方仍可传 `supplier_code`。
|
||||
const supplierCode =
|
||||
req.body?.supplier_code ?? req.body?.supplier ?? req.query?.supplier_code ?? req.query?.supplier ?? null;
|
||||
const result = await softDeleteBidInvitation({ branchCode, roundId, supplierCode, userId: ctx?.userId ?? null });
|
||||
if (!result.ok) {
|
||||
return res.status(result.status).json({ error: result.code, error_description: result.message });
|
||||
@@ -147,7 +149,9 @@ export function mountMainSystemBidWorkflowRoutes(router: Router): void {
|
||||
requireAdminRole(ctx, '仅管理员可恢复竞价邀约');
|
||||
const branchCode = branchOf(req, ctx) || ctx?.branchCode || '';
|
||||
const roundId = String(req.params?.roundId || '');
|
||||
const supplierCode = req.body?.supplier_code ?? req.query?.supplier_code ?? null;
|
||||
// 同上:兼容前端 `?supplier=` 与旧字段 `supplier_code`。
|
||||
const supplierCode =
|
||||
req.body?.supplier_code ?? req.body?.supplier ?? req.query?.supplier_code ?? req.query?.supplier ?? null;
|
||||
const result = await restoreBidInvitation({ branchCode, roundId, supplierCode });
|
||||
if (!result.ok) {
|
||||
return res.status(result.status).json({ error: result.code, error_description: result.message });
|
||||
|
||||
@@ -59,6 +59,7 @@ import {
|
||||
import { listPurchaseOrdersForManagement, sumSelfPurchaseTotalAmount } from '../../../services/purchaseOrderQueryService';
|
||||
import { listPriceInvertRisks } from '../../../services/priceInvertRiskQueryService';
|
||||
import { resolveSelfPurchaseWriteAccess } from '../../../services/selfPurchaseHttpAccess';
|
||||
import { assertOwnedStoragePaths } from '../../../services/storageObjectOwnership';
|
||||
|
||||
const PREP_AMEND_PERMS = [
|
||||
'preparation_manage',
|
||||
@@ -67,6 +68,20 @@ const PREP_AMEND_PERMS = [
|
||||
'order_entry',
|
||||
];
|
||||
|
||||
/**
|
||||
* T9:质检报告上传入口只对「桶内对象路径」做归属断言;
|
||||
* data:/裸 base64/外链不是桶内 key(服务端会另存或忽略),不参与归属校验。
|
||||
*/
|
||||
function uploadPathRefs(files: string[]): string[] {
|
||||
return files.filter((value) => {
|
||||
const text = String(value || '').trim();
|
||||
if (!text) return false;
|
||||
if (/^data:/i.test(text) || /^(?:https?:)?\/\//i.test(text)) return false;
|
||||
if (text.length > 200 && /^[A-Za-z0-9+/=\s]+$/.test(text)) return false;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/** 采购模块 workflow API(质检库与 /purchase*) */
|
||||
export function mountPurchaseWorkflowRoutes(router: Router): void {
|
||||
/**
|
||||
@@ -214,6 +229,8 @@ router.post('/quality-reports/library', authMiddleware, async (req: AuthRequest,
|
||||
? [String(req.body.file)]
|
||||
: [];
|
||||
const supplierCode = String(req.body?.supplier_code || '').trim();
|
||||
// T9:写库前校验报告文件 key 归属(跨分支 key ⇒ 403,不落库)
|
||||
await assertOwnedStoragePaths(ctx, 'quality-reports', uploadPathRefs(files));
|
||||
if (ctx!.isSupplierAccount) {
|
||||
if (!ctx!.supplierCode) {
|
||||
return res.status(403).json({
|
||||
@@ -456,6 +473,8 @@ router.post('/purchase/quality-reports', authMiddleware, async (req: AuthRequest
|
||||
: req.body?.file
|
||||
? [String(req.body.file)]
|
||||
: [];
|
||||
// T9:写库前校验报告文件 key 归属(跨分支 key ⇒ 403,不落库)
|
||||
await assertOwnedStoragePaths(ctx, 'quality-reports', uploadPathRefs(files));
|
||||
const data = await uploadQualityReport({
|
||||
purchaseCode,
|
||||
files,
|
||||
@@ -560,7 +579,7 @@ router.post('/purchase/direct-delivery-photo', authMiddleware, async (req: AuthR
|
||||
return res.json({
|
||||
success: true,
|
||||
path: data.path,
|
||||
photo_url: buildDirectDeliveryPhotoUrls(req, [data.path])[0] || '',
|
||||
photo_url: (await buildDirectDeliveryPhotoUrls(req, [data.path]))[0] || '',
|
||||
purchase_codes: data.purchase_codes,
|
||||
});
|
||||
} catch (error: any) {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Router, Response } from 'express';
|
||||
import { authMiddleware, type AuthRequest } from '../../../middleware/auth';
|
||||
import { getEffectiveBranchCode, readOperatingBranchCode } from '../../../services/tenantScope';
|
||||
import { getWorkflowCtx, requireWorkflowPermission } from '../context';
|
||||
import { listSupplierSkuCandidates } from '../../../services/skuMappingService';
|
||||
|
||||
/** 报价单模块 workflow API */
|
||||
export function mountQuoteWorkflowRoutes(router: Router): void {
|
||||
@@ -180,23 +181,7 @@ router.get('/quote/sku-candidates', authMiddleware, async (req: AuthRequest, res
|
||||
if (!branchCode) return res.status(400).json({ error: 'branch_required', error_description: '请选择当前操作分公司' });
|
||||
const q = String(req.query?.q || '').trim();
|
||||
const limit = Math.min(Math.max(Number(req.query?.limit) || 20, 1), 50);
|
||||
const { pool } = await import('../../../db');
|
||||
const { runWithTenantDbContext } = await import('../../../services/tenantDbContext');
|
||||
const rows = await runWithTenantDbContext(branchCode, async () => {
|
||||
const params: unknown[] = [];
|
||||
let where = "(status IS NULL OR status = 1)";
|
||||
if (q) {
|
||||
params.push(`%${q}%`);
|
||||
where += ` AND (sku_name ILIKE $1 OR sku_code ILIKE $1)`;
|
||||
}
|
||||
params.push(limit);
|
||||
const res2 = await pool.query(
|
||||
`SELECT sku_code, COALESCE(NULLIF(TRIM(sku_name), ''), sku_code) AS sku_name, unit
|
||||
FROM skus WHERE ${where} ORDER BY sku_name ASC, sku_code ASC LIMIT $${params.length}`,
|
||||
params
|
||||
);
|
||||
return res2.rows;
|
||||
});
|
||||
const rows = await listSupplierSkuCandidates({ branchCode, q, limit });
|
||||
return res.json({ data: { rows, limit } });
|
||||
} catch (error: any) {
|
||||
return res.status(error?.status || 500).json({
|
||||
|
||||
Reference in New Issue
Block a user