fix: 加固批量出库导入流程

This commit is contained in:
Nixevol
2026-07-14 07:02:41 +08:00
parent 3c74f03e7e
commit 9644f5c128
54 changed files with 5292 additions and 516 deletions
@@ -0,0 +1,13 @@
import { SettingsController } from './settings.controller';
describe('SettingsController.update', () => {
it('rejects storage changes that bypass the migration and confirm endpoints', () => {
const settings = { update: jest.fn() };
const controller = new SettingsController(settings as any, {} as any);
expect(() => controller.update({ storageDriver: 'oss' })).toThrow(
'存储配置必须通过存储迁移接口修改',
);
expect(settings.update).not.toHaveBeenCalled();
});
});
+20 -1
View File
@@ -1,10 +1,26 @@
import { Body, Controller, Get, Post, Put } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { RoleType } from '@artveris/shared';
import { ErrorCode, RoleType } from '@artveris/shared';
import { SettingsService } from './settings.service';
import { BlockchainService } from '../blockchain/blockchain.service';
import { Roles } from '../common/decorators/roles.decorator';
import { Public } from '../common/decorators/public.decorator';
import { BusinessException } from '../common/exceptions/business.exception';
const STORAGE_SETTING_KEYS = new Set([
'storageDriver',
'storageLocalDir',
'storagePublicPrefix',
'cdnBaseUrl',
'ossEndpoint',
'ossRegion',
'ossBucket',
'ossAccessKeyId',
'ossAccessKeySecret',
'storageMigrationTarget',
'storageMigrationFingerprint',
'storageMigrationCopied',
]);
@ApiTags('系统设置')
@Controller('settings')
@@ -34,6 +50,9 @@ export class SettingsController {
@Put()
@ApiOperation({ summary: '更新系统设置' })
update(@Body() body: Record<string, string>) {
if (Object.keys(body).some((key) => STORAGE_SETTING_KEYS.has(key))) {
throw new BusinessException(ErrorCode.BAD_REQUEST, '存储配置必须通过存储迁移接口修改');
}
return this.settingsService.update(body);
}
@@ -0,0 +1,34 @@
import { SettingsService } from './settings.service';
describe('SettingsService.update', () => {
it('writes a multi-key settings change inside one database transaction', async () => {
const tx = {
systemSetting: { upsert: jest.fn().mockResolvedValue(undefined) },
};
const prisma = {
systemSetting: {
findMany: jest.fn().mockResolvedValue([]),
findUnique: jest.fn().mockResolvedValue(null),
upsert: jest.fn(),
},
$transaction: jest.fn().mockImplementation(async (callback: any) => callback(tx)),
};
const config = { get: jest.fn().mockReturnValue(undefined) };
const service = new SettingsService(prisma as any, config as any);
await service.update({ storageDriver: 'oss', ossBucket: 'artveris' });
expect(prisma.$transaction).toHaveBeenCalledTimes(1);
expect(tx.systemSetting.upsert).toHaveBeenNthCalledWith(1, {
where: { key: 'storageDriver' },
update: { value: 'oss' },
create: { key: 'storageDriver', value: 'oss' },
});
expect(tx.systemSetting.upsert).toHaveBeenNthCalledWith(2, {
where: { key: 'ossBucket' },
update: { value: 'artveris' },
create: { key: 'ossBucket', value: 'artveris' },
});
expect(prisma.systemSetting.upsert).not.toHaveBeenCalled();
});
});
+17 -5
View File
@@ -137,17 +137,29 @@ export class SettingsService {
}
async update(values: Record<string, string>): Promise<Record<string, string>> {
await this.updateAtomic(values);
return this.getAll();
}
async updateAtomic(values: Record<string, string>): Promise<void> {
const updates: Array<{ key: string; value: string }> = [];
for (const [key, value] of Object.entries(values)) {
if (!EDITABLE_KEYS.includes(key)) continue;
if (MASKED_KEYS.has(key) && this.isMaskedSecret(value)) continue;
const nextValue = SECRET_KEYS.has(key) ? await this.encryptSecret(String(value)) : String(value);
await this.prisma.systemSetting.upsert({
where: { key },
update: { value: nextValue },
create: { key, value: nextValue },
updates.push({ key, value: nextValue });
}
if (updates.length) {
await this.prisma.$transaction(async (tx) => {
for (const { key, value } of updates) {
await tx.systemSetting.upsert({
where: { key },
update: { value },
create: { key, value },
});
}
});
}
return this.getAll();
}
// 服务内部直写(不做白名单/加密处理),用于系统自身生成的运行时值(如天河链钱包身份)