Skip to content

Commit a7f5a09

Browse files
committed
fix(secrets): publish recovered credentials to model owners
Refresh canonical model publication at secret activation and fence superseded publication and rollback. Carry authored SecretRef provenance into catalog workers so resolved key and header bytes remain opaque and writable catalog metadata remains safe. Clarify current entry policy after human edits. Retire real-worker test fixtures at their lifecycle boundary and replace timing-based overlap checks with the existing barrier. Focused regressions, type/lint guards, and isolated P1 review pass. The full local worker suite recorded 14 passes and four 120-second timeouts; unchanged clean-Linux and final live proof remain required before merge.
1 parent 270fa6c commit a7f5a09

27 files changed

Lines changed: 1284 additions & 246 deletions

‎config/assertion-safety-baseline.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1941,7 +1941,6 @@ src/agents/models-config-state.ts 1
19411941
src/agents/models-config.merge.ts 2
19421942
src/agents/models-config.plan.ts 4
19431943
src/agents/models-config.providers.policy.lookup.ts 1
1944-
src/agents/models-config.providers.source-managed.ts 4
19451944
src/agents/models-config.ts 1
19461945
src/agents/modes/interactive/theme/theme.ts 11
19471946
src/agents/node-plugin-tools.ts 1

‎docs/providers/ollama.md‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,18 @@ ollama list
186186
openclaw models list
187187
```
188188

189-
Setting `models.providers.ollama` with an explicit `models` array, or a
190-
custom provider with `api: "ollama"` and a non-loopback `baseUrl`, disables
191-
auto-discovery; models must then be defined manually (see
192-
[Configuration](#configuration)). A `models.providers.ollama` entry pointed at
193-
hosted `https://ollama.com` also skips discovery, since Ollama Cloud models
194-
are provider-managed. Loopback custom providers such as
195-
`http://127.0.0.2:11434` still count as local and keep auto-discovery.
189+
A **nonempty** `models.providers.ollama.models` list selects manual models and
190+
skips discovery. When Ollama is in the agent's model scope, an explicit
191+
self-hosted endpoint with `models: []` remains eligible for discovery;
192+
`models.providers.ollama.apiKey` alone does not select that provider for Gateway
193+
model browsing.
194+
195+
Hosted `https://ollama.com` entries skip discovery because Ollama Cloud models
196+
are provider-managed. Without an explicit Ollama endpoint, a custom provider
197+
with `api: "ollama"` and a non-loopback `baseUrl` suppresses ambient localhost
198+
discovery; list that custom provider's models manually (see
199+
[Configuration](#configuration)). Loopback custom providers such as
200+
`http://127.0.0.2:11434` keep ambient local discovery eligible.
196201

197202
You can use a full ref such as `ollama/<pulled-model>:latest` without a
198203
hand-written `models.json` entry; OpenClaw resolves it live. For signed-in
@@ -514,7 +519,7 @@ capability.
514519
</Tab>
515520

516521
<Tab title="Custom base URL">
517-
Explicit config disables auto-discovery, so models must be listed:
522+
This example uses a nonempty manual model list, so it skips discovery:
518523

519524
```json5
520525
{
@@ -564,7 +569,8 @@ Replace model IDs with exact names from `ollama list` or
564569
openclaw models set ollama/gemma4
565570
```
566571

567-
Do not add a `models.providers.ollama` block unless you need manual models.
572+
Leave `models.providers.ollama` unset to use the default local endpoint, or
573+
configure a self-hosted endpoint with `models: []` to keep discovery eligible.
568574

569575
</Accordion>
570576

@@ -1181,8 +1187,10 @@ For full setup and behavior, see [Ollama Web Search](/tools/ollama-search).
11811187
</Accordion>
11821188

11831189
<Accordion title="Ollama not detected">
1184-
Confirm Ollama is running, `OLLAMA_API_KEY` (or an auth profile) is set,
1185-
and `models.providers.ollama` is **not** defined explicitly:
1190+
Confirm Ollama is running and is in the agent's model scope. For ambient
1191+
localhost discovery, set `OLLAMA_API_KEY` (or an auth profile). A nonempty
1192+
manual model list skips discovery; an explicit self-hosted endpoint with
1193+
`models: []` does not:
11861194

11871195
```bash
11881196
ollama serve

‎docs/tools/secrets.md‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,17 @@ again. A later runtime refresh failure does not undo that write: resolve the
6868
reported provider error and run `openclaw secrets reload`, rather than resubmitting.
6969

7070
The same tool result reports `status: "stored"`, the SecretRef, and `currentPolicy`
71-
from one follow-up metadata read. This is current store policy, not an immutable
72-
approval receipt: another write may have changed it. `available` includes the
71+
from one follow-up metadata read. This is the entry's current host list, which you
72+
can edit, not Gateway configuration or an immutable approval receipt: another
73+
write may have changed it. `available` includes the
7374
complete allowed-host list; an empty list means no egress. If the serialized list
7475
exceeds 512 characters, `omitted` reports only `allowedHostCount`. `missing`,
7576
`kind_changed`, and `unavailable` mean the entry disappeared, became an `env`
7677
entry, or has no complete validated host policy. None undo the saved result or invite
77-
resubmission. The agent must use the observed policy instead of its proposal and
78-
make no host claims when the complete list is unavailable.
78+
resubmission. The agent must report current hosts instead of its proposal and
79+
make no host claims when the complete list is unavailable. A difference does not
80+
explain who changed the hosts or why, and is not a reason to prescribe Gateway
81+
configuration changes.
7982

8083
<Warning>
8184
Allowed hosts govern Gateway egress substitution, not config SecretRefs. Leave
@@ -122,7 +125,9 @@ A stored entry is a regular shared-store entry (see
122125
keys, channel tokens). With the default store alias, it is
123126
`{ "source": "store", "provider": "default", "id": "STRIPE_API_KEY" }`;
124127
`secrets.defaults.store` selects a different provider alias. Writes trigger
125-
refresh of affected config and auth-profile references.
128+
refresh of affected config and auth-profile references. A successful refresh
129+
also replaces prepared model state, so later catalog reads use the new
130+
credential for providers already in the agent's model scope.
126131
- `env` entries are readable values; the operator sets them, not the credential
127132
request flow.
128133
- `secret` entries reach Gateway-host subprocess traffic through automatically

‎src/agents/model-auth-provider-config.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,13 +680,25 @@ export function sentinelizeConfigSecretRefEnvApiKey(params: {
680680
: params.apiKey;
681681
}
682682

683-
export function resolveLiteralProviderConfigApiKeyAuth(params: {
684-
cfg: OpenClawConfig | undefined;
683+
export function resolveRuntimeProviderConfigApiKeyAuth(params: {
684+
cfg: OpenClawConfig;
685+
sourceConfig: OpenClawConfig | undefined;
685686
provider: string;
686687
}): ResolvedProviderAuth | undefined {
687688
const { providerConfig, ref } = resolveProviderConfigSecretInput(params.cfg, params.provider);
688-
const apiKey = ref ? undefined : normalizeOptionalSecretInput(providerConfig?.apiKey);
689-
if (!apiKey || isNonSecretApiKeyMarker(apiKey)) {
689+
const sourceRef = resolveProviderConfigSecretInput(params.sourceConfig, params.provider).ref;
690+
// Prepared Ref values are opaque bytes, not authored markers or copy/paste input.
691+
// Legacy metadata markers without a source Ref still use literal validation.
692+
const apiKey = sourceRef
693+
? providerConfig?.apiKey
694+
: ref
695+
? undefined
696+
: normalizeOptionalSecretInput(providerConfig?.apiKey);
697+
if (
698+
typeof apiKey !== "string" ||
699+
!apiKey.trim() ||
700+
(!sourceRef && isNonSecretApiKeyMarker(apiKey))
701+
) {
690702
return undefined;
691703
}
692704
return {

‎src/agents/model-auth-runtime-config.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ export function resolveManagedSecretRefRuntimeProviderAuth(params: {
4646
if (!runtimeConfig || !usesRuntimeProvider) {
4747
return undefined;
4848
}
49-
const resolved = authConfig.resolveLiteralProviderConfigApiKeyAuth({
49+
const resolved = authConfig.resolveRuntimeProviderConfigApiKeyAuth({
5050
cfg: runtimeConfig,
51+
sourceConfig,
5152
provider: params.provider,
5253
});
5354
if (!resolved?.apiKey) {

‎src/agents/model-auth.test.ts‎

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ describe("resolveApiKeyForProviderCore", () => {
12541254
});
12551255
});
12561256

1257-
it.each([
1257+
it.each<{ name: string; apiKey: ModelProviderConfig["apiKey"]; runtimeKey?: string }>([
12581258
{
12591259
name: "generated marker",
12601260
apiKey: NON_ENV_SECRETREF_MARKER,
@@ -1267,58 +1267,74 @@ describe("resolveApiKeyForProviderCore", () => {
12671267
name: "file SecretRef",
12681268
apiKey: { source: "file", provider: "vault", id: "/cliproxy/api-key" } as const,
12691269
},
1270-
])("resolves custom provider $name auth from the active runtime snapshot", async ({ apiKey }) => {
1271-
const sourceConfig = {
1272-
models: {
1273-
providers: {
1274-
cliproxyapi: {
1275-
api: "openai-responses" as const,
1276-
apiKey,
1277-
baseUrl: "https://cliproxy.example/v1",
1278-
models: [],
1270+
...(
1271+
[
1272+
["opaque synthetic marker", CUSTOM_LOCAL_AUTH_MARKER],
1273+
["opaque managed marker", NON_ENV_SECRETREF_MARKER],
1274+
["opaque env marker", "OLLAMA_API_KEY"],
1275+
["opaque env template", "${OPAQUE_KEY}"],
1276+
["opaque whitespace", " synthetic-byte-exact-key "],
1277+
] as const
1278+
).map(([name, runtimeKey]) => ({
1279+
name,
1280+
runtimeKey,
1281+
apiKey: { source: "store", provider: "default", id: "OPAQUE_KEY" } as const,
1282+
})),
1283+
])(
1284+
"resolves custom provider $name auth from the active runtime snapshot",
1285+
async ({ apiKey, runtimeKey = "sk-runtime-cliproxy" }) => {
1286+
const sourceConfig = {
1287+
models: {
1288+
providers: {
1289+
cliproxyapi: {
1290+
api: "openai-responses" as const,
1291+
apiKey,
1292+
baseUrl: "https://cliproxy.example/v1",
1293+
models: [],
1294+
},
12791295
},
12801296
},
1281-
},
1282-
};
1283-
const runtimeConfig = {
1284-
models: {
1285-
providers: {
1286-
cliproxyapi: {
1287-
...sourceConfig.models.providers.cliproxyapi,
1288-
apiKey: "sk-runtime-cliproxy", // pragma: allowlist secret
1297+
};
1298+
const runtimeConfig = {
1299+
models: {
1300+
providers: {
1301+
cliproxyapi: {
1302+
...sourceConfig.models.providers.cliproxyapi,
1303+
apiKey: runtimeKey,
1304+
},
12891305
},
12901306
},
1291-
},
1292-
};
1293-
setRuntimeConfigSnapshot(runtimeConfig, sourceConfig);
1294-
1295-
const resolved = await resolveApiKeyForProviderCore({
1296-
provider: "cliproxyapi",
1297-
cfg: sourceConfig,
1298-
secretSentinels: true,
1299-
store: { version: 1, profiles: {} },
1300-
});
1307+
};
1308+
setRuntimeConfigSnapshot(runtimeConfig, sourceConfig);
13011309

1302-
expectSecretSentinelAuth(resolved, {
1303-
value: "sk-runtime-cliproxy",
1304-
source: "models.providers.cliproxyapi",
1305-
mode: "api-key",
1306-
});
1307-
await expect(
1308-
hasAvailableAuthForProvider({
1310+
const resolved = await resolveApiKeyForProviderCore({
13091311
provider: "cliproxyapi",
13101312
cfg: sourceConfig,
1313+
secretSentinels: true,
13111314
store: { version: 1, profiles: {} },
1312-
}),
1313-
).resolves.toBe(true);
1314-
expect(
1315-
hasRuntimeAvailableProviderAuth({
1316-
provider: "cliproxyapi",
1317-
cfg: sourceConfig,
1318-
allowPluginSyntheticAuth: false,
1319-
}),
1320-
).toBe(true);
1321-
});
1315+
});
1316+
1317+
expectSecretSentinelAuth(resolved, {
1318+
value: runtimeKey,
1319+
source: "models.providers.cliproxyapi",
1320+
mode: "api-key",
1321+
});
1322+
await expect(
1323+
hasAvailableAuthForProvider({
1324+
provider: "cliproxyapi",
1325+
cfg: sourceConfig,
1326+
store: { version: 1, profiles: {} },
1327+
}),
1328+
).resolves.toBe(true);
1329+
expect(
1330+
hasRuntimeAvailableProviderAuth({
1331+
provider: "cliproxyapi",
1332+
cfg: sourceConfig,
1333+
allowPluginSyntheticAuth: false,
1334+
}),
1335+
).toBe(true);
1336+
},
1337+
);
13221338

13231339
it("preserves SecretRef provenance for resolved runtime config clones", async () => {
13241340
const sourceConfig = {

‎src/agents/models-config.plan.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,7 @@ async function planOpenClawModelsJsonWithDeps(
308308
agentDir,
309309
env,
310310
secretDefaults: cfg.secrets?.defaults,
311-
sourceProviders: context.sourceConfigForSecrets.models?.providers,
312-
sourceSecretDefaults: context.sourceConfigForSecrets.secrets?.defaults,
311+
sourceConfigForSecrets: context.sourceConfigForSecrets,
313312
secretRefManagedProviders,
314313
manifestPlugins,
315314
...(providerPolicyManifestRegistry
@@ -329,8 +328,7 @@ async function planOpenClawModelsJsonWithDeps(
329328
const secretEnforcedProviders =
330329
enforceSourceManagedProviderSecrets({
331330
providers: normalizedMergedProviders,
332-
sourceProviders: context.sourceConfigForSecrets.models?.providers,
333-
sourceSecretDefaults: context.sourceConfigForSecrets.secrets?.defaults,
331+
sourceConfigForSecrets: context.sourceConfigForSecrets,
334332
secretRefManagedProviders,
335333
}) ?? normalizedMergedProviders;
336334
const finalProviders = filterWritableProviders(secretEnforcedProviders);

‎src/agents/models-config.providers.normalize-keys.test.ts‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import os from "node:os";
44
import path from "node:path";
55
import { describe, expect, it, vi } from "vitest";
66
import type { OpenClawConfig } from "../config/config.js";
7+
import { createConfigIoContext } from "../config/io.context.js";
8+
import { readConfigFileSnapshotFromContext } from "../config/io.snapshot.js";
79
import { ModelsConfigSchema } from "../config/zod-schema.core.js";
10+
import { withOpenClawTestState } from "../test-utils/openclaw-test-state.js";
811
import { NON_ENV_SECRETREF_MARKER } from "./model-auth-markers.js";
912
import {
1013
normalizeProviderCatalogModelsForConfig,
@@ -364,6 +367,76 @@ describe("normalizeProviders", () => {
364367
}
365368
});
366369

370+
it.each([
371+
{
372+
label: "substituted literal",
373+
authored: "${HEADER_SOURCE}",
374+
env: { HEADER_SOURCE: "${HEADER_LITERAL}" },
375+
expected: "${HEADER_LITERAL}",
376+
},
377+
{
378+
label: "escaped literal",
379+
authored: "$${HEADER_LITERAL}",
380+
env: {},
381+
expected: "${HEADER_LITERAL}",
382+
},
383+
{
384+
label: "pending reference",
385+
authored: "$HEADER_PENDING",
386+
env: {},
387+
expected: "secretref-env:HEADER_PENDING",
388+
},
389+
])(
390+
"preserves loader $label headers through normalization and source enforcement",
391+
async ({ authored, env, expected }) => {
392+
await withOpenClawTestState(
393+
{
394+
label: "catalog-header-facts",
395+
env: { HEADER_LITERAL: undefined, HEADER_PENDING: undefined, ...env },
396+
},
397+
async (state) => {
398+
await state.writeConfig({
399+
plugins: { enabled: false },
400+
models: {
401+
providers: {
402+
custom: {
403+
baseUrl: "https://provider.example/v1",
404+
api: "openai-completions",
405+
apiKey: "plain-fixture-key",
406+
models: [],
407+
headers: { "X.Trace": authored },
408+
},
409+
},
410+
},
411+
});
412+
const snapshot = await readConfigFileSnapshotFromContext(
413+
createConfigIoContext({
414+
configPath: state.configPath,
415+
env: state.env,
416+
homedir: () => state.home,
417+
observe: false,
418+
}),
419+
);
420+
expect(snapshot.valid).toBe(true);
421+
const params = {
422+
providers: snapshot.config.models?.providers,
423+
sourceConfigForSecrets: snapshot.sourceConfig,
424+
};
425+
const normalized = normalizeProviders({
426+
...params,
427+
agentDir: state.agentDir(),
428+
env: state.env,
429+
});
430+
const enforced = enforceSourceManagedProviderSecrets(params);
431+
expect({
432+
normalized: normalized?.custom?.headers?.["X.Trace"],
433+
enforced: enforced?.custom?.headers?.["X.Trace"],
434+
}).toEqual({ normalized: expected, enforced: expected });
435+
},
436+
);
437+
},
438+
);
439+
367440
it("ignores non-object provider entries during source-managed enforcement", () => {
368441
const providers = {
369442
openai: null,
@@ -392,7 +465,7 @@ describe("normalizeProviders", () => {
392465

393466
const enforced = enforceSourceManagedProviderSecrets({
394467
providers,
395-
sourceProviders,
468+
sourceConfigForSecrets: { models: { providers: sourceProviders } },
396469
});
397470
expect((enforced as Record<string, unknown>).openai).toBeNull();
398471
expect(enforced?.moonshot?.apiKey).toBe("MOONSHOT_API_KEY"); // pragma: allowlist secret

0 commit comments

Comments
 (0)