fix(llm-deepseek-api-key): release the route key only for the route's origin

The credential published through ctx.llm.routeCredential() ignored the
destination: resolve() and configured() answered for any URL, unlike the
account route, whose owner checks the destination. Both now answer only
for destinations under the origin of the route's base URL without
embedded credentials, and read the credential store for no other
destination. Messages, Files, and listing requests derive their URLs
from that base URL and are unchanged.
This commit is contained in:
LegGasai committed 2026-11-13 11:36:30 +08:00
1 parent 98003bd816
commit bca49d2ee0
8 files changed
+50 -11

No files matched your search

@@ -9,8 +9,8 @@
en: 9665e1879893fe76
zh: 9da30ad0be6fc38d
/agent-note-route-credentials-deepseek-model-routes-publish-the-credential-consumers-reuse/decision:
en: 8d28911b4a7aaca0
zh: db511eb9dfdc5f5e
en: 5c11dfca48a6343b
zh: e637091ff8593f5d
/agent-note-route-credentials-deepseek-model-routes-publish-the-credential-consumers-reuse/alternatives-considered:
en: 8177946a6bfa1a6d
zh: f523f9057b4b47c5
@@ -14,7 +14,7 @@ Several packages shared one DeepSeek credential without an owner. `@deepseek-ai/
`@deepseek-ai/dsh-llm-deepseek` reads no credential. `DeepSeekAdapterOptions.resolveAuth(connection, endpoint)` returns the grant or `undefined`; `missingCredential(connection, provider)` names the failure of a request without one, `MISSING_CREDENTIAL` by default; `credential` declares the published kind, reference, and presence check. The listing cache scopes by endpoint and a hash of the credential headers.
The route plugins own credentials. `llm-deepseek-api-key` reads its key through `resolveCredentialRef` and `credentialRefConfigured`, new in `@deepseek-ai/dsh-credentials`, which treat a mounted credentials service as authoritative and read the launch environment only without one; it publishes kind `api-key` with the `apiKeyEnv` reference. `llm-deepseek-account` reads `ctx.deepseekAccount.resolveRequestCredential(endpoint)`, maps HTTP 401 to `ACCOUNT_TOKEN_INVALID` and quota failures to `ACCOUNT_QUOTA`, fails without a credential as `ACCOUNT_SIGN_IN_REQUIRED`, publishes kind `account`, and owns the sign-out policy: it cancels its running Agents on `deepseek-account/signed-out` and registers a running-task report with the account service.
The route plugins own credentials. `llm-deepseek-api-key` reads its key through `resolveCredentialRef` and `credentialRefConfigured`, new in `@deepseek-ai/dsh-credentials`, which treat a mounted credentials service as authoritative and read the launch environment only without one; it publishes kind `api-key` with the `apiKeyEnv` reference and releases the key only for destinations under the origin of its base URL. `llm-deepseek-account` reads `ctx.deepseekAccount.resolveRequestCredential(endpoint)`, maps HTTP 401 to `ACCOUNT_TOKEN_INVALID` and quota failures to `ACCOUNT_QUOTA`, fails without a credential as `ACCOUNT_SIGN_IN_REQUIRED`, publishes kind `account`, and owns the sign-out policy: it cancels its running Agents on `deepseek-account/signed-out` and registers a running-task report with the account service.
`@deepseek-ai/dsh-deepseek-account` releases request credentials. `resolveRequestCredential(url)` returns the `x-dsh-auth-token` header, the bare token, and a `reject()` that removes the grant while it still matches and contains storage failures; `registerRunningTaskReport(report)` feeds `hasRunningAccountTasks()`. The service inspects no Agents and depends on neither `dsh-agent` nor `dsh-llm`. The package lives in the `packages/account/` group under its unchanged name.
@@ -14,7 +14,7 @@ Status: implemented
`@deepseek-ai/dsh-llm-deepseek` 不读取任何凭据。`DeepSeekAdapterOptions.resolveAuth(connection, endpoint)` 返回授权或 `undefined`;`missingCredential(connection, provider)` 命名没有凭据时请求的失败,默认为 `MISSING_CREDENTIAL`;`credential` 声明发布的种类、引用和存在性检查。列表缓存以端点和凭据请求头的哈希划分作用域。
路由插件拥有凭据。`llm-deepseek-api-key` 通过 `@deepseek-ai/dsh-credentials` 新增的 `resolveCredentialRef` 和 `credentialRefConfigured` 读取密钥,二者把已挂载的凭据服务视为权威,仅在没有该服务时读取启动环境;它发布种类 `api-key` 并附带 `apiKeyEnv` 引用。`llm-deepseek-account` 读取 `ctx.deepseekAccount.resolveRequestCredential(endpoint)`,把 HTTP 401 映射为 `ACCOUNT_TOKEN_INVALID`、配额失败映射为 `ACCOUNT_QUOTA`,没有凭据时以 `ACCOUNT_SIGN_IN_REQUIRED` 失败,发布种类 `account`,并拥有退登策略:在 `deepseek-account/signed-out` 时取消其运行中的 Agent,并向账号服务注册运行中任务报告。
路由插件拥有凭据。`llm-deepseek-api-key` 通过 `@deepseek-ai/dsh-credentials` 新增的 `resolveCredentialRef` 和 `credentialRefConfigured` 读取密钥,二者把已挂载的凭据服务视为权威,仅在没有该服务时读取启动环境;它发布种类 `api-key` 并附带 `apiKeyEnv` 引用,且只对其基础地址所在 origin 下的目的地释放密钥。`llm-deepseek-account` 读取 `ctx.deepseekAccount.resolveRequestCredential(endpoint)`,把 HTTP 401 映射为 `ACCOUNT_TOKEN_INVALID`、配额失败映射为 `ACCOUNT_QUOTA`,没有凭据时以 `ACCOUNT_SIGN_IN_REQUIRED` 失败,发布种类 `account`,并拥有退登策略:在 `deepseek-account/signed-out` 时取消其运行中的 Agent,并向账号服务注册运行中任务报告。
`@deepseek-ai/dsh-deepseek-account` 释放请求凭据。`resolveRequestCredential(url)` 返回 `x-dsh-auth-token` 请求头、裸 token 和 `reject()`;`reject()` 仅在授权仍一致时删除它并隔离存储失败。`registerRunningTaskReport(report)` 为 `hasRunningAccountTasks()` 提供答案。该服务不检查任何 Agent,也不依赖 `dsh-agent` 或 `dsh-llm`。包以不变的名字位于 `packages/account/` 组。
@@ -9,8 +9,8 @@
en: b6871195e386cc64
zh: 742b05a3ca80883c
/deepseek-ai-dsh-llm-deepseek-api-key/summary:
en: 69c60bbfb6aafb7a
zh: 1afe49ea9cdc3cd5
en: ea0a35b73d3b8f2c
zh: 7e404ed4617c2789
/deepseek-ai-dsh-llm-deepseek-api-key/table-of-contents:
en: 23386452a66b2fbd
zh: 4cf93c4bf17b3da2
+1 -1
View File
@@ -11,7 +11,7 @@ English | [中文](README.zh.md)
Register authentication, model discovery, and the official model listing for `deepseek-official`. This plugin shares the [Messages transport](../llm-deepseek/README.md) and owns credential resolution, catalog availability, and model admission.
Authentication resolution returns the validated API key in `x-api-key` for Messages, Files, and listing requests, and publishes the same key through `ctx.llm.routeCredential('deepseek-official')` with the route's base URL and `apiKeyEnv` reference, so the DeepSeek web providers send it as a bearer token to the same deployment.
Authentication resolution returns the validated API key in `x-api-key` for Messages, Files, and listing requests, and publishes the same key through `ctx.llm.routeCredential('deepseek-official')` with the route's base URL and `apiKeyEnv` reference, so the DeepSeek web providers send it as a bearer token to the same deployment. The published credential releases the key only for destinations under the origin of that base URL: for another origin, or a URL that embeds credentials, `configured()` answers false and `resolve()` releases nothing, and neither reads the credential store.
## Table of Contents
@@ -11,7 +11,7 @@ kind: "package-reference"
为 `deepseek-official` 注册鉴权、模型发现和官方模型列表。通过 [llm-deepseek](../llm-deepseek/README.zh.md) 共用 Messages 请求实现;本包拥有凭据解析、目录可用性判断和模型准入。
鉴权解析将验证后的 API key 放入 `x-api-key`,供 Messages、Files 和列表请求使用,并通过 `ctx.llm.routeCredential('deepseek-official')` 连同路由的基础地址和 `apiKeyEnv` 引用发布同一密钥,使 DeepSeek Web 提供方以 Bearer 令牌把它发往同一部署。
鉴权解析将验证后的 API key 放入 `x-api-key`,供 Messages、Files 和列表请求使用,并通过 `ctx.llm.routeCredential('deepseek-official')` 连同路由的基础地址和 `apiKeyEnv` 引用发布同一密钥,使 DeepSeek Web 提供方以 Bearer 令牌把它发往同一部署。发布的凭据只对该基础地址所在 origin 下的目的地释放密钥:对其他 origin 或内嵌凭据的 URL,`configured()` 回答 false,`resolve()` 不释放任何内容,二者都不读取凭据存储。
## 目录
+14 -3
View File
@@ -41,7 +41,17 @@ export function apply(ctx: Context, config: Config): void {
const value = await resolveCredentialRef(ctx, connection.apiKeyEnv)
return value === undefined ? undefined : assertUsableApiKey(value, settingsNs, connection.apiKeyEnv)
}
const resolveAuth = async (connection: ResolvedDeepSeekOptions): Promise<DeepSeekRequestAuth | undefined> => {
/**
* Whether `endpoint` belongs to the deployment this route sends its key to: the origin of the route's base URL,
* without credentials embedded in the URL.
*/
const servedByRoute = (connection: ResolvedDeepSeekOptions, endpoint: string): boolean => {
const destination = new URL(endpoint)
return destination.origin === new URL(connection.baseURL).origin && destination.username === '' && destination.password === ''
}
const resolveAuth = async (connection: ResolvedDeepSeekOptions, endpoint: string): Promise<DeepSeekRequestAuth | undefined> => {
// The store is read only for a destination this route serves, as the presence check below.
if (!servedByRoute(connection, endpoint)) return undefined
const key = await resolveApiKey(connection)
return key === undefined ? undefined : { headers: { 'x-api-key': key }, value: key }
}
@@ -52,7 +62,7 @@ export function apply(ctx: Context, config: Config): void {
)
const listings = createModelListingCache(ctx, name, PROVIDER, {
settings: () => options().listing,
credential: async () => (await resolveAuth(options()))?.headers,
credential: async url => (await resolveAuth(options(), url))?.headers,
})
let reportedListing: ModelListing | undefined
/** Resolve the catalog, reporting each listing's hidden entries and dropped badges once. */
@@ -98,7 +108,8 @@ export function apply(ctx: Context, config: Config): void {
credential: {
kind: 'api-key',
reference: connection => connection.apiKeyEnv,
configured: connection => credentialRefConfigured(ctx, connection.apiKeyEnv),
configured: async (connection, endpoint) =>
servedByRoute(connection, endpoint) && await credentialRefConfigured(ctx, connection.apiKeyEnv),
},
admit: (connection, model) => { refuseListedModel(model, connection.refusals.get(model)) },
discoverModels: async (provider) => {
@@ -94,3 +94,31 @@ it('publishes the route API key for its consumers from the credential planes it
vi.unstubAllEnvs()
}
})
it.each([
'https://other-origin.example/v1/tools/search',
'http://route.deepseek.test/v1/tools/search',
'https://route.deepseek.test:8443/v1/tools/search',
'https://[email protected]/v1/tools/search',
'https://:[email protected]/v1/tools/search',
])('releases no key for %s, a destination outside the origin of its base URL, without reading the credential store', async (endpoint) => {
const resolve = vi.fn(async () => ({ value: 'stored-key' }))
const describe = vi.fn(async () => ({ configured: true }))
const ctx = new Context()
try {
ctx.provide('credentials', { resolve, describe } as never)
await ctx.plugin(LlmRuntime)
await ctx.plugin(ApiKey, { baseURL: 'https://route.deepseek.test/anthropic' })
const credential = ctx.llm.routeCredential('deepseek-official')
await expect(credential!.configured(endpoint)).resolves.toBe(false)
await expect(credential!.resolve(endpoint)).resolves.toBeUndefined()
expect(resolve).not.toHaveBeenCalled()
expect(describe).not.toHaveBeenCalled()
// The same store answers for a destination under the route's origin.
const served = 'https://route.deepseek.test/v1/tools/search'
await expect(credential!.configured(served)).resolves.toBe(true)
await expect(credential!.resolve(served)).resolves.toEqual({ headers: { 'x-api-key': 'stored-key' }, value: 'stored-key' })
} finally {
await ctx.fiber.dispose()
}
})