UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

54 lines (53 loc) • 2.65 kB
/** * Safe Fetch — SSRF-hardened binary download helper. * * Combines: * - `assertSafeUrl` (validates and rejects blocked IPs) * - undici `Agent` with custom `connect.lookup` so the actual connection * only ever dials addresses we validated (closes the DNS-rebinding window * where the resolver returns a public IP for the guard but a private IP * for the real request). * - `readBoundedBuffer` for size cap. * - `redirect: "manual"` so a 3xx → private-IP redirect can't bypass * the guard. * * Use this for **every** download of an external (caller-supplied or * third-party-returned) URL. Direct `fetch(url)` of such URLs is unsafe. * * Redirects are refused by default. A caller that opts in (`maxRedirects`) * gets each hop validated and pinned exactly like the first request, and the * caller's `headers` are sent to the first host only — a credential meant for * one origin is never replayed to the one it redirects to. `allowInsecure` * and `allowPrivateNetwork` relax the URL policy for SDK callers that have * said so explicitly; the server path never passes them. * * When the environment configures a proxy for the URL (HTTPS_PROXY / ALL_PROXY, * and the host is not in NO_PROXY) the download goes through that proxy and * nowhere else: there is no fallback to a direct connection. The proxy resolves * the host name, so DNS pinning cannot apply; the URL is vetted by * `assertSafeUrlForProxy` instead (see there for what that covers). * * @module utils/safeFetch */ import type { SafeDownloadOptions, SafeDownloadResult } from "../types/index.js"; /** * Safely download an external URL and report what came back. * * Same guarantees as {@link safeDownload}; additionally returns the final * response's `Content-Type`/`Content-Encoding`, the URL that produced the body * and how many redirect hops were taken. * * @throws {Error} if any URL on the way is unsafe, the response is too large, * a redirect is encountered beyond what `maxRedirects` allows, or the HTTP * status indicates failure. */ export declare function safeDownloadWithMeta(url: string, options: SafeDownloadOptions): Promise<SafeDownloadResult>; /** * Safely download a binary asset from an external URL. * * @throws {Error} if the URL is unsafe, the response is too large, a redirect * is encountered, or the HTTP status indicates failure. */ export declare function safeDownload(url: string, options: SafeDownloadOptions): Promise<Buffer>; /** Hop count applied when a caller opts into redirects without naming one. */ export declare function defaultRedirectHops(follow: boolean): number;