mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 14:18:26 +00:00
refactor(web-tools): share URL allowlist resolver
This commit is contained in:
@@ -26,6 +26,7 @@ import {
|
|||||||
normalizeCacheKey,
|
normalizeCacheKey,
|
||||||
readCache,
|
readCache,
|
||||||
readResponseText,
|
readResponseText,
|
||||||
|
resolveWebUrlAllowlist,
|
||||||
resolveCacheTtlMs,
|
resolveCacheTtlMs,
|
||||||
resolveTimeoutSeconds,
|
resolveTimeoutSeconds,
|
||||||
withTimeout,
|
withTimeout,
|
||||||
@@ -75,17 +76,7 @@ type WebFetchConfig = NonNullable<OpenClawConfig["tools"]>["web"] extends infer
|
|||||||
type WebConfig = NonNullable<OpenClawConfig["tools"]>["web"];
|
type WebConfig = NonNullable<OpenClawConfig["tools"]>["web"];
|
||||||
|
|
||||||
export function resolveFetchUrlAllowlist(web?: WebConfig): string[] | undefined {
|
export function resolveFetchUrlAllowlist(web?: WebConfig): string[] | undefined {
|
||||||
if (!web || typeof web !== "object") {
|
return resolveWebUrlAllowlist(web);
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
if (!("urlAllowlist" in web)) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const allowlist = web.urlAllowlist;
|
|
||||||
if (!Array.isArray(allowlist)) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return allowlist.length > 0 ? allowlist : undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isUrlAllowedByAllowlist(url: string, allowlist: string[]): boolean {
|
export function isUrlAllowedByAllowlist(url: string, allowlist: string[]): boolean {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
normalizeCacheKey,
|
normalizeCacheKey,
|
||||||
readCache,
|
readCache,
|
||||||
readResponseText,
|
readResponseText,
|
||||||
|
resolveWebUrlAllowlist,
|
||||||
resolveCacheTtlMs,
|
resolveCacheTtlMs,
|
||||||
resolveTimeoutSeconds,
|
resolveTimeoutSeconds,
|
||||||
withTimeout,
|
withTimeout,
|
||||||
@@ -79,17 +80,7 @@ type WebSearchConfig = NonNullable<OpenClawConfig["tools"]>["web"] extends infer
|
|||||||
type WebConfig = NonNullable<OpenClawConfig["tools"]>["web"];
|
type WebConfig = NonNullable<OpenClawConfig["tools"]>["web"];
|
||||||
|
|
||||||
export function resolveUrlAllowlist(web?: WebConfig): string[] | undefined {
|
export function resolveUrlAllowlist(web?: WebConfig): string[] | undefined {
|
||||||
if (!web || typeof web !== "object") {
|
return resolveWebUrlAllowlist(web);
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
if (!("urlAllowlist" in web)) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const allowlist = web.urlAllowlist;
|
|
||||||
if (!Array.isArray(allowlist)) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return allowlist.length > 0 ? allowlist : undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function filterResultsByAllowlist(
|
export function filterResultsByAllowlist(
|
||||||
|
|||||||
@@ -8,6 +8,20 @@ export const DEFAULT_TIMEOUT_SECONDS = 30;
|
|||||||
export const DEFAULT_CACHE_TTL_MINUTES = 15;
|
export const DEFAULT_CACHE_TTL_MINUTES = 15;
|
||||||
const DEFAULT_CACHE_MAX_ENTRIES = 100;
|
const DEFAULT_CACHE_MAX_ENTRIES = 100;
|
||||||
|
|
||||||
|
export function resolveWebUrlAllowlist(web: unknown): string[] | undefined {
|
||||||
|
if (!web || typeof web !== "object") {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
if (!("urlAllowlist" in web)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const allowlist = (web as { urlAllowlist?: unknown }).urlAllowlist;
|
||||||
|
if (!Array.isArray(allowlist)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return allowlist.length > 0 ? allowlist : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
export function resolveTimeoutSeconds(value: unknown, fallback: number): number {
|
export function resolveTimeoutSeconds(value: unknown, fallback: number): number {
|
||||||
const parsed = typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
const parsed = typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
||||||
return Math.max(1, Math.floor(parsed));
|
return Math.max(1, Math.floor(parsed));
|
||||||
|
|||||||
Reference in New Issue
Block a user