fix:"模型准入改为公平排队并收紧渠道凭据与目录暴露"

- acquire-pair.lua 增加模型到达顺序队列:空闲槽位先给更早等待者,超时/放弃/过期即出队,仅因自身 Key 满的等待者不占模型队位
- 修改渠道 Base URL 必须重新填写凭据,阻止已存密钥被发送到新地址;审计记录新旧地址
- 普通用户模型目录只返回渠道 id/name/status,不再暴露 Base URL 和凭据掩码
- 管理端用量 CSV 导出中和公式前缀
- 新增真实 Redis 公平准入、渠道凭据重填、目录字段和 CSV 回归

验证:mvn -B -o clean verify 403 项通过(独立 PostgreSQL/Redis)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
xppandClaude Opus 5.5 committed 2026-10-22 12:06:41 +08:00
1 parent 2e53b1d9c8
commit 1a608f8ff8
9 files changed
+178 -16

No files matched your search

@@ -30,7 +30,7 @@ public class CatalogController {
@GetMapping("/models")
public Map<String, Object> models(jakarta.servlet.http.HttpServletRequest request) {
authService.requireUser(AuthController.sessionToken(request));
return Map.of("providers", catalogService.listProviders(false), "models", catalogService.listModels(false), "logicalModels", catalogService.listDefinitions(false));
return Map.of("providers", catalogService.listPublicProviders(), "models", catalogService.listModels(false), "logicalModels", catalogService.listDefinitions(false));
}
@GetMapping("/overview")
@@ -149,6 +149,12 @@ public class CatalogService {
boolean publishingChannel = !"active".equals(previous.get("status")) && "active".equals(nextStatus);
jdbc.update("UPDATE model_providers SET code=?, name=?, protocol=?, source_type=?, base_url=?, description=?, credential_type=?, status=?, updated_at=now() WHERE id=?", code,name,protocol,sourceType,baseUrl,optional(value,"description",""),credentialType,nextStatus,id);
String secret=optional(input,"credentialSecret","");
// A stored credential is bound to the endpoint it was entered for. Redirecting it elsewhere
// would send the decrypted key to the new host, so changing the endpoint requires re-entry.
boolean endpointChanged=!java.util.Objects.equals(normalizedEndpoint(previous.get("baseUrl")),normalizedEndpoint(baseUrl));
if (endpointChanged && !"none".equals(credentialType) && secret.isBlank() && previous.get("credentialMasked")!=null) {
throw new ApiException("CREDENTIAL_REENTRY_REQUIRED","修改渠道地址时须重新填写凭据,已保存的凭据不会发送到新地址");
}
if ("none".equals(credentialType)) jdbc.update("UPDATE model_providers SET credential_ciphertext=NULL, credential_masked=NULL WHERE id=?",id);
else if (!secret.isBlank()) jdbc.update("UPDATE model_providers SET credential_ciphertext=?, credential_masked=? WHERE id=?",secretCipher.encrypt(secret),mask(secret),id);
if (publishingChannel) {
@@ -157,10 +163,27 @@ public class CatalogService {
// an administrator can still disable individual groups afterwards.
jdbc.update("UPDATE model_catalog SET status='active', updated_at=now() WHERE provider_id=? AND status='disabled' AND pricing IS NOT NULL AND deleted_at IS NULL", id);
}
auth.audit("provider.updated",admin.id(),new AuthService.RequestMeta(null,null),Map.of("providerId",id));
Map<String,Object> details=new LinkedHashMap<>();
details.put("providerId",id);
if (endpointChanged) { details.put("previousBaseUrl",String.valueOf(previous.get("baseUrl"))); details.put("baseUrl",String.valueOf(baseUrl)); }
if (!secret.isBlank() || "none".equals(credentialType)) details.put("credentialReplaced",true);
auth.audit("provider.updated",admin.id(),new AuthService.RequestMeta(null,null),details);
return findProvider(id);
}
private static String normalizedEndpoint(Object value) {
return value == null || String.valueOf(value).isBlank() ? null : String.valueOf(value).trim().replaceAll("/+$", "").toLowerCase(java.util.Locale.ROOT);
}
/** Ordinary users only need channel identity for display; endpoints and credential hints stay admin-only. */
public List<Map<String, Object>> listPublicProviders() {
return listProviders(false).stream().map(provider -> {
Map<String, Object> visible = new LinkedHashMap<>();
for (String field : List.of("id", "name", "status")) if (provider.get(field) != null) visible.put(field, provider.get(field));
return visible;
}).toList();
}
private void validateBaseUrl(String value) {
if (value == null || value.isBlank()) return;
try {
@@ -228,6 +228,7 @@ public class GatewayGuard {
String modelBucket = redisPrefix + "gateway:slots:model:" + model.get("id");
String keyBucket = redisPrefix + "gateway:slots:key:" + keyId;
String waitingBucket = redisPrefix + "gateway:waiting:model:" + model.get("id");
String orderBucket = redisPrefix + "gateway:order:model:" + model.get("id");
String token = UUID.randomUUID().toString();
long waitMs = Math.max(0, Math.min(queueWaitMs, requestedWaitMs > 0 ? requestedWaitMs : queueWaitMs));
long deadline = System.nanoTime() + java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(waitMs);
@@ -249,7 +250,7 @@ public class GatewayGuard {
}
while (true) {
long remaining = Math.max(0, java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime()));
long acquired = executePair(modelBucket, keyBucket, waitingBucket, Integer.toString(modelLimit), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token, Long.toString(remaining), Long.toString(Math.max(SLOT_TTL_MS, waitMs + 1000)));
long acquired = executePair(modelBucket, keyBucket, waitingBucket, orderBucket, Integer.toString(modelLimit), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token, Long.toString(remaining), Long.toString(Math.max(SLOT_TTL_MS, waitMs + 1000)));
if (acquired == 1) {
admitted = true; // The acquisition script also removed the waiting entry.
return new Admission(modelBucket, keyBucket, token);
@@ -266,7 +267,11 @@ public class GatewayGuard {
}
}
} finally {
if (!admitted && waitMs > 0) releaseQuietly(waitingBucket, token);
if (!admitted && waitMs > 0) {
// Leaving the queue must also release this caller's FIFO position.
releaseQuietly(waitingBucket, token);
releaseQuietly(orderBucket, token);
}
}
}
@@ -301,9 +306,9 @@ public class GatewayGuard {
throw new GatewayLimitException("GATEWAY_GUARD_UNAVAILABLE", "网关流量控制暂不可用,请稍后重试", 503, 3);
}
private long executePair(String modelKey, String keyKey, String waitingKey, String modelLimit, String keyLimit, String ttl, String token, String waitMs, String waitingTtl) {
private long executePair(String modelKey, String keyKey, String waitingKey, String orderKey, String modelLimit, String keyLimit, String ttl, String token, String waitMs, String waitingTtl) {
try {
Long value = redis.execute(ACQUIRE_PAIR, List.of(modelKey, keyKey, waitingKey), modelLimit, keyLimit, ttl, token, waitMs, waitingTtl);
Long value = redis.execute(ACQUIRE_PAIR, List.of(modelKey, keyKey, waitingKey, orderKey), modelLimit, keyLimit, ttl, token, waitMs, waitingTtl);
if (value != null) return value;
} catch (DataAccessException exception) {
log.warn("Gateway pair admission Redis operation failed ({})", exception.getClass().getSimpleName());
@@ -450,7 +450,12 @@ public class UsageAuditService {
}
private String row(Object... values) { List<String> cells = new ArrayList<>(); for (Object value : values) cells.add(csv(value)); return String.join(",", cells); }
private String csv(Object value) { String text = value == null ? "" : String.valueOf(value); return "\"" + text.replace("\"", "\"\"").replace("\r", " ").replace("\n", " ") + "\""; }
static String csv(Object value) {
String text = value == null ? "" : String.valueOf(value).replace("\r", " ").replace("\n", " ");
// Prompts, outputs and key names are user-controlled; never let a spreadsheet evaluate them.
if (text.stripLeading().matches("^[=+@\\-].*") || text.startsWith("\t")) text = "'" + text;
return "\"" + text.replace("\"", "\"\"") + "\"";
}
private UUID parseUuid(String value) { try { return UUID.fromString(value); } catch (Exception exception) { throw new ApiException("INVALID_ID", "记录 ID 格式无效"); } }
private String blankToNull(String value) { return value == null || value.isBlank() ? null : value; }
private OffsetDateTime now() { return OffsetDateTime.now(ZoneOffset.UTC); }
@@ -1,19 +1,40 @@
-- KEYS: model slots, key slots, model waiting (score = wait expiry), model arrival order (score = first arrival).
-- ARGV: model limit, key limit, slot ttl, token, remaining wait ms, waiting ttl.
-- Returns 1 admitted, 0 model busy or behind earlier waiters, 2 own API Key busy.
local time = redis.call('TIME')
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', now)
redis.call('ZREMRANGEBYSCORE', KEYS[2], '-inf', now)
-- Expired waiters (crashed or timed-out callers) must also leave the arrival order.
local expired = redis.call('ZRANGEBYSCORE', KEYS[3], '-inf', now)
for _, member in ipairs(expired) do redis.call('ZREM', KEYS[4], member) end
redis.call('ZREMRANGEBYSCORE', KEYS[3], '-inf', now)
local busy = nil
if redis.call('ZCARD', KEYS[1]) >= tonumber(ARGV[1]) then busy = 0
elseif redis.call('ZCARD', KEYS[2]) >= tonumber(ARGV[2]) then busy = 2 end
if busy ~= nil then
if tonumber(ARGV[5]) > 0 then
redis.call('ZADD', KEYS[3], now + tonumber(ARGV[5]), ARGV[4])
local wait = tonumber(ARGV[5])
local function remember_waiting()
if wait > 0 then
redis.call('ZADD', KEYS[3], now + wait, ARGV[4])
redis.call('PEXPIRE', KEYS[3], ARGV[6])
end
return busy
end
-- A waiter blocked only by its own API Key limit does not hold a place ahead of other keys.
if redis.call('ZCARD', KEYS[2]) >= tonumber(ARGV[2]) then
redis.call('ZREM', KEYS[4], ARGV[4])
remember_waiting()
return 2
end
local ahead = redis.call('ZRANK', KEYS[4], ARGV[4])
if not ahead then ahead = redis.call('ZCARD', KEYS[4]) end
-- FIFO: a free model slot belongs to earlier model waiters before a newer request.
if redis.call('ZCARD', KEYS[1]) + ahead >= tonumber(ARGV[1]) then
if wait > 0 then
redis.call('ZADD', KEYS[4], 'NX', now, ARGV[4])
redis.call('PEXPIRE', KEYS[4], ARGV[6])
end
remember_waiting()
return 0
end
redis.call('ZREM', KEYS[3], ARGV[4])
redis.call('ZREM', KEYS[4], ARGV[4])
redis.call('ZADD', KEYS[1], now + tonumber(ARGV[3]), ARGV[4])
redis.call('ZADD', KEYS[2], now + tonumber(ARGV[3]), ARGV[4])
redis.call('PEXPIRE', KEYS[1], ARGV[3])
@@ -125,10 +125,16 @@ class PlatformAdminPostgresTest {
assertFalse(catalog.keyCanInvoke(key,secondId));
assertFalse(catalog.keyCanInvoke(key,modelId));
assertThrows(ApiException.class, () -> catalog.updateKeyModels(ownerToken,keyId,List.of(secondId),auth));
var edited = catalog.updateProvider(channelId,Map.of("name","Relay Updated","baseUrl","https://example.test/v2","credentialSecret",""),ownerToken,auth);
// The stored key must never follow a changed endpoint without being re-entered.
assertEquals("CREDENTIAL_REENTRY_REQUIRED", assertThrows(ApiException.class, () -> catalog.updateProvider(channelId,Map.of("baseUrl","https://attacker.test/v1","credentialSecret",""),ownerToken,auth)).getCode());
assertEquals("https://example.test/v1",jdbc.queryForObject("SELECT base_url FROM model_providers WHERE id=?",String.class,channelId));
var edited = catalog.updateProvider(channelId,Map.of("name","Relay Updated","baseUrl","https://example.test/v1/","credentialSecret",""),ownerToken,auth);
assertEquals("Relay Updated",edited.get("name"));
assertFalse(edited.containsKey("credentialSecret"));
assertEquals(cipher,jdbc.queryForObject("SELECT credential_ciphertext FROM model_providers WHERE id=?",String.class,channelId));
catalog.updateProvider(channelId,Map.of("baseUrl","https://example.test/v2","credentialSecret","replacement-test-secret"),ownerToken,auth);
assertNotEquals(cipher,jdbc.queryForObject("SELECT credential_ciphertext FROM model_providers WHERE id=?",String.class,channelId));
assertTrue(jdbc.queryForObject("SELECT string_agg(details::text,' ') FROM auth_audit_log WHERE event_type='provider.updated'",String.class).contains("previousBaseUrl"));
catalog.saveDefinition(modelId,Map.of("publicName","Renamed"),ownerToken,auth);
assertEquals("Renamed",catalog.activeModel(firstId).get("publicName"));
catalog.updateModel(firstId,Map.of("sourceLabel","Edited Group","upstreamModel","upstream-c","maxConcurrency",2),ownerToken,auth);
@@ -160,6 +166,10 @@ class PlatformAdminPostgresTest {
var cookie = new Cookie("moyi_session",memberToken);
var response = mvc.perform(get("/api/models").cookie(cookie)).andExpect(status().isOk()).andReturn();
var payload = json.readTree(response.getResponse().getContentAsString());
// Members see channel identity only, never endpoints or credential hints.
assertTrue(payload.get("providers").size() > 0);
assertTrue(payload.get("providers").findValues("baseUrl").isEmpty());
assertTrue(payload.get("providers").findValues("credentialMasked").isEmpty());
assertEquals(catalog.listDefinitions(false).size(),payload.get("logicalModels").size());
assertTrue(payload.get("logicalModels").size() > 250);
assertTrue(payload.get("logicalModels").findValuesAsText("id").contains(pending.get("id")));
@@ -0,0 +1,80 @@
package com.moyi.controlplane.gateway;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import static org.junit.jupiter.api.Assertions.*;
@EnabledIfEnvironmentVariable(named="MOYI_GATEWAY_TEST_REDIS_PORT", matches="[0-9]{2,5}")
class FairAdmissionRedisTest {
private LettuceConnectionFactory factory;
private StringRedisTemplate redis;
private final String prefix = "moyi:test:" + UUID.randomUUID() + ":";
private final Map<String,Object> model = Map.of("id", "fair-model", "maxConcurrency", 1);
@BeforeEach void setup() {
factory = new LettuceConnectionFactory("127.0.0.1", Integer.parseInt(System.getenv("MOYI_GATEWAY_TEST_REDIS_PORT")));
factory.afterPropertiesSet(); factory.start();
redis = new StringRedisTemplate(factory); redis.afterPropertiesSet();
}
@AfterEach void cleanup() {
if (redis != null) redis.delete(redis.keys(prefix + "*"));
if (factory != null) factory.destroy();
}
private GatewayGuard guard(long waitMs) { return new GatewayGuard(redis, 60, 300, 1, 4, waitMs, 10, prefix); }
@Test void newRequestCannotOvertakeAnEarlierWaiter() throws Exception {
var guard = guard(5000);
var holder = guard.acquire(model, UUID.randomUUID(), 5000);
var earlier = CompletableFuture.supplyAsync(() -> guard.acquire(model, UUID.randomUUID(), 5000));
// The earlier waiter is queued before the slot is released.
long until = System.nanoTime() + TimeUnit.SECONDS.toNanos(2);
while (zcard("order") == 0 && System.nanoTime() < until) Thread.sleep(5);
assertEquals(1L, zcard("order"));
holder.close();
// A newcomer that does not wait finds the free slot reserved for the earlier waiter.
var newcomer = assertThrows(GatewayLimitException.class, () -> guard(0).acquire(model, UUID.randomUUID(), 0));
assertEquals("MODEL_BUSY", newcomer.getCode());
try (var admitted = earlier.get(3, TimeUnit.SECONDS)) {
assertEquals(0L, zcard("order"));
assertEquals(0L, zcard("waiting"));
}
}
@Test void timedOutWaiterLeavesTheQueueAndDoesNotBlockLaterRequests() throws Exception {
var holder = guard(5000).acquire(model, UUID.randomUUID(), 5000);
assertThrows(GatewayLimitException.class, () -> guard(30).acquire(model, UUID.randomUUID(), 30));
assertEquals(0L, zcard("order"));
assertEquals(0L, zcard("waiting"));
holder.close();
try (var next = guard(0).acquire(model, UUID.randomUUID(), 0)) { assertNotNull(next); }
}
@Test void waiterBlockedByItsOwnKeyDoesNotHoldTheModelQueue() throws Exception {
UUID busyKey = UUID.randomUUID();
var keyGuard = new GatewayGuard(redis, 60, 300, 2, 1, 5000, 10, prefix);
var unlimitedModel = Map.<String,Object>of("id", "fair-model", "maxConcurrency", 2);
var keyHolder = keyGuard.acquire(unlimitedModel, busyKey, 5000);
var blockedByKey = CompletableFuture.supplyAsync(() -> keyGuard.acquire(unlimitedModel, busyKey, 200));
Thread.sleep(50);
// Another key may use the remaining model slot while the first key is at its own limit.
try (var other = keyGuard.acquire(unlimitedModel, UUID.randomUUID(), 0)) { assertNotNull(other); }
var failure = assertThrows(java.util.concurrent.ExecutionException.class, () -> blockedByKey.get(3, TimeUnit.SECONDS));
assertEquals("KEY_BUSY", ((GatewayLimitException) failure.getCause()).getCode());
keyHolder.close();
}
private long zcard(String kind) {
Long value = redis.opsForZSet().zCard(prefix + "gateway:" + kind + ":model:fair-model");
return value == null ? 0 : value;
}
}
@@ -0,0 +1,16 @@
package com.moyi.controlplane.usage;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class UsageCsvTest {
@Test void adminExportNeutralizesSpreadsheetFormulas() {
assertEquals("\"'=HYPERLINK(\"\"https://evil.test\"\")\"", UsageAuditService.csv("=HYPERLINK(\"https://evil.test\")"));
assertEquals("\"' @SUM(A1)\"", UsageAuditService.csv(" @SUM(A1)"));
assertEquals("\"'-1+2\"", UsageAuditService.csv("-1+2"));
assertEquals("\"'+cmd\"", UsageAuditService.csv("+cmd"));
assertEquals("\"plain text next\"", UsageAuditService.csv("plain text\r\nnext"));
assertEquals("\"\"", UsageAuditService.csv(null));
}
}