fix: 修复后端竞态/安全/数据处理问题并移除冗余 outImage 列

首次激活改条件更新消除并发竞态;备份恢复补模型全集与版本校验防清库;
审核列表分页总数、操作日志未捕获 Promise、出库导入重复码、加密密钥可编辑、
溯源码盐以 env 为准、证书按 originKey reconcile、env 加载去重等修复;
新增 parseBigId 防非法入参 500、全局安全响应头、去除 SVG 上传、接入 throttler 限流、
入队失败补偿、hashData 稳定序列化、无效码负缓存、看板已激活口径调整;
移除冗余 TraceCode.outImage 列统一 outImages(迁移 drop_trace_code_out_image)。
This commit is contained in:
Nixevol
2026-07-09 13:14:26 +08:00
parent c39974e72d
commit f712b16c1f
29 changed files with 328 additions and 170 deletions
@@ -15,8 +15,8 @@ export interface TraceContext {
}
const MAP_TTL = 3600; // 短码→id 映射缓存 1 小时
const OUTBOUND_TRACE_TITLE = '本件出库记录';
const OUTBOUND_TRACE_DESC = '该追溯码对应实物的出库实况记录。';
const MISS_TTL = 60; // 无效码负缓存 60 秒,降低无效码刷库压力
const MISS_SENTINEL = '0'; // 负缓存哨兵值(真实 id 从 1 起,不会为 0)
@Injectable()
export class PublicTraceService {
@@ -44,12 +44,13 @@ export class PublicTraceService {
description: string | null;
mediaFiles: unknown;
}>,
code: { outTime?: Date | null; outImage?: string | null; outImages?: unknown },
code: { outTime?: Date | null; outImages?: unknown },
) {
const timeline = nodes.map((n) => ({
nodeType: n.nodeType,
title: n.title,
occurredAt: n.occurredAt,
system: null as string | null,
title: n.title as string | null,
occurredAt: n.occurredAt as Date | null,
location: n.location,
description: n.description,
media: this.mapMedia(n.mediaFiles),
@@ -60,17 +61,17 @@ export class PublicTraceService {
.map((item) => (typeof item === 'string' ? { key: item } : item))
.filter((item) => item?.key)
.map((item) => ({ name: item.name || '出库实况图', type: item.type || 'image', url: this.storage.getUrl(item.key as string) }))
: code.outImage
? [{ name: '出库实况图', type: 'image', url: this.storage.getUrl(code.outImage) }]
: [];
: [];
if (code.outTime || outMedia.length) {
// 单件出库记录:不下发中文标题/说明,标记 system=OUTBOUND_RECORD 由 C 端按语言本地化
timeline.push({
nodeType: 'OUTBOUND',
title: OUTBOUND_TRACE_TITLE,
system: 'OUTBOUND_RECORD',
title: null,
occurredAt: code.outTime ?? null,
location: null,
description: OUTBOUND_TRACE_DESC,
description: null,
media: outMedia,
});
}
@@ -81,12 +82,17 @@ export class PublicTraceService {
private async resolveId(shortCode: string): Promise<bigint | null> {
const cacheKey = `trace:map:${shortCode}`;
const cached = await this.redis.get(cacheKey);
if (cached === MISS_SENTINEL) return null;
if (cached) return BigInt(cached);
const code = await this.prisma.traceCode.findUnique({
where: { shortCode },
select: { id: true },
});
if (!code) return null;
if (!code) {
// 负缓存无效码,避免被脚本高频扫描时每次都落库
await this.redis.set(cacheKey, MISS_SENTINEL, 'EX', MISS_TTL);
return null;
}
await this.redis.set(cacheKey, code.id.toString(), 'EX', MAP_TTL);
return code.id;
}
@@ -137,13 +143,14 @@ export class PublicTraceService {
return { status: 'INVALID' as const };
}
// 激活 / 计数
// 激活 / 计数:用条件更新消除并发首次扫码的竞态。
// 只有真正把 INACTIVE 抢成 ACTIVE 的那一次请求 firstActivate=true(updateMany 返回 count=1),
// 其余并发请求 count=0,退化为原子自增,避免 queryCount 被双双写成 1、firstActivatedAt 被覆盖。
const now = new Date();
let firstActivate = false;
let updated;
if (code.status === TraceCodeStatus.INACTIVE) {
updated = await this.prisma.traceCode.update({
where: { id },
const claimed = await this.prisma.traceCode.updateMany({
where: { id, status: TraceCodeStatus.INACTIVE },
data: {
status: TraceCodeStatus.ACTIVE,
firstActivatedAt: now,
@@ -151,13 +158,16 @@ export class PublicTraceService {
lastQueriedAt: now,
},
});
firstActivate = true;
} else {
updated = await this.prisma.traceCode.update({
firstActivate = claimed.count > 0;
}
if (!firstActivate) {
await this.prisma.traceCode.update({
where: { id },
data: { queryCount: { increment: 1 }, lastQueriedAt: now },
});
}
// 回查最新值组装响应(首次激活时读到自身写入,重复查询读到自增后的值)
const updated = await this.prisma.traceCode.findUniqueOrThrow({ where: { id } });
const proof = await this.prisma.blockchainProof.findFirst({
where: { traceCodeId: id, status: ProofStatus.ONCHAIN },