|
1 | | -import type { Dispatcher } from "undici"; |
2 | 1 | import { Type } from "@sinclair/typebox"; |
3 | 2 | import type { OpenClawConfig } from "../../config/config.js"; |
4 | 3 | import type { AnyAgentTool } from "./common.js"; |
5 | | -import { |
6 | | - closeDispatcher, |
7 | | - createPinnedDispatcher, |
8 | | - resolvePinnedHostname, |
9 | | - SsrFBlockedError, |
10 | | -} from "../../infra/net/ssrf.js"; |
| 4 | +import { fetchWithSsrFGuard } from "../../infra/net/fetch-guard.js"; |
| 5 | +import { SsrFBlockedError } from "../../infra/net/ssrf.js"; |
11 | 6 | import { wrapExternalContent, wrapWebContent } from "../../security/external-content.js"; |
12 | 7 | import { stringEnum } from "../schema/typebox.js"; |
13 | 8 | import { jsonResult, readNumberParam, readStringParam } from "./common.js"; |
@@ -184,79 +179,6 @@ function looksLikeHtml(value: string): boolean { |
184 | 179 | return head.startsWith("<!doctype html") || head.startsWith("<html"); |
185 | 180 | } |
186 | 181 |
|
187 | | -function isRedirectStatus(status: number): boolean { |
188 | | - return status === 301 || status === 302 || status === 303 || status === 307 || status === 308; |
189 | | -} |
190 | | - |
191 | | -async function fetchWithRedirects(params: { |
192 | | - url: string; |
193 | | - maxRedirects: number; |
194 | | - timeoutSeconds: number; |
195 | | - userAgent: string; |
196 | | -}): Promise<{ response: Response; finalUrl: string; dispatcher: Dispatcher }> { |
197 | | - const signal = withTimeout(undefined, params.timeoutSeconds * 1000); |
198 | | - const visited = new Set<string>(); |
199 | | - let currentUrl = params.url; |
200 | | - let redirectCount = 0; |
201 | | - |
202 | | - while (true) { |
203 | | - let parsedUrl: URL; |
204 | | - try { |
205 | | - parsedUrl = new URL(currentUrl); |
206 | | - } catch { |
207 | | - throw new Error("Invalid URL: must be http or https"); |
208 | | - } |
209 | | - if (!["http:", "https:"].includes(parsedUrl.protocol)) { |
210 | | - throw new Error("Invalid URL: must be http or https"); |
211 | | - } |
212 | | - |
213 | | - const pinned = await resolvePinnedHostname(parsedUrl.hostname); |
214 | | - const dispatcher = createPinnedDispatcher(pinned); |
215 | | - let res: Response; |
216 | | - try { |
217 | | - res = await fetch(parsedUrl.toString(), { |
218 | | - method: "GET", |
219 | | - headers: { |
220 | | - Accept: "*/*", |
221 | | - "User-Agent": params.userAgent, |
222 | | - "Accept-Language": "en-US,en;q=0.9", |
223 | | - }, |
224 | | - signal, |
225 | | - redirect: "manual", |
226 | | - dispatcher, |
227 | | - } as RequestInit); |
228 | | - } catch (err) { |
229 | | - await closeDispatcher(dispatcher); |
230 | | - throw err; |
231 | | - } |
232 | | - |
233 | | - if (isRedirectStatus(res.status)) { |
234 | | - const location = res.headers.get("location"); |
235 | | - if (!location) { |
236 | | - await closeDispatcher(dispatcher); |
237 | | - throw new Error(`Redirect missing location header (${res.status})`); |
238 | | - } |
239 | | - redirectCount += 1; |
240 | | - if (redirectCount > params.maxRedirects) { |
241 | | - await closeDispatcher(dispatcher); |
242 | | - throw new Error(`Too many redirects (limit: ${params.maxRedirects})`); |
243 | | - } |
244 | | - const nextUrl = new URL(location, parsedUrl).toString(); |
245 | | - if (visited.has(nextUrl)) { |
246 | | - await closeDispatcher(dispatcher); |
247 | | - throw new Error("Redirect loop detected"); |
248 | | - } |
249 | | - visited.add(nextUrl); |
250 | | - void res.body?.cancel(); |
251 | | - await closeDispatcher(dispatcher); |
252 | | - currentUrl = nextUrl; |
253 | | - continue; |
254 | | - } |
255 | | - |
256 | | - return { response: res, finalUrl: currentUrl, dispatcher }; |
257 | | - } |
258 | | -} |
259 | | - |
260 | 182 | function formatWebFetchErrorDetail(params: { |
261 | 183 | detail: string; |
262 | 184 | contentType?: string | null; |
@@ -465,18 +387,22 @@ async function runWebFetch(params: { |
465 | 387 |
|
466 | 388 | const start = Date.now(); |
467 | 389 | let res: Response; |
468 | | - let dispatcher: Dispatcher | null = null; |
| 390 | + let release: (() => Promise<void>) | null = null; |
469 | 391 | let finalUrl = params.url; |
470 | 392 | try { |
471 | | - const result = await fetchWithRedirects({ |
| 393 | + const result = await fetchWithSsrFGuard({ |
472 | 394 | url: params.url, |
473 | 395 | maxRedirects: params.maxRedirects, |
474 | | - timeoutSeconds: params.timeoutSeconds, |
475 | | - userAgent: params.userAgent, |
| 396 | + timeoutMs: params.timeoutSeconds * 1000, |
| 397 | + headers: { |
| 398 | + Accept: "*/*", |
| 399 | + "User-Agent": params.userAgent, |
| 400 | + "Accept-Language": "en-US,en;q=0.9", |
| 401 | + }, |
476 | 402 | }); |
477 | 403 | res = result.response; |
478 | 404 | finalUrl = result.finalUrl; |
479 | | - dispatcher = result.dispatcher; |
| 405 | + release = result.release; |
480 | 406 | } catch (error) { |
481 | 407 | if (error instanceof SsrFBlockedError) { |
482 | 408 | throw error; |
@@ -630,7 +556,9 @@ async function runWebFetch(params: { |
630 | 556 | writeCache(FETCH_CACHE, cacheKey, payload, params.cacheTtlMs); |
631 | 557 | return payload; |
632 | 558 | } finally { |
633 | | - await closeDispatcher(dispatcher); |
| 559 | + if (release) { |
| 560 | + await release(); |
| 561 | + } |
634 | 562 | } |
635 | 563 | } |
636 | 564 |
|
|
0 commit comments