|
1 | 1 | import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime"; |
2 | | -import { |
3 | | - normalizeResolvedSecretInputString, |
4 | | - normalizeSecretInput, |
5 | | -} from "openclaw/plugin-sdk/secret-input"; |
| 2 | +import { resolveDefaultSecretProviderAlias } from "openclaw/plugin-sdk/provider-auth"; |
| 3 | +import { resolveSecretInputString, normalizeSecretInput } from "openclaw/plugin-sdk/secret-input"; |
6 | 4 |
|
7 | 5 | export const DEFAULT_FIRECRAWL_BASE_URL = "https://api.firecrawl.dev"; |
8 | 6 | export const DEFAULT_FIRECRAWL_SEARCH_TIMEOUT_SECONDS = 30; |
9 | 7 | export const DEFAULT_FIRECRAWL_SCRAPE_TIMEOUT_SECONDS = 60; |
10 | 8 | export const DEFAULT_FIRECRAWL_MAX_AGE_MS = 172_800_000; |
| 9 | +const FIRECRAWL_API_KEY_ENV_VAR = "FIRECRAWL_API_KEY"; |
11 | 10 |
|
12 | 11 | type WebSearchConfig = NonNullable<OpenClawConfig["tools"]>["web"] extends infer Web |
13 | 12 | ? Web extends { search?: infer Search } |
@@ -104,33 +103,101 @@ export function resolveFirecrawlFetchConfig(cfg?: OpenClawConfig): FirecrawlFetc |
104 | 103 | return firecrawl as FirecrawlFetchConfig; |
105 | 104 | } |
106 | 105 |
|
107 | | -function normalizeConfiguredSecret(value: unknown, path: string): string | undefined { |
108 | | - return normalizeSecretInput( |
109 | | - normalizeResolvedSecretInputString({ |
110 | | - value, |
111 | | - path, |
112 | | - }), |
113 | | - ); |
| 106 | +type ConfiguredSecretResolution = |
| 107 | + | { status: "available"; value: string } |
| 108 | + | { status: "missing" } |
| 109 | + | { status: "blocked" }; |
| 110 | + |
| 111 | +function canResolveEnvSecretRefInReadOnlyPath(params: { |
| 112 | + cfg?: OpenClawConfig; |
| 113 | + provider: string; |
| 114 | + id: string; |
| 115 | +}): boolean { |
| 116 | + const providerConfig = params.cfg?.secrets?.providers?.[params.provider]; |
| 117 | + if (!providerConfig) { |
| 118 | + return params.provider === resolveDefaultSecretProviderAlias(params.cfg ?? {}, "env"); |
| 119 | + } |
| 120 | + if (providerConfig.source !== "env") { |
| 121 | + return false; |
| 122 | + } |
| 123 | + const allowlist = providerConfig.allowlist; |
| 124 | + return !allowlist || allowlist.includes(params.id); |
| 125 | +} |
| 126 | + |
| 127 | +function resolveConfiguredSecret( |
| 128 | + value: unknown, |
| 129 | + path: string, |
| 130 | + cfg?: OpenClawConfig, |
| 131 | +): ConfiguredSecretResolution { |
| 132 | + const resolved = resolveSecretInputString({ |
| 133 | + value, |
| 134 | + path, |
| 135 | + defaults: cfg?.secrets?.defaults, |
| 136 | + mode: "inspect", |
| 137 | + }); |
| 138 | + if (resolved.status === "available") { |
| 139 | + const normalized = normalizeSecretInput(resolved.value); |
| 140 | + return normalized ? { status: "available", value: normalized } : { status: "missing" }; |
| 141 | + } |
| 142 | + if (resolved.status === "missing") { |
| 143 | + return { status: "missing" }; |
| 144 | + } |
| 145 | + if (resolved.ref.source !== "env") { |
| 146 | + return { status: "blocked" }; |
| 147 | + } |
| 148 | + const envVarName = resolved.ref.id.trim(); |
| 149 | + if (envVarName !== FIRECRAWL_API_KEY_ENV_VAR) { |
| 150 | + return { status: "blocked" }; |
| 151 | + } |
| 152 | + if ( |
| 153 | + !canResolveEnvSecretRefInReadOnlyPath({ |
| 154 | + cfg, |
| 155 | + provider: resolved.ref.provider, |
| 156 | + id: envVarName, |
| 157 | + }) |
| 158 | + ) { |
| 159 | + return { status: "blocked" }; |
| 160 | + } |
| 161 | + const envValue = normalizeSecretInput(process.env[envVarName]); |
| 162 | + return envValue ? { status: "available", value: envValue } : { status: "missing" }; |
114 | 163 | } |
115 | 164 |
|
116 | 165 | export function resolveFirecrawlApiKey(cfg?: OpenClawConfig): string | undefined { |
117 | 166 | const pluginConfig = cfg?.plugins?.entries?.firecrawl?.config as PluginEntryConfig; |
118 | 167 | const search = resolveFirecrawlSearchConfig(cfg); |
119 | 168 | const fetch = resolveFirecrawlFetchConfig(cfg); |
120 | | - return ( |
121 | | - normalizeConfiguredSecret( |
122 | | - pluginConfig?.webFetch?.apiKey, |
123 | | - "plugins.entries.firecrawl.config.webFetch.apiKey", |
124 | | - ) || |
125 | | - normalizeConfiguredSecret( |
126 | | - search?.apiKey, |
127 | | - "plugins.entries.firecrawl.config.webSearch.apiKey", |
128 | | - ) || |
129 | | - normalizeConfiguredSecret(search?.apiKey, "tools.web.search.firecrawl.apiKey") || |
130 | | - normalizeConfiguredSecret(fetch?.apiKey, "tools.web.fetch.firecrawl.apiKey") || |
131 | | - normalizeSecretInput(process.env.FIRECRAWL_API_KEY) || |
132 | | - undefined |
133 | | - ); |
| 169 | + const configuredCandidates: Array<{ value: unknown; path: string }> = [ |
| 170 | + { |
| 171 | + value: pluginConfig?.webFetch?.apiKey, |
| 172 | + path: "plugins.entries.firecrawl.config.webFetch.apiKey", |
| 173 | + }, |
| 174 | + { |
| 175 | + value: search?.apiKey, |
| 176 | + path: "plugins.entries.firecrawl.config.webSearch.apiKey", |
| 177 | + }, |
| 178 | + { |
| 179 | + value: search?.apiKey, |
| 180 | + path: "tools.web.search.firecrawl.apiKey", |
| 181 | + }, |
| 182 | + { |
| 183 | + value: fetch?.apiKey, |
| 184 | + path: "tools.web.fetch.firecrawl.apiKey", |
| 185 | + }, |
| 186 | + ]; |
| 187 | + let blockedConfiguredSecret = false; |
| 188 | + for (const candidate of configuredCandidates) { |
| 189 | + const resolved = resolveConfiguredSecret(candidate.value, candidate.path, cfg); |
| 190 | + if (resolved.status === "available") { |
| 191 | + return resolved.value; |
| 192 | + } |
| 193 | + if (resolved.status === "blocked") { |
| 194 | + blockedConfiguredSecret = true; |
| 195 | + } |
| 196 | + } |
| 197 | + if (blockedConfiguredSecret) { |
| 198 | + return undefined; |
| 199 | + } |
| 200 | + return normalizeSecretInput(process.env[FIRECRAWL_API_KEY_ENV_VAR]) || undefined; |
134 | 201 | } |
135 | 202 |
|
136 | 203 | export function resolveFirecrawlBaseUrl(cfg?: OpenClawConfig): string { |
|
0 commit comments