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
+27 -3
View File
@@ -36,7 +36,6 @@ export class AttestationService {
batchNo: code.batch.batchNo,
outbound: {
outTime: code.outTime,
outImage: code.outImage,
outImages: code.outImages,
},
dynamicFields: code.artwork.dynamicFields,
@@ -52,10 +51,30 @@ export class AttestationService {
rawResponse: { requestMeta: summary },
},
});
await this.queue.add('onchain', { proofId: proof.id });
// 入队失败(如 Redis 抖动)时把 proof 标记 FAILED,避免留下永远处理不了的 PENDING 悬挂记录
try {
await this.queue.add('onchain', { proofId: proof.id });
} catch (e) {
await this.markQueueFailed(proof.id, proof.rawResponse, e);
throw new BusinessException(ErrorCode.INTERNAL_ERROR, '上链任务入队失败,请稍后重试');
}
return proof;
}
private async markQueueFailed(proofId: number, raw: unknown, e: unknown) {
await this.prisma.blockchainProof.update({
where: { id: proofId },
data: {
status: ProofStatus.FAILED,
rawResponse: {
...(raw && typeof raw === 'object' ? (raw as object) : {}),
enqueueError: (e as Error).message,
failedAt: new Date().toISOString(),
},
},
});
}
async retry(proofId: number) {
const proof = await this.prisma.blockchainProof.findUnique({ where: { id: proofId } });
if (!proof) throw new BusinessException(ErrorCode.NOT_FOUND, '存证记录不存在', 404);
@@ -75,7 +94,12 @@ export class AttestationService {
},
},
});
await this.queue.add('onchain', { proofId });
try {
await this.queue.add('onchain', { proofId });
} catch (e) {
await this.markQueueFailed(proofId, proof.rawResponse, e);
throw new BusinessException(ErrorCode.INTERNAL_ERROR, '上链任务入队失败,请稍后重试');
}
return { success: true };
}
@@ -3,6 +3,7 @@ import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { RoleType } from '@artveris/shared';
import { AttestationService } from './attestation.service';
import { Roles } from '../common/decorators/roles.decorator';
import { parseBigId } from '../common/utils/bigint.util';
@ApiTags('区块链存证')
@ApiBearerAuth()
@@ -14,7 +15,7 @@ export class BlockchainController {
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '为追溯码触发数据存证上链' })
onchain(@Param('id') id: string) {
return this.attestation.trigger(BigInt(id));
return this.attestation.trigger(parseBigId(id, '追溯码 ID'));
}
@Post('blockchain-proofs/:id/retry')
+11 -1
View File
@@ -10,6 +10,15 @@ const TIANHE_TEST_URL = 'https://test.api.tichain.tianhecloud.com';
const TIANHE_PRODUCTION_URL = 'https://api.tichain.tianhecloud.com';
const DEFAULT_TIANHE_USER_ID = 'artveris-platform';
// 递归按 key 排序的稳定序列化,用于内容哈希(与 JSON.stringify 不同,属性顺序不影响结果)
function stableStringify(value: unknown): string {
if (value === null || typeof value !== 'object') return JSON.stringify(value) ?? 'null';
if (Array.isArray(value)) return `[${value.map(stableStringify).join(',')}]`;
const keys = Object.keys(value as Record<string, unknown>).sort();
const entries = keys.map((k) => `${JSON.stringify(k)}:${stableStringify((value as Record<string, unknown>)[k])}`);
return `{${entries.join(',')}}`;
}
@Injectable()
export class BlockchainService {
private readonly logger = new Logger(BlockchainService.name);
@@ -20,7 +29,8 @@ export class BlockchainService {
) {}
hashData(data: unknown): string {
return '0x' + createHash('sha256').update(JSON.stringify(data)).digest('hex');
// 稳定序列化:递归按 key 排序,确保「同数据同哈希」,不受对象属性顺序影响
return '0x' + createHash('sha256').update(stableStringify(data)).digest('hex');
}
async attest(payload: AttestPayload): Promise<AttestResult> {