fix: 修复系统一致性与安全问题

This commit is contained in:
que01 committed 2026-10-14 17:36:46 +08:00
1 parent aaeec14733
commit f158f906d5
19 files changed
+194 -34

No files matched your search

+6
View File
@@ -19,6 +19,12 @@ export class AdminAccessService {
throw new UnauthorizedException("管理员登录态无效或已过期");
}
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
const user = await this.prisma.adminUser.findUnique({
where: { id: identity.subject },
select: { mustChangePassword: true },
});
if (!user || user.mustChangePassword)
throw new ForbiddenException("请先完成初始密码修改后再访问管理后台");
const membership = await this.prisma.brandMembership.findUnique({
where: { adminUserId_brandId: { adminUserId: identity.subject, brandId: identity.brandId } },
});
+1
View File
@@ -10,6 +10,7 @@ export class AdminMembershipGuard implements CanActivate {
canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest<FastifyRequest>();
if (request.url.startsWith("/admin/session/login")) return Promise.resolve(true);
if (request.url.startsWith("/admin/session/change-password")) return Promise.resolve(true);
if (!request.url.startsWith("/admin/")) return Promise.resolve(true);
if (
/^\/admin\/workspace\/(install|capabilities(?:\/|$)|template(?:\/|$)|fields(?:\/|$))/.test(
+1 -1
View File
@@ -164,7 +164,7 @@ class AdminWorkspaceController {
where: { brandId, source: "sample" },
});
const existingOffering = await this.prisma.offering.count({
where: { brandId, name: "序章·入户门" },
where: { brandId, name: { in: sample.offerings.map((item) => item.name) } },
});
if (existing || existingOffering) throw new BadRequestException("示例内容已经导入");
const result = await this.prisma.$transaction(async (tx) => {
@@ -11,7 +11,6 @@ export class UpdateMiniAppSettingsDto {
@MaxLength(200)
apiBaseUrl?: string;
@IsOptional()
@IsOptional()
@IsObject()
build?: Record<string, unknown>;
@@ -136,7 +136,7 @@ export class AdminUploadController {
const target = path.join(this.localMediaRoot, key);
await mkdir(path.dirname(target), { recursive: true });
return {
uploadUrl: `/api/admin/uploads/local?key=${encodeURIComponent(key)}`,
uploadUrl: `/admin/uploads/local?key=${encodeURIComponent(key)}`,
key,
publicUrl: `${this.localMediaBaseUrl}/${key}`,
mode: "local",
@@ -132,7 +132,8 @@ export class CustomerInquirySubmissionController {
if (field.type === "number" && !Number.isFinite(Number(value)))
throw new BadRequestException(`${label}必须是数字`);
}
const allowedKeys = new Set(fields.map((field) => String(field.key)));
const visibleFields = fields.filter((field) => this.isVisibleField(field, body.values));
const allowedKeys = new Set(visibleFields.map((field) => String(field.key)));
const values = Object.fromEntries(
Object.entries(body.values).filter(([key]) => allowedKeys.has(key)),
);
@@ -150,4 +151,29 @@ export class CustomerInquirySubmissionController {
});
return this.present(submission);
}
private visibilityRuleMatches(rule: unknown, values: Record<string, unknown>): boolean {
if (!rule || typeof rule !== "object") return true;
const item = rule as Record<string, unknown>;
if (typeof item.field !== "string") return true;
const value = values[item.field];
switch (item.operator) {
case "exists":
return Array.isArray(value) ? value.length > 0 : Boolean(value);
case "equals":
return value === item.value;
case "not-equals":
return value !== item.value;
case "in":
return Array.isArray(item.value) && typeof value === "string" && item.value.includes(value);
default:
return true;
}
}
private isVisibleField(field: Record<string, unknown>, values: Record<string, unknown>): boolean {
return (Array.isArray(field.visibleWhen) ? field.visibleWhen : []).every((rule) =>
this.visibilityRuleMatches(rule, values),
);
}
}
@@ -1,4 +1,11 @@
import { BadRequestException, Controller, Get, Param, Query } from "@nestjs/common";
import {
BadRequestException,
Controller,
Get,
NotFoundException,
Param,
Query,
} from "@nestjs/common";
import { PublicBrandContextService } from "./public-brand-context.service.js";
import { BrandReleaseService } from "../../brand-release.service.js";
@@ -34,7 +41,7 @@ export class PublicOfferingController {
const item = (snapshot?.offerings ?? [])
.map((raw) => this.object(raw))
.find((offering) => offering.id === id && offering.isEnabled === true);
if (!item) throw new BadRequestException("业务提供项不存在");
if (!item) throw new NotFoundException("业务提供项不存在");
const forms = (snapshot?.forms ?? []).filter((raw) => {
const form = this.object(raw);
return form.offeringId === id && form.isEnabled === true;
@@ -149,6 +149,35 @@ export function toRuntimeInquiryFields(snapshot: Prisma.JsonValue): RuntimeInqui
? [{ label: item.label, value: item.value }]
: [];
});
const visibleWhen = asArray(value.visibleWhen).flatMap((rule) => {
if (!rule || typeof rule !== "object") return [];
const item = rule as Record<string, unknown>;
if (typeof item.field !== "string") return [];
const operator: "equals" | "not-equals" | "exists" | "in" = item.operator as
"equals" | "not-equals" | "exists" | "in";
if (
operator !== "equals" &&
operator !== "not-equals" &&
operator !== "exists" &&
operator !== "in"
)
return [];
const ruleValue = item.value;
const normalizedValue =
typeof ruleValue === "string" ||
typeof ruleValue === "number" ||
typeof ruleValue === "boolean" ||
(Array.isArray(ruleValue) && ruleValue.every((entry) => typeof entry === "string"))
? ruleValue
: undefined;
return [
{
field: item.field,
operator,
...(normalizedValue === undefined ? {} : { value: normalizedValue }),
},
];
});
return [
{
key: value.key,
@@ -156,6 +185,7 @@ export function toRuntimeInquiryFields(snapshot: Prisma.JsonValue): RuntimeInqui
type,
required: value.required === true,
...(options.length ? { options } : {}),
...(visibleWhen.length ? { visibleWhen } : {}),
},
];
});