mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 17:44:33 +00:00
refactor(net): unify proxy env checks and guarded fetch modes
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
import http from "node:http";
|
||||
import https from "node:https";
|
||||
import { isLoopbackHost } from "../gateway/net.js";
|
||||
import { hasProxyEnvConfigured } from "../infra/net/proxy-env.js";
|
||||
|
||||
/** HTTP agent that never uses a proxy — for localhost CDP connections. */
|
||||
const directHttpAgent = new http.Agent();
|
||||
@@ -39,15 +40,7 @@ export function getDirectAgentForCdp(url: string): http.Agent | https.Agent | un
|
||||
* interfere with loopback connections.
|
||||
*/
|
||||
export function hasProxyEnv(): boolean {
|
||||
const env = process.env;
|
||||
return Boolean(
|
||||
env.HTTP_PROXY ||
|
||||
env.http_proxy ||
|
||||
env.HTTPS_PROXY ||
|
||||
env.https_proxy ||
|
||||
env.ALL_PROXY ||
|
||||
env.all_proxy,
|
||||
);
|
||||
return hasProxyEnvConfigured();
|
||||
}
|
||||
|
||||
const LOOPBACK_ENTRIES = "localhost,127.0.0.1,[::1]";
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { hasProxyEnvConfigured } from "../infra/net/proxy-env.js";
|
||||
import {
|
||||
isPrivateNetworkAllowedByPolicy,
|
||||
resolvePinnedHostnameWithPolicy,
|
||||
type LookupFn,
|
||||
type SsrFPolicy,
|
||||
@@ -6,28 +8,6 @@ import {
|
||||
|
||||
const NETWORK_NAVIGATION_PROTOCOLS = new Set(["http:", "https:"]);
|
||||
const SAFE_NON_NETWORK_URLS = new Set(["about:blank"]);
|
||||
const ENV_PROXY_KEYS = [
|
||||
"HTTP_PROXY",
|
||||
"HTTPS_PROXY",
|
||||
"ALL_PROXY",
|
||||
"http_proxy",
|
||||
"https_proxy",
|
||||
"all_proxy",
|
||||
] as const;
|
||||
|
||||
function hasEnvProxyConfigured(): boolean {
|
||||
for (const key of ENV_PROXY_KEYS) {
|
||||
const value = process.env[key];
|
||||
if (typeof value === "string" && value.trim().length > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function allowsPrivateNetworkNavigation(policy?: SsrFPolicy): boolean {
|
||||
return policy?.dangerouslyAllowPrivateNetwork === true || policy?.allowPrivateNetwork === true;
|
||||
}
|
||||
|
||||
function isAllowedNonNetworkNavigationUrl(parsed: URL): boolean {
|
||||
// Keep non-network navigation explicit; about:blank is the only allowed bootstrap URL.
|
||||
@@ -82,7 +62,7 @@ export async function assertBrowserNavigationAllowed(
|
||||
// can bypass strict destination-binding intent from pre-navigation DNS checks.
|
||||
// In strict mode, fail closed unless private-network navigation is explicitly
|
||||
// enabled by policy.
|
||||
if (hasEnvProxyConfigured() && !allowsPrivateNetworkNavigation(opts.ssrfPolicy)) {
|
||||
if (hasProxyEnvConfigured() && !isPrivateNetworkAllowedByPolicy(opts.ssrfPolicy)) {
|
||||
throw new InvalidBrowserNavigationUrlError(
|
||||
"Navigation blocked: strict browser SSRF policy cannot be enforced while env proxy variables are set",
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user