@@ -11,15 +11,16 @@ import type { OpenClawConfig } from "../config/types.openclaw.js";
1111import {
1212 coerceSecretRef ,
1313 DEFAULT_SECRET_PROVIDER_ALIAS ,
14+ parseEnvTemplateSecretRef ,
1415 type SecretInput ,
1516 type SecretRef ,
1617} from "../config/types.secrets.js" ;
1718import type { OAuthCredentials } from "../llm/oauth.js" ;
1819import { getProviderEnvVars } from "../secrets/provider-env-vars.js" ;
20+ import { isValidSecretRef } from "../secrets/ref-contract.js" ;
1921import { normalizeSecretInput } from "../utils/normalize-secret-input.js" ;
2022import type { SecretInputMode } from "./provider-auth-types.js" ;
2123
22- const ENV_REF_PATTERN = / ^ \$ \{ ( [ A - Z ] [ A - Z 0 - 9 _ ] * ) \} $ / ;
2324type UpsertAuthProfileParams = Parameters < typeof upsertAuthProfileWithLock > [ 0 ] ;
2425
2526const resolveAuthAgentDir = ( agentDir ?: string , config ?: OpenClawConfig ) =>
@@ -40,14 +41,6 @@ function buildEnvSecretRef(id: string): SecretRef {
4041 return { source : "env" , provider : DEFAULT_SECRET_PROVIDER_ALIAS , id } ;
4142}
4243
43- function parseEnvSecretRef ( value : string ) : SecretRef | null {
44- const match = ENV_REF_PATTERN . exec ( value ) ;
45- if ( ! match ) {
46- return null ;
47- }
48- return buildEnvSecretRef ( match [ 1 ] ) ;
49- }
50-
5144function resolveProviderDefaultEnvSecretRef ( provider : string , config ?: OpenClawConfig ) : SecretRef {
5245 const envVars = getProviderEnvVars ( provider , {
5346 ...( config ? { config } : { } ) ,
@@ -67,15 +60,25 @@ function resolveApiKeySecretInput(
6760 input : SecretInput ,
6861 options ?: ApiKeyStorageOptions ,
6962) : SecretInput {
63+ if ( input !== null && typeof input === "object" ) {
64+ const coercedRef = coerceSecretRef ( input ) ;
65+ if ( ! coercedRef || ! isValidSecretRef ( coercedRef ) ) {
66+ throw new Error ( "API key SecretRef is invalid." ) ;
67+ }
68+ return coercedRef ;
69+ }
7070 if ( options ?. secretInputMode === "plaintext" ) {
7171 return normalizeSecretInput ( input ) ;
7272 }
7373 const coercedRef = coerceSecretRef ( input ) ;
7474 if ( coercedRef ) {
75+ if ( ! isValidSecretRef ( coercedRef ) ) {
76+ throw new Error ( "API key SecretRef is invalid." ) ;
77+ }
7578 return coercedRef ;
7679 }
7780 const normalized = normalizeSecretInput ( input ) ;
78- const inlineEnvRef = parseEnvSecretRef ( normalized ) ;
81+ const inlineEnvRef = parseEnvTemplateSecretRef ( normalized , DEFAULT_SECRET_PROVIDER_ALIAS ) ;
7982 if ( inlineEnvRef ) {
8083 return inlineEnvRef ;
8184 }
0 commit comments