fix: 收口模板契约与安全门禁

统一模板包 visual 契约并补齐完成证据;修复 release 版本一致性、管理员 token 撤销、CORS 与默认品牌配置;收敛依赖安全风险并完成全量回归验证。
This commit is contained in:
que01 committed 2026-10-18 22:05:31 +08:00
1 parent 456171aa85
commit 4f0b50c7f1
84 files changed
+7367 -5180

No files matched your search

+95 -5
View File
@@ -13,6 +13,7 @@ import type { TemplatePackageManifest } from "@personal-brand/config";
import type { CreateTrustContentDto, UpdateTrustContentDto } from "./admin.dto.js";
import type { RollbackTemplateDto, UpgradeTemplateDto } from "./admin.dto.js";
import { PrismaService } from "./prisma.service.js";
import { recordTemplateOperationFailure } from "./modules/admin/template-operation-failure.js";
@Injectable()
export class TemplateWorkspaceService {
@@ -27,7 +28,7 @@ export class TemplateWorkspaceService {
const manifest = record(
installation.templatePackageVersion.manifestSnapshot,
) as unknown as TemplatePackageManifest;
if (validateTemplatePackageManifest(manifest as unknown as TemplatePackageManifest).length)
if (validateTemplatePackageManifest(manifest).length)
throw new BadRequestException("当前安装的模板包不符合统一模板契约");
return manifest.trustContentTypes?.map((item) => item.type) ?? [];
}
@@ -88,7 +89,32 @@ export class TemplateWorkspaceService {
throw new BadRequestException("品牌行业模型与模板安装不一致,请修复安装数据");
return template.key;
}
async install(brandId: string, templateKey: BrandTemplateKey, templatePackageVersionId: string) {
async install(
brandId: string,
templateKey: BrandTemplateKey,
templatePackageVersionId: string,
actorId: string,
requestId?: string,
) {
try {
return await this.installTemplate(brandId, templateKey, templatePackageVersionId);
} catch (error) {
await recordTemplateOperationFailure(this.prisma, {
action: error instanceof ConflictException ? "install_conflict" : "install",
error,
...(requestId ? { requestId } : {}),
actorId,
brandId,
});
throw error;
}
}
private async installTemplate(
brandId: string,
templateKey: BrandTemplateKey,
templatePackageVersionId: string,
) {
const template = getBrandTemplate(templateKey);
if (!template) throw new BadRequestException("行业模板不存在");
const [existing, brand] = await Promise.all([
@@ -171,7 +197,7 @@ export class TemplateWorkspaceService {
) {
throw new BadRequestException("模板包版本与行业或视觉模板不匹配");
}
if (validateTemplatePackageManifest(manifest as unknown as TemplatePackageManifest).length)
if (validateTemplatePackageManifest(manifest).length)
throw new BadRequestException("模板包版本不符合统一模板契约");
return version;
}
@@ -185,6 +211,30 @@ export class TemplateWorkspaceService {
},
actorId: string,
requestId?: string,
) {
try {
return await this.selectVersionUnrecorded(brandId, body, actorId, requestId);
} catch (error) {
await recordTemplateOperationFailure(this.prisma, {
action: "select_version",
error,
...(requestId ? { requestId } : {}),
actorId,
brandId,
});
throw error;
}
}
private async selectVersionUnrecorded(
brandId: string,
body: {
templatePackageVersionId: string;
expectedCurrentPackageVersionId?: string | null;
confirm: boolean;
},
actorId: string,
requestId?: string,
) {
if (!body.confirm) throw new BadRequestException("选择模板版本必须显式确认");
const [installation, brand, target] = await Promise.all([
@@ -410,6 +460,26 @@ export class TemplateWorkspaceService {
};
}
async upgrade(brandId: string, body: UpgradeTemplateDto, actorId: string, requestId?: string) {
try {
return await this.upgradeUnrecorded(brandId, body, actorId, requestId);
} catch (error) {
await recordTemplateOperationFailure(this.prisma, {
action: error instanceof ConflictException ? "install_conflict" : "upgrade",
error,
...(requestId ? { requestId } : {}),
actorId,
brandId,
});
throw error;
}
}
private async upgradeUnrecorded(
brandId: string,
body: UpgradeTemplateDto,
actorId: string,
requestId?: string,
) {
if (!body.confirm) throw new BadRequestException("升级模板必须显式确认");
const installation = await this.installation(brandId);
const target = await this.prisma.templatePackageVersion.findUnique({
@@ -492,6 +562,26 @@ export class TemplateWorkspaceService {
});
}
async rollback(brandId: string, actorId: string, body: RollbackTemplateDto, requestId?: string) {
try {
return await this.rollbackUnrecorded(brandId, actorId, body, requestId);
} catch (error) {
await recordTemplateOperationFailure(this.prisma, {
action: "rollback",
error,
...(requestId ? { requestId } : {}),
actorId,
brandId,
});
throw error;
}
}
private async rollbackUnrecorded(
brandId: string,
actorId: string,
body: RollbackTemplateDto,
requestId?: string,
) {
if (!body.confirm) throw new BadRequestException("回滚模板必须显式确认");
const installation = await this.installation(brandId);
if (installation.templateVersion !== body.expectedCurrentVersion)
@@ -564,9 +654,9 @@ export class TemplateWorkspaceService {
}
}
async function readPackageFiles(
function readPackageFiles(
version: Pick<TemplatePackageVersion, "validationReport">,
): Promise<Record<string, unknown>[]> {
): Record<string, unknown>[] {
return manifestRecords(record(version.validationReport).files);
}