fix(cr2-backend): 工作台包凭据拒绝与并发幂等、会话/任务终态收口
- CR-02/05/21:confirm 前解包拒绝凭据文件、清理被拒对象;V176 版本唯一键;并发 confirm 幂等 - CR-07/08/09/10:登录世代回读再签、心跳只停续期、终态任务不被失败回调改写、告警先推送再推进基线 - CR-11:品牌 Gating 写操作限平台超管 - CR-18:关键词分页钳制 + V177 四表 DDL - CR-20:中断恢复标志并上抛 - catch 棘轮按 9 处新增逐条点名更新基线 定向 154 例全绿(0 fail / 0 skip);V174/V175 未回改。
This commit is contained in:
@@ -260,13 +260,27 @@ public class AuthService {
|
||||
return authProperties.getCookieName();
|
||||
}
|
||||
|
||||
/** 登录推进世代;返回 0 行说明用户行已消失,不能再签发一个「世代比库还新」的令牌。 */
|
||||
/**
|
||||
* 登录推进世代;返回 0 行说明用户行已消失,不能再签发一个「世代比库还新」的令牌。
|
||||
*
|
||||
* <p>CR-07:自增用原子 UPDATE,但令牌必须按<b>回读到的库值</b>签发。两路并发登录
|
||||
* 都读到 N 时,库里会被推到 N+2;若各自按内存 N+1 签,两张 token 都落后一代,
|
||||
* 登录成功后第一次请求就被 session_epoch 校验打成 401。回读牺牲「两张 token 世代
|
||||
* 完全相同」的极端情形,换取任何一张都不会低于库值。</p>
|
||||
*/
|
||||
private void advanceSessionEpoch(LoginUserEntity user) {
|
||||
int updated = loginUserMapper.incrementSessionEpoch(user.getId());
|
||||
if (updated != 1) {
|
||||
log.warn("[auth] 登录推进会话世代失败 userId={} updated={}", user.getId(), updated);
|
||||
throw new BusinessException(500, "登录状态更新失败,请重试");
|
||||
}
|
||||
LoginUserEntity refreshed = loginUserMapper.selectById(user.getId());
|
||||
if (refreshed != null && refreshed.getSessionEpoch() != null) {
|
||||
user.setSessionEpoch(refreshed.getSessionEpoch());
|
||||
return;
|
||||
}
|
||||
// 自增已成功却回读不到(极端主从延迟/半提交):退回内存 +1 只作防御,
|
||||
// 不掩盖问题——下一轮校验会按库值把 stale token 拒掉。
|
||||
long current = user.getSessionEpoch() == null ? 0L : user.getSessionEpoch();
|
||||
user.setSessionEpoch(current + 1);
|
||||
}
|
||||
|
||||
+27
-5
@@ -1,8 +1,10 @@
|
||||
package com.nanri.aiimage.modules.brandgating.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingAddRequest;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingImportRequest;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingStatusRequest;
|
||||
@@ -20,6 +22,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@@ -40,6 +43,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@RequestMapping("/api/admin/brand-gating")
|
||||
@Tag(name = "品牌Gating库(后台)", description = "品牌Gating库的列表/筛选/统计、单条新增、启停、删除与批量导入。")
|
||||
public class BrandGatingController {
|
||||
@@ -79,7 +83,7 @@ public class BrandGatingController {
|
||||
public ApiResponse<BrandGatingAddResultVo> add(
|
||||
HttpServletRequest request,
|
||||
@Valid @RequestBody BrandGatingAddRequest body) {
|
||||
requireBrandGatingAccess(request);
|
||||
requirePlatformSuperAdmin(request);
|
||||
return ApiResponse.success(brandGatingService.add(body));
|
||||
}
|
||||
|
||||
@@ -88,7 +92,7 @@ public class BrandGatingController {
|
||||
public ApiResponse<BrandGatingDeleteResultVo> delete(
|
||||
HttpServletRequest request,
|
||||
@PathVariable("id") Long id) {
|
||||
requireBrandGatingAccess(request);
|
||||
requirePlatformSuperAdmin(request);
|
||||
return ApiResponse.success(brandGatingService.delete(id));
|
||||
}
|
||||
|
||||
@@ -98,7 +102,7 @@ public class BrandGatingController {
|
||||
HttpServletRequest request,
|
||||
@PathVariable("id") Long id,
|
||||
@Valid @RequestBody BrandGatingStatusRequest body) {
|
||||
requireBrandGatingAccess(request);
|
||||
requirePlatformSuperAdmin(request);
|
||||
return ApiResponse.success(brandGatingService.updateStatus(id, body.getStatus()));
|
||||
}
|
||||
|
||||
@@ -107,13 +111,31 @@ public class BrandGatingController {
|
||||
public ApiResponse<BrandGatingImportResultVo> importRows(
|
||||
HttpServletRequest request,
|
||||
@RequestBody BrandGatingImportRequest body) {
|
||||
requireBrandGatingAccess(request);
|
||||
requirePlatformSuperAdmin(request);
|
||||
return ApiResponse.success(brandGatingService.importRows(body));
|
||||
}
|
||||
|
||||
/** 逐端点把门:登录态 + 「品牌Gating库」后台菜单权限(admin_brand_gating)。 */
|
||||
/** 只读端点:登录态 + 「品牌Gating库」后台菜单权限(admin_brand_gating)。 */
|
||||
private void requireBrandGatingAccess(HttpServletRequest request) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireUser(request);
|
||||
permissionMenuService.requireConsolePageAccess(operator, DENIAL_MESSAGE, PERMISSION_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写端点(新增/启停/删除/批量导入):在菜单权限之上再要求平台超级管理员。
|
||||
*
|
||||
* <p>CR-11:{@code biz_brand_gating} 全平台共享、没有 company_id;任一公司管理员被授予
|
||||
* {@code admin_brand_gating} 后就能物理删除/启停影响全平台的 gating 行。读保持原口径,
|
||||
* 写收窄到平台超管(与 {@code AdminDeviceLogController.requireSuperAdmin} 同一判据)。</p>
|
||||
*/
|
||||
private AdminUserEntity requirePlatformSuperAdmin(HttpServletRequest request) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireUser(request);
|
||||
permissionMenuService.requireConsolePageAccess(operator, DENIAL_MESSAGE, PERMISSION_KEY);
|
||||
if (!DeviceSessionPolicy.isSuper(adminAuthSupport.currentRole(operator))) {
|
||||
log.warn("[brand-gating] 非平台超管写操作被拒 userId={} username={} role={}",
|
||||
operator.getId(), operator.getUsername(), adminAuthSupport.currentRole(operator));
|
||||
throw new BusinessException(403, "仅平台超级管理员可以修改品牌Gating库");
|
||||
}
|
||||
return operator;
|
||||
}
|
||||
}
|
||||
|
||||
+34
@@ -24,6 +24,9 @@ import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HexFormat;
|
||||
@@ -351,6 +354,37 @@ public class OssStorageService {
|
||||
return HexFormat.of().formatHex(digest.digest());
|
||||
}
|
||||
|
||||
/**
|
||||
* 把工作台客户端包流式落到本地临时文件,供 confirm 阶段做 zip 内容检查(CR-02)。
|
||||
*
|
||||
* <p>包体上限 512MB,不能整包读入内存;下载按 1MB 块流式拷贝,临时文件由调用方负责删除。
|
||||
* 该方法只做传输,不做内容判定。</p>
|
||||
*/
|
||||
public Path downloadWorkbenchClientObjectToTempFile(String objectKey) {
|
||||
requireWorkbenchClientKey(objectKey);
|
||||
Path temp = null;
|
||||
try {
|
||||
temp = Files.createTempFile("workbench-client-package-", ".zip");
|
||||
try (InputStream stream = buildClient().getObject(GetObjectArgs.builder()
|
||||
.bucket(publicClientBucket())
|
||||
.object(objectKey)
|
||||
.build())) {
|
||||
Files.copy(stream, temp, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
return temp;
|
||||
} catch (Exception ex) {
|
||||
if (temp != null) {
|
||||
try {
|
||||
Files.deleteIfExists(temp);
|
||||
} catch (Exception cleanupFailure) {
|
||||
log.warn("[oss] 清理工作台安装包临时文件失败 path={} err={}",
|
||||
temp, cleanupFailure.getMessage());
|
||||
}
|
||||
}
|
||||
throw storageFailure("download", objectKey, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/** 工作台客户端包公开下载 URL:优先 download-endpoint(主机 B 反代回源),未配置回退 public-endpoint。 */
|
||||
public String getWorkbenchClientDownloadUrl(String objectKey) {
|
||||
requireWorkbenchClientKey(objectKey);
|
||||
|
||||
+7
-1
@@ -28,6 +28,12 @@ public class KeywordUsageAdminService {
|
||||
|
||||
private static final long DEFAULT_PAGE_SIZE = 15;
|
||||
private static final long MAX_PAGE_SIZE = 100;
|
||||
/**
|
||||
* 页码上限(CR-18):三表 UNION 的深分页本身昂贵,且 {@code (page-1)*pageSize}
|
||||
* 在 {@code page=Long.MAX_VALUE} 时会溢出成负数,生成 {@code LIMIT -N,15} 直接 500。
|
||||
* 这里钳到安全上限;10000 页 × 100 条 = 100 万行,已远超实际可翻范围。
|
||||
*/
|
||||
private static final long MAX_PAGE = 10_000;
|
||||
private static final List<String> BASE_COUNTRIES = List.of("英国", "德国", "法国", "西班牙", "意大利");
|
||||
|
||||
private final KeywordUsageAdminMapper keywordUsageAdminMapper;
|
||||
@@ -100,7 +106,7 @@ public class KeywordUsageAdminService {
|
||||
filter.setUser(trimToNull(user));
|
||||
filter.setCountry(normalizeCountry(country));
|
||||
filter.setKeyword(trimToNull(keyword));
|
||||
long safePage = page == null || page < 1 ? 1 : page;
|
||||
long safePage = page == null || page < 1 ? 1 : Math.min(page, MAX_PAGE);
|
||||
long safeSize = pageSize == null ? DEFAULT_PAGE_SIZE : Math.min(Math.max(pageSize, 1), MAX_PAGE_SIZE);
|
||||
filter.setOffset((safePage - 1) * safeSize);
|
||||
filter.setLimit(safeSize);
|
||||
|
||||
+7
@@ -3033,6 +3033,13 @@ public class ShopDataCrawlTaskService extends AbstractFileTaskPipeline<ShopDataC
|
||||
FileTaskEntity task = fileTaskMapper.selectById(job.getTaskId());
|
||||
if (task == null || !MODULE_TYPE.equals(task.getModuleType())) return;
|
||||
ensureTaskOwnedByCurrentInstance(task, "fail shop data crawl result file job");
|
||||
// CR-09:终态守卫。终态父任务上仍可能遗留 PENDING/RUNNING 的结果作业,
|
||||
// 兜底扫描走到这里时不能把已 SUCCESS/CANCELLED/FAILED 的任务无条件改写成 FAILED。
|
||||
if (isTerminalTaskStatus(task.getStatus())) {
|
||||
log.info("[shop-data-crawl] skip result job failure because parent task is terminal taskId={} status={} jobId={}",
|
||||
task.getId(), task.getStatus(), job.getId());
|
||||
return;
|
||||
}
|
||||
FileResultEntity row = job.getResultId() == null ? null : fileResultMapper.selectById(job.getResultId());
|
||||
if (row != null && MODULE_TYPE.equals(row.getModuleType())) markResultFailed(row, message);
|
||||
List<FileResultEntity> rows = listScopeRows(job.getTaskId());
|
||||
|
||||
+5
@@ -264,6 +264,11 @@ public class SimilarAsinLlmService {
|
||||
for (int i = 0; i < futures.size(); i++) {
|
||||
try {
|
||||
merged.add(FutureDeadline.await(futures.get(i), futureDeadlineMillis(), "LLM 单行处理"));
|
||||
} catch (InterruptedException ex) {
|
||||
// CR-20:中断不是「这一行失败」。恢复中断标志并向上抛,不能落入下面的
|
||||
// 宽泛 Exception 分支把 InterruptedException 吞掉(worker 会继续跑完剩余行)。
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("LLM 单行等待被中断", ex);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[similar-asin][llm] row failed index={} asin={} err={}",
|
||||
i, rows.get(i).getAsin(), failureMessage(ex));
|
||||
|
||||
+30
-5
@@ -206,7 +206,8 @@ public class TaskResultFileJobWorker {
|
||||
processClaimedWithHeartbeat(job, claim);
|
||||
}
|
||||
|
||||
private ScheduledFuture<?> startJobHeartbeat(TaskFileJobEntity job) {
|
||||
private ScheduledFuture<?> startJobHeartbeat(TaskFileJobEntity job,
|
||||
AtomicBoolean parentTerminalObserved) {
|
||||
long interval = Math.max(1000L, heartbeatIntervalMillis);
|
||||
AtomicBoolean terminalStopped = new AtomicBoolean(false);
|
||||
return jobHeartbeatExecutor.scheduleWithFixedDelay(() -> {
|
||||
@@ -216,6 +217,7 @@ public class TaskResultFileJobWorker {
|
||||
try {
|
||||
if (taskFileJobService.isOwningTaskTerminal(job.getTaskId())) {
|
||||
if (terminalStopped.compareAndSet(false, true)) {
|
||||
parentTerminalObserved.set(true);
|
||||
TaskDistributedLockService.LockHandle handle = activeLockHandles.get(job.getId());
|
||||
if (handle != null) {
|
||||
// 良性停续期:父任务已终态,不是续期失败,不能打 renewalLost=true 造成假告警
|
||||
@@ -223,9 +225,11 @@ public class TaskResultFileJobWorker {
|
||||
}
|
||||
// 终态后不再 touchRunning:否则 job 的 updated_at 一直被刷新,
|
||||
// job 级 stuck 扫描永远看不到这行,成为不可回收的 RUNNING 遗骸。
|
||||
// 判死但不打 terminal_callback_at:留给兜底扫描出部分结果,失败任务仍可下载。
|
||||
taskFileJobService.markFailedPermanentAwaitingFallback(job, "父任务已终态,结果作业不再执行");
|
||||
log.info("[task-file-job] parent task terminal; stopped renewal and failed job jobId={} taskId={} moduleType={} stage=TERMINAL_STOP_RENEW",
|
||||
// CR-08:这里只停续期/停心跳,**不判死**。执行线程可能正在写完整结果,
|
||||
// 心跳抢先把行翻成 FAILED 会让随后的 markSuccess(.eq(status,RUNNING))
|
||||
// 丢掉成功状态,下一次兜底扫描立刻出部分结果。终态判定统一放到
|
||||
// processClaimedWithHeartbeat 的 finally —— 执行线程退出后再看这一行。
|
||||
log.info("[task-file-job] parent task terminal; stopped renewal, waiting for execution thread jobId={} taskId={} moduleType={} stage=TERMINAL_STOP_RENEW",
|
||||
job.getId(), job.getTaskId(), job.getModuleType());
|
||||
}
|
||||
return;
|
||||
@@ -250,14 +254,35 @@ public class TaskResultFileJobWorker {
|
||||
job.getId(), job.getTaskId(), job.getModuleType(), claim.getUpdatedAt());
|
||||
return;
|
||||
}
|
||||
ScheduledFuture<?> heartbeat = startJobHeartbeat(job);
|
||||
AtomicBoolean parentTerminalObserved = new AtomicBoolean(false);
|
||||
ScheduledFuture<?> heartbeat = startJobHeartbeat(job, parentTerminalObserved);
|
||||
try {
|
||||
processInternal(job);
|
||||
} finally {
|
||||
heartbeat.cancel(false);
|
||||
// CR-08:心跳只负责停续期;终态判定必须等执行线程退出后再做。
|
||||
// 此时若 markSuccess 已落库,finishAfterExecution 会保留 SUCCESS;
|
||||
// 只有执行线程没写出成功状态的行才判死并留给兜底扫描出部分结果。
|
||||
finishAfterExecution(job, parentTerminalObserved);
|
||||
}
|
||||
}
|
||||
|
||||
/** 执行线程退出后的终态收口:心跳期间观察到父任务终态才触发,绝不覆盖已落库的 SUCCESS。 */
|
||||
void finishAfterExecution(TaskFileJobEntity job, AtomicBoolean parentTerminalObserved) {
|
||||
if (job == null || !parentTerminalObserved.get()) {
|
||||
return;
|
||||
}
|
||||
TaskFileJobEntity latest = taskFileJobService.findById(job.getId());
|
||||
if (latest != null && "SUCCESS".equals(latest.getStatus())) {
|
||||
log.info("[task-file-job] parent task terminal after execution but job already succeeded; keep success jobId={} taskId={} moduleType={} stage=TERMINAL_KEEP_SUCCESS",
|
||||
job.getId(), job.getTaskId(), job.getModuleType());
|
||||
return;
|
||||
}
|
||||
taskFileJobService.markFailedPermanentAwaitingFallback(job, "父任务已终态,结果作业不再执行");
|
||||
log.info("[task-file-job] parent task terminal after execution; failed job for fallback jobId={} taskId={} moduleType={} stage=TERMINAL_AFTER_EXECUTION",
|
||||
job.getId(), job.getTaskId(), job.getModuleType());
|
||||
}
|
||||
|
||||
private void processInternal(TaskFileJobEntity job) {
|
||||
long startedAt = System.currentTimeMillis();
|
||||
try {
|
||||
|
||||
+22
-6
@@ -162,15 +162,31 @@ public class CostGuardScheduler {
|
||||
} else if (crossed(before.proxy, platform.proxy, threshold)) {
|
||||
reason = platformReason(platform.proxy, threshold);
|
||||
}
|
||||
baselineStore.put(PLATFORM_USER_ID, new CostGuardBaselineStore.Baseline(
|
||||
platform.proxy, platform.llmCalls, platform.llmTokens, nowMillis));
|
||||
if (reason == null) {
|
||||
baselineStore.put(PLATFORM_USER_ID, new CostGuardBaselineStore.Baseline(
|
||||
platform.proxy, platform.llmCalls, platform.llmTokens, nowMillis));
|
||||
return;
|
||||
}
|
||||
int pushed = notificationDispatchService.pushToAdmins(NotificationService.SCENE_COST_GUARD,
|
||||
NotificationService.LEVEL_WARNING, "平台自费代理提取超告警线",
|
||||
reason + "。这笔消耗没有归属账号、不能自动暂停,请检查 cookie 生成器 / 平台兜底池的提取链接档位。",
|
||||
"cost_guard_platform_proxy:" + LocalDate.now(), null);
|
||||
// CR-10:告警先推送成功、再推进基线。先 put 后 push 时,推送抛异常/返回 0
|
||||
// 会让当天基线已越过阈值,下一轮 crossed 不再成立,告警永久丢失。
|
||||
int pushed;
|
||||
try {
|
||||
pushed = notificationDispatchService.pushToAdmins(NotificationService.SCENE_COST_GUARD,
|
||||
NotificationService.LEVEL_WARNING, "平台自费代理提取超告警线",
|
||||
reason + "。这笔消耗没有归属账号、不能自动暂停,请检查 cookie 生成器 / 平台兜底池的提取链接档位。",
|
||||
"cost_guard_platform_proxy:" + LocalDate.now(), null);
|
||||
} catch (RuntimeException ex) {
|
||||
log.warn("[cost-guard] 平台自费告警推送失败,本轮不推进基线待下轮重试 次数={} 阈值={} err={}",
|
||||
platform.proxy, threshold, ex.getMessage());
|
||||
return;
|
||||
}
|
||||
if (pushed <= 0) {
|
||||
log.warn("[cost-guard] 平台自费告警没有落库到任何管理员,本轮不推进基线待下轮重试 次数={} 阈值={}",
|
||||
platform.proxy, threshold);
|
||||
return;
|
||||
}
|
||||
baselineStore.put(PLATFORM_USER_ID, new CostGuardBaselineStore.Baseline(
|
||||
platform.proxy, platform.llmCalls, platform.llmTokens, nowMillis));
|
||||
log.warn("[cost-guard] 平台自费代理提取告警 次数={} 阈值={} 管理员落库={}",
|
||||
platform.proxy, threshold, pushed);
|
||||
}
|
||||
|
||||
+153
-6
@@ -8,12 +8,17 @@ import com.nanri.aiimage.modules.workbenchclient.model.entity.WorkbenchClientPac
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -22,6 +27,8 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
/**
|
||||
* 数富AI工作台客户端安装/更新包管理。
|
||||
@@ -48,6 +55,24 @@ public class WorkbenchClientPackageService {
|
||||
/** 直传预签名默认有效期:90MB 级包在慢公网入口上传时,3 分钟窗口会在途过期。 */
|
||||
private static final int DEFAULT_PRESIGN_EXPIRY_SECONDS = 3600;
|
||||
|
||||
/**
|
||||
* 安装包内禁止出现的凭据文件基名(大小写不敏感)。
|
||||
*
|
||||
* <p>CR-02:工作台安装包是免登录公开下载的,包内一旦带 {@code kw_share_cfg.json}
|
||||
* 这类文件就等于把 API 口令连同下载链接一起公开。此清单只做发布前硬拒绝,
|
||||
* 真实凭据轮换由工作台客户端仓库完成(本仓不打包它)。</p>
|
||||
*/
|
||||
private static final Set<String> FORBIDDEN_CREDENTIAL_NAMES = Set.of(
|
||||
"kw_share_cfg.json", ".env", ".netrc", ".git-credentials",
|
||||
"id_rsa", "id_dsa", "id_ecdsa", "id_ed25519");
|
||||
|
||||
/** 凭据类后缀:私钥/证书/密钥库,命中任意一个即拒绝。 */
|
||||
private static final List<String> FORBIDDEN_CREDENTIAL_SUFFIXES =
|
||||
List.of(".pem", ".key", ".p12", ".pfx", ".jks", ".keystore");
|
||||
|
||||
/** zip 条目数上限:只读中央目录,但仍要防病态包把 confirm 拖成分钟级。 */
|
||||
private static final int MAX_ZIP_ENTRIES = 200_000;
|
||||
|
||||
/** 允许配置覆盖;上下限防止误配成 0 或过长窗口。 */
|
||||
@Value("${aiimage.workbench-client.presign-expiry-seconds:3600}")
|
||||
private int presignExpirySeconds = DEFAULT_PRESIGN_EXPIRY_SECONDS;
|
||||
@@ -131,6 +156,10 @@ public class WorkbenchClientPackageService {
|
||||
* 签发直传预签名:对象 key 由「毫秒时间戳 + 随机 UUID + 安全版本号」组成。
|
||||
* UUID 是 CR-08 的修复点:只靠毫秒时间戳时,多实例时钟相同或同毫秒两名管理员
|
||||
* 上传同一版本会生成同一个 key,后一次 PUT 覆盖前一次对象、先确认的 DB 行摘要失效。
|
||||
*
|
||||
* <p>CR-21:返回的上传 URL 视为<b>一次性凭据</b>——任何拿到它的人都能在 TTL 内写对象。
|
||||
* 3600 秒是覆盖慢公网 90MB 级包上传的最小必要窗口,只存在响应体里,
|
||||
* <b>不得写入日志、审计文本或异常信息</b>;确认完成后即失效,不提供续签。</p>
|
||||
*/
|
||||
public Map<String, Object> presignPackage(String fileName, String version) {
|
||||
String normalizedName = requireZipFileName(fileName);
|
||||
@@ -155,6 +184,11 @@ public class WorkbenchClientPackageService {
|
||||
/**
|
||||
* 直传确认:校验对象存在、大小上限、版本号与对象名一致,服务端复算 sha256 后落库。
|
||||
* <p>sha256 为空或长度不对直接拒绝——宁可让管理员重传,也不让客户端拿到没有锚点的包。
|
||||
* <p>CR-02:落库前把对象流式落到临时文件、用 {@link ZipFile} 只读扫描条目名;
|
||||
* 包内出现 {@code kw_share_cfg.json}、{@code .env}、{@code *.pem}、{@code *.key}
|
||||
* 等凭据文件即拒绝并清理已上传对象,避免公开桶里出现带口令的安装包。
|
||||
* <p>CR-05:同 objectKey 并发 confirm 幂等返回已登记行;同 version 不同 objectKey
|
||||
* 由数据库唯一键 {@code uk_workbench_client_version} 兜底,冲突时清理对象并返回业务错误。
|
||||
*/
|
||||
public Map<String, Object> confirmPackage(String objectKey, String fileName, String version) {
|
||||
if (objectKey == null || !objectKey.startsWith(OBJECT_KEY_PREFIX)) {
|
||||
@@ -170,10 +204,7 @@ public class WorkbenchClientPackageService {
|
||||
throw new BusinessException("版本号与上传对象不匹配,请重新上传");
|
||||
}
|
||||
// 幂等:重复 confirm 同一对象返回已登记行,不再报错(CR-08)
|
||||
WorkbenchClientPackageEntity existing = packageMapper.selectOne(
|
||||
new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
|
||||
.eq(WorkbenchClientPackageEntity::getObjectKey, objectKey)
|
||||
.last("LIMIT 1"));
|
||||
WorkbenchClientPackageEntity existing = findByObjectKey(objectKey);
|
||||
if (existing != null) {
|
||||
log.info("[workbench-client] 直传确认幂等命中 id={} objectKey={}", existing.getId(), objectKey);
|
||||
return toItemMap(existing);
|
||||
@@ -197,9 +228,11 @@ public class WorkbenchClientPackageService {
|
||||
Long sameVersion = packageMapper.selectCount(new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
|
||||
.eq(WorkbenchClientPackageEntity::getVersion, normalizedVersion));
|
||||
if (sameVersion != null && sameVersion > 0) {
|
||||
// 同版本不同字节会让已升级的装机按版本号比较后不升级,客户端还会拿到错误摘要
|
||||
// 同版本不同字节会让已升级的装机按版本号比较后不升级,客户端还会拿到错误摘要。
|
||||
// 拒绝时必须清理对象:512MB 级残留留在公开桶可被反复触发耗存储(CR-05)。
|
||||
log.warn("[workbench-client] 直传确认失败:版本已存在 version={} objectKey={}",
|
||||
normalizedVersion, objectKey);
|
||||
removeRejectedObject(objectKey);
|
||||
throw new BusinessException("该版本已存在,请提升版本号或先删除旧包");
|
||||
}
|
||||
WorkbenchClientPackageEntity latest = newest();
|
||||
@@ -207,8 +240,11 @@ public class WorkbenchClientPackageService {
|
||||
// 按上传时间取最新时,低版本后传会顶掉当前更新目标,已升级装机反而被判"无需更新"
|
||||
log.warn("[workbench-client] 直传确认失败:版本低于当前最新 version={} latest={} objectKey={}",
|
||||
normalizedVersion, latest.getVersion(), objectKey);
|
||||
removeRejectedObject(objectKey);
|
||||
throw new BusinessException("版本号不能低于当前最新版本 " + latest.getVersion());
|
||||
}
|
||||
// 内容检查排在版本/大小之后:先做便宜判定,避免为大包白下载一遍
|
||||
rejectCredentialFiles(objectKey);
|
||||
String sha256 = ossStorageService.workbenchClientObjectSha256(objectKey);
|
||||
if (sha256 == null || sha256.length() != 64) {
|
||||
log.warn("[workbench-client] 直传确认失败:摘要计算异常 objectKey={} sha256={}", objectKey, sha256);
|
||||
@@ -221,13 +257,124 @@ public class WorkbenchClientPackageService {
|
||||
entity.setObjectKey(objectKey);
|
||||
entity.setFileSize(size);
|
||||
entity.setSha256(sha256);
|
||||
packageMapper.insert(entity);
|
||||
try {
|
||||
packageMapper.insert(entity);
|
||||
} catch (DuplicateKeyException duplicate) {
|
||||
// 同 objectKey 并发 confirm:另一线程已登记,按幂等返回(CR-05)
|
||||
WorkbenchClientPackageEntity concurrent = findByObjectKey(objectKey);
|
||||
if (concurrent != null) {
|
||||
log.info("[workbench-client] 并发直传确认幂等命中 id={} objectKey={}",
|
||||
concurrent.getId(), objectKey);
|
||||
return toItemMap(concurrent);
|
||||
}
|
||||
// 同 version 不同 objectKey:唯一键 uk_workbench_client_version 拦下后一行
|
||||
log.warn("[workbench-client] 直传确认失败:版本并发冲突 version={} objectKey={}",
|
||||
normalizedVersion, objectKey);
|
||||
removeRejectedObject(objectKey);
|
||||
throw new BusinessException("该版本已存在,请提升版本号或先删除旧包");
|
||||
}
|
||||
log.info("[workbench-client] 客户端包登记成功 id={} version={} fileName={} bytes={} sha256={}",
|
||||
entity.getId(), normalizedVersion, normalizedName, size, sha256);
|
||||
WorkbenchClientPackageEntity saved = packageMapper.selectById(entity.getId());
|
||||
return toItemMap(saved == null ? entity : saved);
|
||||
}
|
||||
|
||||
private WorkbenchClientPackageEntity findByObjectKey(String objectKey) {
|
||||
return packageMapper.selectOne(new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
|
||||
.eq(WorkbenchClientPackageEntity::getObjectKey, objectKey)
|
||||
.last("LIMIT 1"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布前内容检查:把对象流式落到临时文件,只读扫描 zip 条目名。
|
||||
*
|
||||
* <p>不整包读入内存、不解压内容;{@link ZipFile} 只读中央目录。命中凭据文件或
|
||||
* 非法 zip 时删除已上传对象,确保公开桶里不会留下不可登记的大对象。</p>
|
||||
*/
|
||||
private void rejectCredentialFiles(String objectKey) {
|
||||
Path packageFile = ossStorageService.downloadWorkbenchClientObjectToTempFile(objectKey);
|
||||
try (ZipFile zipFile = new ZipFile(packageFile.toFile())) {
|
||||
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
int scanned = 0;
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = entries.nextElement();
|
||||
if (++scanned > MAX_ZIP_ENTRIES) {
|
||||
log.warn("[workbench-client] 直传确认拒绝:zip 条目数超限 objectKey={} limit={}",
|
||||
objectKey, MAX_ZIP_ENTRIES);
|
||||
removeRejectedObject(objectKey);
|
||||
throw new BusinessException("安装包内文件条目过多,请重新打包上传");
|
||||
}
|
||||
if (entry.isDirectory()) {
|
||||
continue;
|
||||
}
|
||||
String baseName = baseName(entry.getName());
|
||||
if (isCredentialFileName(baseName)) {
|
||||
log.warn("[workbench-client] 直传确认拒绝:安装包含凭据文件 objectKey={} entry={}",
|
||||
objectKey, entry.getName());
|
||||
removeRejectedObject(objectKey);
|
||||
throw new BusinessException("安装包内含凭据配置文件(" + baseName
|
||||
+ "),禁止发布,请移除后重新打包上传");
|
||||
}
|
||||
}
|
||||
} catch (BusinessException ex) {
|
||||
throw ex;
|
||||
} catch (IOException ex) {
|
||||
log.warn("[workbench-client] 直传确认失败:安装包不是有效 zip objectKey={} err={}",
|
||||
objectKey, ex.getMessage());
|
||||
removeRejectedObject(objectKey);
|
||||
throw new BusinessException("上传的安装包不是有效的 zip 文件,请重新打包上传");
|
||||
} finally {
|
||||
deleteTempQuietly(packageFile);
|
||||
}
|
||||
}
|
||||
|
||||
private static String baseName(String entryName) {
|
||||
if (entryName == null) {
|
||||
return "";
|
||||
}
|
||||
String normalized = entryName.replace('\\', '/');
|
||||
int slash = normalized.lastIndexOf('/');
|
||||
return slash < 0 ? normalized : normalized.substring(slash + 1);
|
||||
}
|
||||
|
||||
private static boolean isCredentialFileName(String fileName) {
|
||||
String lower = fileName == null ? "" : fileName.trim().toLowerCase(Locale.ROOT);
|
||||
if (lower.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (FORBIDDEN_CREDENTIAL_NAMES.contains(lower) || lower.startsWith(".env.")) {
|
||||
return true;
|
||||
}
|
||||
for (String suffix : FORBIDDEN_CREDENTIAL_SUFFIXES) {
|
||||
if (lower.endsWith(suffix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** 拒绝分支统一清对象;清理失败只告警,不让业务错误被存储异常盖掉。 */
|
||||
private void removeRejectedObject(String objectKey) {
|
||||
try {
|
||||
ossStorageService.removeWorkbenchClientObject(objectKey);
|
||||
} catch (RuntimeException ex) {
|
||||
log.warn("[workbench-client] 清理被拒安装包对象失败 objectKey={} err={}",
|
||||
objectKey, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteTempQuietly(Path packageFile) {
|
||||
if (packageFile == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Files.deleteIfExists(packageFile);
|
||||
} catch (IOException ex) {
|
||||
log.warn("[workbench-client] 删除安装包临时文件失败 path={} err={}",
|
||||
packageFile, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 id 批量删除记录;MinIO 对象仅在删除后无任何剩余记录引用时才清除。
|
||||
* <p>最新包被删后,客户端下次检查自动回落到上一版(按上传时间取最新)。
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
-- =============================================================
|
||||
-- 迁移 V176__workbench_client_package_version_unique
|
||||
-- 立账:ops 仓 docs/audit/CODE_REVIEW_REPORT.md CR-05(2026-10-26)
|
||||
--
|
||||
-- 1. 变更目的:给「数富AI工作台客户端包」的版本号加唯一约束。
|
||||
-- confirmPackage 原实现是 SELECT COUNT(version) 再 INSERT,两步之间无数据库
|
||||
-- 约束保护:两名管理员并发确认同一版本、不同 objectKey 时,两次 count 都读到 0,
|
||||
-- 最终落两行同 version、不同 sha256 的记录,客户端会拿到错误的完整性锚点。
|
||||
--
|
||||
-- 2. 口径:
|
||||
-- - 唯一键名 uk_workbench_client_version,列 version;
|
||||
-- - objectKey 已有 uk_workbench_client_object_key(V167),两把唯一键各守一条
|
||||
-- 并发路径:同对象幂等、同版本拒绝;
|
||||
-- - 线上只读核验(2026-10-26):当前 7 行、7 个不同 version,无重复,可安全加键。
|
||||
--
|
||||
-- 3. 影响表与数据量:biz_workbench_client_package 加一把唯一索引;表为 KB 级字典表。
|
||||
--
|
||||
-- 4. 回滚(如需):ALTER TABLE `biz_workbench_client_package`
|
||||
-- DROP INDEX `uk_workbench_client_version`;
|
||||
-- =============================================================
|
||||
|
||||
ALTER TABLE `biz_workbench_client_package`
|
||||
ADD UNIQUE KEY `uk_workbench_client_version` (`version`);
|
||||
@@ -0,0 +1,79 @@
|
||||
-- =============================================================
|
||||
-- 迁移 V177__keyword_usage_tables
|
||||
-- 立账:ops 仓 docs/audit/CODE_REVIEW_REPORT.md CR-18(2026-10-26)
|
||||
--
|
||||
-- 1. 变更目的:给「关键词使用记录」依赖的四张表补 Flyway DDL。
|
||||
-- keyword_usage / keyword_add_log / keyword_delete_log / keyword_share 在现网主库
|
||||
-- 早已存在(桌面 Tkinter 工具与 web 版共用),此前没有入库的建表脚本;新环境首次
|
||||
-- 打开管理后台会因表缺失直接 500。本迁移只服务新环境,线上四表已存在。
|
||||
--
|
||||
-- 2. 口径:
|
||||
-- - 一律 CREATE TABLE IF NOT EXISTS,线上重跑逐条 no-op;
|
||||
-- - 字段按 KeywordUsageAdminMapper.xml / KeywordShareMapper.xml 与
|
||||
-- KeywordUsageEntity / KeywordAddLogEntity / KeywordDeleteLogEntity /
|
||||
-- KeywordShareEntity 逐列核对,不臆造业务列;
|
||||
-- - keyword_share 与 keyword_usage 的 uk_comp_cw(company_id, country, keyword)
|
||||
-- 是既有 UPSERT(ON DUPLICATE KEY UPDATE)与删除/标记语义的依赖键,必须一并建;
|
||||
-- - keyword_delete_log 只留痕,不建唯一键(同一词可被反复添加/删除)。
|
||||
--
|
||||
-- 3. 影响表与数据量:新环境建 4 张空表;已有环境 4 条 no-op。
|
||||
--
|
||||
-- 4. 回滚(如需,按依赖顺序):
|
||||
-- DROP TABLE IF EXISTS `keyword_usage`;
|
||||
-- DROP TABLE IF EXISTS `keyword_add_log`;
|
||||
-- DROP TABLE IF EXISTS `keyword_delete_log`;
|
||||
-- DROP TABLE IF EXISTS `keyword_share`;
|
||||
-- =============================================================
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `keyword_share` (
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||
`country` VARCHAR(32) NOT NULL COMMENT '国家(英国/德国/法国/西班牙/意大利等)',
|
||||
`keyword` VARCHAR(255) NOT NULL COMMENT '关键词',
|
||||
`chinese` VARCHAR(255) DEFAULT NULL COMMENT '中文释义',
|
||||
`ktype` VARCHAR(64) DEFAULT NULL COMMENT '关键词类型',
|
||||
`source` VARCHAR(64) DEFAULT NULL COMMENT '来源',
|
||||
`category` VARCHAR(128) DEFAULT NULL COMMENT '一级类目',
|
||||
`sub_category` VARCHAR(128) DEFAULT NULL COMMENT '二级类目',
|
||||
`category_l3` VARCHAR(128) DEFAULT NULL COMMENT '三级类目',
|
||||
`category_l4` VARCHAR(128) DEFAULT NULL COMMENT '四级类目',
|
||||
`user_name` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '添加人',
|
||||
`add_time` DATETIME DEFAULT NULL COMMENT '添加时间',
|
||||
`company_id` BIGINT NOT NULL DEFAULT 1 COMMENT '公司隔离键',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_comp_cw` (`company_id`, `country`, `keyword`),
|
||||
KEY `idx_keyword_share_company_category` (`company_id`, `category`, `sub_category`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='五国关键词共享-词本体';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `keyword_usage` (
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||
`country` VARCHAR(32) NOT NULL COMMENT '国家',
|
||||
`keyword` VARCHAR(255) NOT NULL COMMENT '关键词',
|
||||
`user_name` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '使用者',
|
||||
`used_time` DATETIME DEFAULT NULL COMMENT '最近使用时间',
|
||||
`company_id` BIGINT NOT NULL DEFAULT 1 COMMENT '公司隔离键',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_comp_cw` (`company_id`, `country`, `keyword`),
|
||||
KEY `idx_keyword_usage_company_time` (`company_id`, `used_time`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='五国关键词共享-使用状态';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `keyword_add_log` (
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||
`country` VARCHAR(32) NOT NULL COMMENT '国家',
|
||||
`keyword` VARCHAR(255) NOT NULL COMMENT '关键词',
|
||||
`user_name` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '添加人',
|
||||
`add_time` DATETIME DEFAULT NULL COMMENT '添加时间',
|
||||
`company_id` BIGINT NOT NULL DEFAULT 1 COMMENT '公司隔离键',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_keyword_add_log_company_time` (`company_id`, `add_time`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='五国关键词共享-添加留痕';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `keyword_delete_log` (
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||
`country` VARCHAR(32) NOT NULL COMMENT '国家',
|
||||
`keyword` VARCHAR(255) NOT NULL COMMENT '关键词',
|
||||
`user_name` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '删除人',
|
||||
`delete_time` DATETIME DEFAULT NULL COMMENT '删除时间',
|
||||
`company_id` BIGINT NOT NULL DEFAULT 1 COMMENT '公司隔离键',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_keyword_delete_log_company_time` (`company_id`, `delete_time`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='五国关键词共享-删除留痕';
|
||||
Reference in New Issue
Block a user