@blockrun/clawrouter 0.12.298 → 0.12.299

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/cli.js +147 -73
  2. package/dist/index.js +146 -72
  3. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -67263,7 +67263,7 @@ var require_websocket = __commonJS({
67263
67263
  var net3 = __require("net");
67264
67264
  var tls2 = __require("tls");
67265
67265
  var { randomBytes: randomBytes9, createHash: createHash6 } = __require("crypto");
67266
- var { Duplex, Readable: Readable3 } = __require("stream");
67266
+ var { Duplex, Readable: Readable4 } = __require("stream");
67267
67267
  var { URL: URL3 } = __require("url");
67268
67268
  var PerMessageDeflate2 = require_permessage_deflate();
67269
67269
  var Receiver2 = require_receiver();
@@ -104625,6 +104625,11 @@ ${lines.join("\n")}`;
104625
104625
  });
104626
104626
 
104627
104627
  // src/local-guard.ts
104628
+ import { request as httpRequest } from "http";
104629
+ import { request as httpsRequest } from "https";
104630
+ import { lookup as dnsLookup } from "dns/promises";
104631
+ import { isIP } from "net";
104632
+ import { Readable } from "stream";
104628
104633
  import { closeSync as closeSync3, fstatSync as fstatSync2, openSync as openSync3, readSync as readSync2, realpathSync } from "fs";
104629
104634
  import { homedir as homedir11 } from "os";
104630
104635
  import { join as join15 } from "path";
@@ -104701,18 +104706,84 @@ function isBlockedSsrfHost(hostname) {
104701
104706
  }
104702
104707
  return false;
104703
104708
  }
104709
+ async function resolvePublicAddresses(hostname, resolve2 = systemResolver) {
104710
+ const h = hostname.replace(/^\[/, "").replace(/\]$/, "");
104711
+ const family = isIP(h);
104712
+ const addrs = family ? [{ address: h, family }] : await resolve2(h);
104713
+ if (addrs.length === 0) throw new Error(`could not resolve ${hostname}`);
104714
+ const blocked = addrs.find((a) => isBlockedSsrfHost(a.address));
104715
+ if (blocked) {
104716
+ throw new Error(
104717
+ `refusing to fetch ${hostname}: it resolves to a private/loopback/metadata address (${blocked.address})`
104718
+ );
104719
+ }
104720
+ return addrs;
104721
+ }
104722
+ function pinnedLookup(addrs) {
104723
+ return (_hostname, options, callback) => {
104724
+ if (options.all) {
104725
+ callback(null, addrs);
104726
+ } else {
104727
+ callback(null, addrs[0].address, addrs[0].family);
104728
+ }
104729
+ };
104730
+ }
104731
+ function pinnedFetch(url2, addrs, init2 = {}) {
104732
+ const method = (init2.method ?? "GET").toUpperCase();
104733
+ const body = init2.body;
104734
+ if (body != null && typeof body !== "string" && !(body instanceof Uint8Array)) {
104735
+ return Promise.reject(new TypeError("pinnedFetch supports only string or byte bodies"));
104736
+ }
104737
+ const headers = Object.fromEntries(new Headers(init2.headers));
104738
+ const send = url2.protocol === "https:" ? httpsRequest : httpRequest;
104739
+ return new Promise((resolve2, reject) => {
104740
+ const req = send(
104741
+ url2,
104742
+ { method, headers, lookup: pinnedLookup(addrs), signal: init2.signal ?? void 0 },
104743
+ (res) => {
104744
+ const status2 = res.statusCode ?? 0;
104745
+ if (status2 < 200 || status2 > 599) {
104746
+ res.destroy();
104747
+ reject(new Error(`unexpected HTTP status ${status2} from ${url2.host}`));
104748
+ return;
104749
+ }
104750
+ const resHeaders = new Headers();
104751
+ for (let i = 0; i + 1 < res.rawHeaders.length; i += 2) {
104752
+ resHeaders.append(res.rawHeaders[i], res.rawHeaders[i + 1]);
104753
+ }
104754
+ const noBody = method === "HEAD" || NULL_BODY_STATUSES.has(status2);
104755
+ if (noBody) res.resume();
104756
+ resolve2(
104757
+ new Response(noBody ? null : Readable.toWeb(res), {
104758
+ status: status2,
104759
+ statusText: res.statusMessage,
104760
+ headers: resHeaders
104761
+ })
104762
+ );
104763
+ }
104764
+ );
104765
+ req.on("error", reject);
104766
+ req.end(body ?? void 0);
104767
+ });
104768
+ }
104704
104769
  async function ssrfSafeFetch(url2, init2 = {}, maxHops = 5) {
104705
- const { allowPrivate, ...fetchInit } = init2;
104770
+ const { allowPrivate, viaUpstreamProxy, resolve: resolve2, ...fetchInit } = init2;
104706
104771
  let current = url2;
104707
104772
  for (let hop = 0; hop <= maxHops; hop++) {
104708
104773
  const u = new URL(current);
104709
104774
  if (u.protocol !== "http:" && u.protocol !== "https:") {
104710
104775
  throw new Error(`refusing to fetch non-http(s) URL (${u.protocol})`);
104711
104776
  }
104712
- if (!allowPrivate && isBlockedSsrfHost(u.hostname)) {
104713
- throw new Error(`refusing to fetch a private/loopback/metadata address: ${u.hostname}`);
104777
+ let res;
104778
+ if (allowPrivate) {
104779
+ res = await fetch(current, { ...fetchInit, redirect: "manual" });
104780
+ } else {
104781
+ if (isBlockedSsrfHost(u.hostname)) {
104782
+ throw new Error(`refusing to fetch a private/loopback/metadata address: ${u.hostname}`);
104783
+ }
104784
+ const addrs = await resolvePublicAddresses(u.hostname, resolve2);
104785
+ res = viaUpstreamProxy ? await fetch(current, { ...fetchInit, redirect: "manual" }) : await pinnedFetch(u, addrs, fetchInit);
104714
104786
  }
104715
- const res = await fetch(current, { ...fetchInit, redirect: "manual" });
104716
104787
  if (res.status >= 300 && res.status < 400) {
104717
104788
  const loc = res.headers.get("location");
104718
104789
  if (!loc) return res;
@@ -104751,11 +104822,13 @@ function readLocalImageAsDataUri(filePath) {
104751
104822
  closeSync3(fd);
104752
104823
  }
104753
104824
  }
104754
- var METADATA_HOSTS, MAX_LOCAL_IMAGE_BYTES, IMAGE_SIGNATURES;
104825
+ var METADATA_HOSTS, systemResolver, NULL_BODY_STATUSES, MAX_LOCAL_IMAGE_BYTES, IMAGE_SIGNATURES;
104755
104826
  var init_local_guard = __esm({
104756
104827
  "src/local-guard.ts"() {
104757
104828
  "use strict";
104758
104829
  METADATA_HOSTS = /* @__PURE__ */ new Set(["metadata", "metadata.goog", "instance-data"]);
104830
+ systemResolver = (hostname) => dnsLookup(hostname, { all: true, verbatim: true });
104831
+ NULL_BODY_STATUSES = /* @__PURE__ */ new Set([101, 103, 204, 205, 304]);
104759
104832
  MAX_LOCAL_IMAGE_BYTES = 25 * 1024 * 1024;
104760
104833
  IMAGE_SIGNATURES = [
104761
104834
  {
@@ -108278,13 +108351,13 @@ var require_util2 = __commonJS({
108278
108351
  function TAOCheck() {
108279
108352
  return "success";
108280
108353
  }
108281
- function appendFetchMetadata(httpRequest) {
108282
- if (!isURLPotentiallyTrustworthy(requestCurrentURL(httpRequest))) {
108354
+ function appendFetchMetadata(httpRequest2) {
108355
+ if (!isURLPotentiallyTrustworthy(requestCurrentURL(httpRequest2))) {
108283
108356
  return;
108284
108357
  }
108285
108358
  let header = null;
108286
- header = httpRequest.mode;
108287
- httpRequest.headersList.set("sec-fetch-mode", header, true);
108359
+ header = httpRequest2.mode;
108360
+ httpRequest2.headersList.set("sec-fetch-mode", header, true);
108288
108361
  }
108289
108362
  function appendRequestOriginHeader(request2) {
108290
108363
  let serializedOrigin = request2.origin;
@@ -120163,7 +120236,7 @@ var require_readable = __commonJS({
120163
120236
  "use strict";
120164
120237
  var assert10 = __require("assert");
120165
120238
  var { addAbortListener } = __require("events");
120166
- var { Readable: Readable3 } = __require("stream");
120239
+ var { Readable: Readable4 } = __require("stream");
120167
120240
  var { RequestAbortedError, NotSupportedError, InvalidArgumentError, AbortError } = require_errors();
120168
120241
  var util5 = require_util();
120169
120242
  var kConsume = /* @__PURE__ */ Symbol("kConsume");
@@ -120176,7 +120249,7 @@ var require_readable = __commonJS({
120176
120249
  var kBytesRead = /* @__PURE__ */ Symbol("kBytesRead");
120177
120250
  var noop2 = () => {
120178
120251
  };
120179
- var BodyReadable = class extends Readable3 {
120252
+ var BodyReadable = class extends Readable4 {
120180
120253
  /**
120181
120254
  * @param {object} opts
120182
120255
  * @param {(this: Readable, size: number) => void} opts.resume
@@ -120575,7 +120648,7 @@ var require_api_request = __commonJS({
120575
120648
  "use strict";
120576
120649
  var assert10 = __require("assert");
120577
120650
  var { AsyncResource } = __require("async_hooks");
120578
- var { Readable: Readable3 } = require_readable();
120651
+ var { Readable: Readable4 } = require_readable();
120579
120652
  var { InvalidArgumentError, RequestAbortedError } = require_errors();
120580
120653
  var util5 = require_util();
120581
120654
  function noop2() {
@@ -120661,7 +120734,7 @@ var require_api_request = __commonJS({
120661
120734
  const parsedHeaders = headers;
120662
120735
  const contentType = parsedHeaders?.["content-type"];
120663
120736
  const contentLength = parsedHeaders?.["content-length"];
120664
- const res = new Readable3({
120737
+ const res = new Readable4({
120665
120738
  resume: () => controller.resume(),
120666
120739
  abort: (reason) => controller.abort(reason),
120667
120740
  contentType,
@@ -121037,7 +121110,7 @@ var require_api_pipeline = __commonJS({
121037
121110
  "node_modules/undici/lib/api/api-pipeline.js"(exports, module) {
121038
121111
  "use strict";
121039
121112
  var {
121040
- Readable: Readable3,
121113
+ Readable: Readable4,
121041
121114
  Duplex,
121042
121115
  PassThrough
121043
121116
  } = __require("stream");
@@ -121054,7 +121127,7 @@ var require_api_pipeline = __commonJS({
121054
121127
  function noop2() {
121055
121128
  }
121056
121129
  var kResume = /* @__PURE__ */ Symbol("resume");
121057
- var PipelineRequest = class extends Readable3 {
121130
+ var PipelineRequest = class extends Readable4 {
121058
121131
  constructor() {
121059
121132
  super({ autoDestroy: true });
121060
121133
  this[kResume] = null;
@@ -121072,7 +121145,7 @@ var require_api_pipeline = __commonJS({
121072
121145
  callback(err);
121073
121146
  }
121074
121147
  };
121075
- var PipelineResponse = class extends Readable3 {
121148
+ var PipelineResponse = class extends Readable4 {
121076
121149
  constructor(resume2) {
121077
121150
  super({ autoDestroy: true });
121078
121151
  this[kResume] = resume2;
@@ -124126,7 +124199,7 @@ var require_dump = __commonJS({
124126
124199
  var require_dns = __commonJS({
124127
124200
  "node_modules/undici/lib/interceptor/dns.js"(exports, module) {
124128
124201
  "use strict";
124129
- var { isIP: isIP2 } = __require("net");
124202
+ var { isIP: isIP3 } = __require("net");
124130
124203
  var { lookup } = __require("dns");
124131
124204
  var DecoratorHandler = require_decorator_handler();
124132
124205
  var { InvalidArgumentError, InformationalError } = require_errors();
@@ -124543,7 +124616,7 @@ var require_dns = __commonJS({
124543
124616
  return dispatch(origDispatchOpts, handler);
124544
124617
  }
124545
124618
  const origin2 = origDispatchOpts.origin.constructor === URL ? origDispatchOpts.origin : new URL(origDispatchOpts.origin);
124546
- if (isIP2(origin2.hostname) !== 0) {
124619
+ if (isIP3(origin2.hostname) !== 0) {
124547
124620
  return dispatch(origDispatchOpts, handler);
124548
124621
  }
124549
124622
  instance.runLookup(origin2, origDispatchOpts, (err, newOrigin) => {
@@ -126614,7 +126687,7 @@ var require_cache2 = __commonJS({
126614
126687
  "node_modules/undici/lib/interceptor/cache.js"(exports, module) {
126615
126688
  "use strict";
126616
126689
  var assert10 = __require("assert");
126617
- var { Readable: Readable3 } = __require("stream");
126690
+ var { Readable: Readable4 } = __require("stream");
126618
126691
  var util5 = require_util();
126619
126692
  var CacheHandler = require_cache_handler();
126620
126693
  var MemoryCacheStore = require_memory_cache_store();
@@ -126810,7 +126883,7 @@ var require_cache2 = __commonJS({
126810
126883
  return dispatch(opts, new CacheHandler(globalOpts, cacheKey3, handler));
126811
126884
  }
126812
126885
  function sendCachedValue(handler, opts, result, age, context, isStale2) {
126813
- const stream4 = util5.isStream(result.body) ? result.body : Readable3.from(result.body ?? []);
126886
+ const stream4 = util5.isStream(result.body) ? result.body : Readable4.from(result.body ?? []);
126814
126887
  assert10(!stream4.destroyed, "stream should not be destroyed");
126815
126888
  assert10(!stream4.readableDidRead, "stream should not be readableDidRead");
126816
126889
  let aborted = false;
@@ -130275,7 +130348,7 @@ var require_fetch = __commonJS({
130275
130348
  subresourceSet
130276
130349
  } = require_constants3();
130277
130350
  var EE = __require("events");
130278
- var { Readable: Readable3, pipeline: pipeline2, finished: finished2, isErrored, isReadable } = __require("stream");
130351
+ var { Readable: Readable4, pipeline: pipeline2, finished: finished2, isErrored, isReadable } = __require("stream");
130279
130352
  var { addAbortListener, bufferToLowerCasedHeaderName } = require_util();
130280
130353
  var { SocketError } = require_errors();
130281
130354
  var { dataURLProcessor, serializeAMimeType, minimizeSupportedMimeType } = require_data_url();
@@ -130904,90 +130977,90 @@ var require_fetch = __commonJS({
130904
130977
  async function httpNetworkOrCacheFetch(fetchParams, isAuthenticationFetch = false, isNewConnectionFetch = false) {
130905
130978
  const request2 = fetchParams.request;
130906
130979
  let httpFetchParams = null;
130907
- let httpRequest = null;
130980
+ let httpRequest2 = null;
130908
130981
  let response = null;
130909
130982
  const httpCache = null;
130910
130983
  const revalidatingFlag = false;
130911
130984
  if (request2.window === "no-window" && request2.redirect === "error") {
130912
130985
  httpFetchParams = fetchParams;
130913
- httpRequest = request2;
130986
+ httpRequest2 = request2;
130914
130987
  } else {
130915
130988
  if (request2.body?.source != null) {
130916
- httpRequest = cloneRequest(request2);
130989
+ httpRequest2 = cloneRequest(request2);
130917
130990
  } else {
130918
- httpRequest = cloneRequest({ ...request2, body: null });
130919
- httpRequest.body = request2.body;
130991
+ httpRequest2 = cloneRequest({ ...request2, body: null });
130992
+ httpRequest2.body = request2.body;
130920
130993
  }
130921
130994
  httpFetchParams = { ...fetchParams };
130922
- httpFetchParams.request = httpRequest;
130995
+ httpFetchParams.request = httpRequest2;
130923
130996
  }
130924
130997
  const includeCredentials = request2.credentials === "include" || request2.credentials === "same-origin" && request2.responseTainting === "basic";
130925
- const contentLength = httpRequest.body ? httpRequest.body.length : null;
130998
+ const contentLength = httpRequest2.body ? httpRequest2.body.length : null;
130926
130999
  let contentLengthHeaderValue = null;
130927
- if (httpRequest.body == null && ["POST", "PUT"].includes(httpRequest.method)) {
131000
+ if (httpRequest2.body == null && ["POST", "PUT"].includes(httpRequest2.method)) {
130928
131001
  contentLengthHeaderValue = "0";
130929
131002
  }
130930
131003
  if (contentLength != null) {
130931
131004
  contentLengthHeaderValue = isomorphicEncode(`${contentLength}`);
130932
131005
  }
130933
- if (contentLengthHeaderValue != null && !httpRequest.headersList.contains("content-length", true)) {
130934
- httpRequest.headersList.append("content-length", contentLengthHeaderValue, true);
131006
+ if (contentLengthHeaderValue != null && !httpRequest2.headersList.contains("content-length", true)) {
131007
+ httpRequest2.headersList.append("content-length", contentLengthHeaderValue, true);
130935
131008
  }
130936
- if (contentLength != null && httpRequest.keepalive) {
131009
+ if (contentLength != null && httpRequest2.keepalive) {
130937
131010
  }
130938
- if (webidl.is.URL(httpRequest.referrer)) {
130939
- httpRequest.headersList.append("referer", isomorphicEncode(httpRequest.referrer.href), true);
131011
+ if (webidl.is.URL(httpRequest2.referrer)) {
131012
+ httpRequest2.headersList.append("referer", isomorphicEncode(httpRequest2.referrer.href), true);
130940
131013
  }
130941
- appendRequestOriginHeader(httpRequest);
130942
- appendFetchMetadata(httpRequest);
130943
- if (!httpRequest.headersList.contains("user-agent", true)) {
130944
- httpRequest.headersList.append("user-agent", defaultUserAgent, true);
131014
+ appendRequestOriginHeader(httpRequest2);
131015
+ appendFetchMetadata(httpRequest2);
131016
+ if (!httpRequest2.headersList.contains("user-agent", true)) {
131017
+ httpRequest2.headersList.append("user-agent", defaultUserAgent, true);
130945
131018
  }
130946
- if (httpRequest.cache === "default" && (httpRequest.headersList.contains("if-modified-since", true) || httpRequest.headersList.contains("if-none-match", true) || httpRequest.headersList.contains("if-unmodified-since", true) || httpRequest.headersList.contains("if-match", true) || httpRequest.headersList.contains("if-range", true))) {
130947
- httpRequest.cache = "no-store";
131019
+ if (httpRequest2.cache === "default" && (httpRequest2.headersList.contains("if-modified-since", true) || httpRequest2.headersList.contains("if-none-match", true) || httpRequest2.headersList.contains("if-unmodified-since", true) || httpRequest2.headersList.contains("if-match", true) || httpRequest2.headersList.contains("if-range", true))) {
131020
+ httpRequest2.cache = "no-store";
130948
131021
  }
130949
- if (httpRequest.cache === "no-cache" && !httpRequest.preventNoCacheCacheControlHeaderModification && !httpRequest.headersList.contains("cache-control", true)) {
130950
- httpRequest.headersList.append("cache-control", "max-age=0", true);
131022
+ if (httpRequest2.cache === "no-cache" && !httpRequest2.preventNoCacheCacheControlHeaderModification && !httpRequest2.headersList.contains("cache-control", true)) {
131023
+ httpRequest2.headersList.append("cache-control", "max-age=0", true);
130951
131024
  }
130952
- if (httpRequest.cache === "no-store" || httpRequest.cache === "reload") {
130953
- if (!httpRequest.headersList.contains("pragma", true)) {
130954
- httpRequest.headersList.append("pragma", "no-cache", true);
131025
+ if (httpRequest2.cache === "no-store" || httpRequest2.cache === "reload") {
131026
+ if (!httpRequest2.headersList.contains("pragma", true)) {
131027
+ httpRequest2.headersList.append("pragma", "no-cache", true);
130955
131028
  }
130956
- if (!httpRequest.headersList.contains("cache-control", true)) {
130957
- httpRequest.headersList.append("cache-control", "no-cache", true);
131029
+ if (!httpRequest2.headersList.contains("cache-control", true)) {
131030
+ httpRequest2.headersList.append("cache-control", "no-cache", true);
130958
131031
  }
130959
131032
  }
130960
- if (httpRequest.headersList.contains("range", true)) {
130961
- httpRequest.headersList.append("accept-encoding", "identity", true);
131033
+ if (httpRequest2.headersList.contains("range", true)) {
131034
+ httpRequest2.headersList.append("accept-encoding", "identity", true);
130962
131035
  }
130963
- if (!httpRequest.headersList.contains("accept-encoding", true)) {
130964
- if (urlHasHttpsScheme(requestCurrentURL(httpRequest))) {
130965
- httpRequest.headersList.append("accept-encoding", "br, gzip, deflate, zstd", true);
131036
+ if (!httpRequest2.headersList.contains("accept-encoding", true)) {
131037
+ if (urlHasHttpsScheme(requestCurrentURL(httpRequest2))) {
131038
+ httpRequest2.headersList.append("accept-encoding", "br, gzip, deflate, zstd", true);
130966
131039
  } else {
130967
- httpRequest.headersList.append("accept-encoding", "gzip, deflate", true);
131040
+ httpRequest2.headersList.append("accept-encoding", "gzip, deflate", true);
130968
131041
  }
130969
131042
  }
130970
- httpRequest.headersList.delete("host", true);
131043
+ httpRequest2.headersList.delete("host", true);
130971
131044
  if (includeCredentials) {
130972
- if (!httpRequest.headersList.contains("authorization", true)) {
131045
+ if (!httpRequest2.headersList.contains("authorization", true)) {
130973
131046
  let authorizationValue = null;
130974
- if (hasAuthenticationEntry(httpRequest) && (httpRequest.useURLCredentials === void 0 || !includesCredentials(requestCurrentURL(httpRequest)))) {
130975
- } else if (includesCredentials(requestCurrentURL(httpRequest)) && isAuthenticationFetch) {
130976
- const { username, password } = requestCurrentURL(httpRequest);
131047
+ if (hasAuthenticationEntry(httpRequest2) && (httpRequest2.useURLCredentials === void 0 || !includesCredentials(requestCurrentURL(httpRequest2)))) {
131048
+ } else if (includesCredentials(requestCurrentURL(httpRequest2)) && isAuthenticationFetch) {
131049
+ const { username, password } = requestCurrentURL(httpRequest2);
130977
131050
  authorizationValue = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
130978
131051
  }
130979
131052
  if (authorizationValue !== null) {
130980
- httpRequest.headersList.append("Authorization", authorizationValue, false);
131053
+ httpRequest2.headersList.append("Authorization", authorizationValue, false);
130981
131054
  }
130982
131055
  }
130983
131056
  }
130984
131057
  if (httpCache == null) {
130985
- httpRequest.cache = "no-store";
131058
+ httpRequest2.cache = "no-store";
130986
131059
  }
130987
- if (httpRequest.cache !== "no-store" && httpRequest.cache !== "reload") {
131060
+ if (httpRequest2.cache !== "no-store" && httpRequest2.cache !== "reload") {
130988
131061
  }
130989
131062
  if (response == null) {
130990
- if (httpRequest.cache === "only-if-cached") {
131063
+ if (httpRequest2.cache === "only-if-cached") {
130991
131064
  return makeNetworkError("only if cached");
130992
131065
  }
130993
131066
  const forwardResponse = await httpNetworkFetch(
@@ -130995,7 +131068,7 @@ var require_fetch = __commonJS({
130995
131068
  includeCredentials,
130996
131069
  isNewConnectionFetch
130997
131070
  );
130998
- if (!safeMethodsSet.has(httpRequest.method) && forwardResponse.status >= 200 && forwardResponse.status <= 399) {
131071
+ if (!safeMethodsSet.has(httpRequest2.method) && forwardResponse.status >= 200 && forwardResponse.status <= 399) {
130999
131072
  }
131000
131073
  if (revalidatingFlag && forwardResponse.status === 304) {
131001
131074
  }
@@ -131003,12 +131076,12 @@ var require_fetch = __commonJS({
131003
131076
  response = forwardResponse;
131004
131077
  }
131005
131078
  }
131006
- response.urlList = [...httpRequest.urlList];
131007
- if (httpRequest.headersList.contains("range", true)) {
131079
+ response.urlList = [...httpRequest2.urlList];
131080
+ if (httpRequest2.headersList.contains("range", true)) {
131008
131081
  response.rangeRequested = true;
131009
131082
  }
131010
131083
  response.requestIncludesCredentials = includeCredentials;
131011
- if (response.status === 401 && httpRequest.responseTainting !== "cors" && includeCredentials && (request2.useURLCredentials !== void 0 || isTraversableNavigable(request2.traversableForUserPrompts))) {
131084
+ if (response.status === 401 && httpRequest2.responseTainting !== "cors" && includeCredentials && (request2.useURLCredentials !== void 0 || isTraversableNavigable(request2.traversableForUserPrompts))) {
131012
131085
  if (request2.body != null) {
131013
131086
  if (request2.body.source == null) {
131014
131087
  return response;
@@ -131305,7 +131378,7 @@ var require_fetch = __commonJS({
131305
131378
  }
131306
131379
  }
131307
131380
  }
131308
- this.body = new Readable3({ read: () => controller.resume() });
131381
+ this.body = new Readable4({ read: () => controller.resume() });
131309
131382
  const onError = (err) => this.onResponseError(controller, err);
131310
131383
  resolve2({
131311
131384
  status: status2,
@@ -137039,7 +137112,7 @@ var init_v1 = __esm({
137039
137112
  import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
137040
137113
  import { createHmac } from "crypto";
137041
137114
  import { createServer } from "http";
137042
- import { finished, Readable } from "stream";
137115
+ import { finished, Readable as Readable2 } from "stream";
137043
137116
  import { pipeline } from "stream/promises";
137044
137117
  import { homedir as homedir13 } from "os";
137045
137118
  import { join as join17 } from "path";
@@ -137895,7 +137968,7 @@ async function proxyPaidApiRequest(req, res, apiBase, payFetch, getActualPayment
137895
137968
  if (accountPassthrough) {
137896
137969
  if (upstream.body) {
137897
137970
  await pipeline(
137898
- Readable.fromWeb(upstream.body),
137971
+ Readable2.fromWeb(upstream.body),
137899
137972
  res,
137900
137973
  { signal: clientAbort.signal }
137901
137974
  );
@@ -138831,7 +138904,8 @@ async function startProxy(options) {
138831
138904
  } else if (val.startsWith("https://") || val.startsWith("http://")) {
138832
138905
  const imgResp = await ssrfSafeFetch(val, {
138833
138906
  signal: clientAbort.signal,
138834
- allowPrivate: process.env.CLAWROUTER_ALLOW_PRIVATE_FETCH === "1"
138907
+ allowPrivate: process.env.CLAWROUTER_ALLOW_PRIVATE_FETCH === "1",
138908
+ viaUpstreamProxy: Boolean(upstreamProxy)
138835
138909
  });
138836
138910
  if (!imgResp.ok)
138837
138911
  throw new Error(`Failed to download ${field} from ${val}: HTTP ${imgResp.status}`);
@@ -159684,7 +159758,7 @@ var init_readBlob = __esm({
159684
159758
 
159685
159759
  // node_modules/axios/lib/helpers/formDataToStream.js
159686
159760
  import util2 from "util";
159687
- import { Readable as Readable2 } from "stream";
159761
+ import { Readable as Readable3 } from "stream";
159688
159762
  var BOUNDARY_ALPHABET, textEncoder, CRLF, CRLF_BYTES, CRLF_BYTES_COUNT, FormDataPart, formDataToStream, formDataToStream_default;
159689
159763
  var init_formDataToStream = __esm({
159690
159764
  "node_modules/axios/lib/helpers/formDataToStream.js"() {
@@ -159764,7 +159838,7 @@ var init_formDataToStream = __esm({
159764
159838
  computedHeaders["Content-Length"] = contentLength;
159765
159839
  }
159766
159840
  headersHandler && headersHandler(computedHeaders);
159767
- return Readable2.from(
159841
+ return Readable3.from(
159768
159842
  (async function* () {
159769
159843
  for (const part of parts) {
159770
159844
  yield boundaryBytes;
package/dist/index.js CHANGED
@@ -37680,7 +37680,7 @@ var require_websocket = __commonJS({
37680
37680
  var net3 = __require("net");
37681
37681
  var tls2 = __require("tls");
37682
37682
  var { randomBytes: randomBytes9, createHash: createHash6 } = __require("crypto");
37683
- var { Duplex, Readable: Readable3 } = __require("stream");
37683
+ var { Duplex, Readable: Readable4 } = __require("stream");
37684
37684
  var { URL: URL3 } = __require("url");
37685
37685
  var PerMessageDeflate2 = require_permessage_deflate();
37686
37686
  var Receiver2 = require_receiver();
@@ -76444,13 +76444,13 @@ var require_util2 = __commonJS({
76444
76444
  function TAOCheck() {
76445
76445
  return "success";
76446
76446
  }
76447
- function appendFetchMetadata(httpRequest) {
76448
- if (!isURLPotentiallyTrustworthy(requestCurrentURL(httpRequest))) {
76447
+ function appendFetchMetadata(httpRequest2) {
76448
+ if (!isURLPotentiallyTrustworthy(requestCurrentURL(httpRequest2))) {
76449
76449
  return;
76450
76450
  }
76451
76451
  let header = null;
76452
- header = httpRequest.mode;
76453
- httpRequest.headersList.set("sec-fetch-mode", header, true);
76452
+ header = httpRequest2.mode;
76453
+ httpRequest2.headersList.set("sec-fetch-mode", header, true);
76454
76454
  }
76455
76455
  function appendRequestOriginHeader(request2) {
76456
76456
  let serializedOrigin = request2.origin;
@@ -88329,7 +88329,7 @@ var require_readable = __commonJS({
88329
88329
  "use strict";
88330
88330
  var assert10 = __require("assert");
88331
88331
  var { addAbortListener } = __require("events");
88332
- var { Readable: Readable3 } = __require("stream");
88332
+ var { Readable: Readable4 } = __require("stream");
88333
88333
  var { RequestAbortedError, NotSupportedError, InvalidArgumentError, AbortError } = require_errors();
88334
88334
  var util5 = require_util();
88335
88335
  var kConsume = /* @__PURE__ */ Symbol("kConsume");
@@ -88342,7 +88342,7 @@ var require_readable = __commonJS({
88342
88342
  var kBytesRead = /* @__PURE__ */ Symbol("kBytesRead");
88343
88343
  var noop2 = () => {
88344
88344
  };
88345
- var BodyReadable = class extends Readable3 {
88345
+ var BodyReadable = class extends Readable4 {
88346
88346
  /**
88347
88347
  * @param {object} opts
88348
88348
  * @param {(this: Readable, size: number) => void} opts.resume
@@ -88741,7 +88741,7 @@ var require_api_request = __commonJS({
88741
88741
  "use strict";
88742
88742
  var assert10 = __require("assert");
88743
88743
  var { AsyncResource } = __require("async_hooks");
88744
- var { Readable: Readable3 } = require_readable();
88744
+ var { Readable: Readable4 } = require_readable();
88745
88745
  var { InvalidArgumentError, RequestAbortedError } = require_errors();
88746
88746
  var util5 = require_util();
88747
88747
  function noop2() {
@@ -88827,7 +88827,7 @@ var require_api_request = __commonJS({
88827
88827
  const parsedHeaders = headers;
88828
88828
  const contentType = parsedHeaders?.["content-type"];
88829
88829
  const contentLength = parsedHeaders?.["content-length"];
88830
- const res = new Readable3({
88830
+ const res = new Readable4({
88831
88831
  resume: () => controller.resume(),
88832
88832
  abort: (reason) => controller.abort(reason),
88833
88833
  contentType,
@@ -89203,7 +89203,7 @@ var require_api_pipeline = __commonJS({
89203
89203
  "node_modules/undici/lib/api/api-pipeline.js"(exports, module) {
89204
89204
  "use strict";
89205
89205
  var {
89206
- Readable: Readable3,
89206
+ Readable: Readable4,
89207
89207
  Duplex,
89208
89208
  PassThrough
89209
89209
  } = __require("stream");
@@ -89220,7 +89220,7 @@ var require_api_pipeline = __commonJS({
89220
89220
  function noop2() {
89221
89221
  }
89222
89222
  var kResume = /* @__PURE__ */ Symbol("resume");
89223
- var PipelineRequest = class extends Readable3 {
89223
+ var PipelineRequest = class extends Readable4 {
89224
89224
  constructor() {
89225
89225
  super({ autoDestroy: true });
89226
89226
  this[kResume] = null;
@@ -89238,7 +89238,7 @@ var require_api_pipeline = __commonJS({
89238
89238
  callback(err);
89239
89239
  }
89240
89240
  };
89241
- var PipelineResponse = class extends Readable3 {
89241
+ var PipelineResponse = class extends Readable4 {
89242
89242
  constructor(resume2) {
89243
89243
  super({ autoDestroy: true });
89244
89244
  this[kResume] = resume2;
@@ -92292,7 +92292,7 @@ var require_dump = __commonJS({
92292
92292
  var require_dns = __commonJS({
92293
92293
  "node_modules/undici/lib/interceptor/dns.js"(exports, module) {
92294
92294
  "use strict";
92295
- var { isIP: isIP2 } = __require("net");
92295
+ var { isIP: isIP3 } = __require("net");
92296
92296
  var { lookup } = __require("dns");
92297
92297
  var DecoratorHandler = require_decorator_handler();
92298
92298
  var { InvalidArgumentError, InformationalError } = require_errors();
@@ -92709,7 +92709,7 @@ var require_dns = __commonJS({
92709
92709
  return dispatch(origDispatchOpts, handler);
92710
92710
  }
92711
92711
  const origin2 = origDispatchOpts.origin.constructor === URL ? origDispatchOpts.origin : new URL(origDispatchOpts.origin);
92712
- if (isIP2(origin2.hostname) !== 0) {
92712
+ if (isIP3(origin2.hostname) !== 0) {
92713
92713
  return dispatch(origDispatchOpts, handler);
92714
92714
  }
92715
92715
  instance.runLookup(origin2, origDispatchOpts, (err, newOrigin) => {
@@ -94780,7 +94780,7 @@ var require_cache2 = __commonJS({
94780
94780
  "node_modules/undici/lib/interceptor/cache.js"(exports, module) {
94781
94781
  "use strict";
94782
94782
  var assert10 = __require("assert");
94783
- var { Readable: Readable3 } = __require("stream");
94783
+ var { Readable: Readable4 } = __require("stream");
94784
94784
  var util5 = require_util();
94785
94785
  var CacheHandler = require_cache_handler();
94786
94786
  var MemoryCacheStore = require_memory_cache_store();
@@ -94976,7 +94976,7 @@ var require_cache2 = __commonJS({
94976
94976
  return dispatch(opts, new CacheHandler(globalOpts, cacheKey3, handler));
94977
94977
  }
94978
94978
  function sendCachedValue(handler, opts, result, age, context, isStale2) {
94979
- const stream4 = util5.isStream(result.body) ? result.body : Readable3.from(result.body ?? []);
94979
+ const stream4 = util5.isStream(result.body) ? result.body : Readable4.from(result.body ?? []);
94980
94980
  assert10(!stream4.destroyed, "stream should not be destroyed");
94981
94981
  assert10(!stream4.readableDidRead, "stream should not be readableDidRead");
94982
94982
  let aborted = false;
@@ -98441,7 +98441,7 @@ var require_fetch = __commonJS({
98441
98441
  subresourceSet
98442
98442
  } = require_constants3();
98443
98443
  var EE = __require("events");
98444
- var { Readable: Readable3, pipeline: pipeline2, finished: finished2, isErrored, isReadable } = __require("stream");
98444
+ var { Readable: Readable4, pipeline: pipeline2, finished: finished2, isErrored, isReadable } = __require("stream");
98445
98445
  var { addAbortListener, bufferToLowerCasedHeaderName } = require_util();
98446
98446
  var { SocketError } = require_errors();
98447
98447
  var { dataURLProcessor, serializeAMimeType, minimizeSupportedMimeType } = require_data_url();
@@ -99070,90 +99070,90 @@ var require_fetch = __commonJS({
99070
99070
  async function httpNetworkOrCacheFetch(fetchParams, isAuthenticationFetch = false, isNewConnectionFetch = false) {
99071
99071
  const request2 = fetchParams.request;
99072
99072
  let httpFetchParams = null;
99073
- let httpRequest = null;
99073
+ let httpRequest2 = null;
99074
99074
  let response = null;
99075
99075
  const httpCache = null;
99076
99076
  const revalidatingFlag = false;
99077
99077
  if (request2.window === "no-window" && request2.redirect === "error") {
99078
99078
  httpFetchParams = fetchParams;
99079
- httpRequest = request2;
99079
+ httpRequest2 = request2;
99080
99080
  } else {
99081
99081
  if (request2.body?.source != null) {
99082
- httpRequest = cloneRequest(request2);
99082
+ httpRequest2 = cloneRequest(request2);
99083
99083
  } else {
99084
- httpRequest = cloneRequest({ ...request2, body: null });
99085
- httpRequest.body = request2.body;
99084
+ httpRequest2 = cloneRequest({ ...request2, body: null });
99085
+ httpRequest2.body = request2.body;
99086
99086
  }
99087
99087
  httpFetchParams = { ...fetchParams };
99088
- httpFetchParams.request = httpRequest;
99088
+ httpFetchParams.request = httpRequest2;
99089
99089
  }
99090
99090
  const includeCredentials = request2.credentials === "include" || request2.credentials === "same-origin" && request2.responseTainting === "basic";
99091
- const contentLength = httpRequest.body ? httpRequest.body.length : null;
99091
+ const contentLength = httpRequest2.body ? httpRequest2.body.length : null;
99092
99092
  let contentLengthHeaderValue = null;
99093
- if (httpRequest.body == null && ["POST", "PUT"].includes(httpRequest.method)) {
99093
+ if (httpRequest2.body == null && ["POST", "PUT"].includes(httpRequest2.method)) {
99094
99094
  contentLengthHeaderValue = "0";
99095
99095
  }
99096
99096
  if (contentLength != null) {
99097
99097
  contentLengthHeaderValue = isomorphicEncode(`${contentLength}`);
99098
99098
  }
99099
- if (contentLengthHeaderValue != null && !httpRequest.headersList.contains("content-length", true)) {
99100
- httpRequest.headersList.append("content-length", contentLengthHeaderValue, true);
99099
+ if (contentLengthHeaderValue != null && !httpRequest2.headersList.contains("content-length", true)) {
99100
+ httpRequest2.headersList.append("content-length", contentLengthHeaderValue, true);
99101
99101
  }
99102
- if (contentLength != null && httpRequest.keepalive) {
99102
+ if (contentLength != null && httpRequest2.keepalive) {
99103
99103
  }
99104
- if (webidl.is.URL(httpRequest.referrer)) {
99105
- httpRequest.headersList.append("referer", isomorphicEncode(httpRequest.referrer.href), true);
99104
+ if (webidl.is.URL(httpRequest2.referrer)) {
99105
+ httpRequest2.headersList.append("referer", isomorphicEncode(httpRequest2.referrer.href), true);
99106
99106
  }
99107
- appendRequestOriginHeader(httpRequest);
99108
- appendFetchMetadata(httpRequest);
99109
- if (!httpRequest.headersList.contains("user-agent", true)) {
99110
- httpRequest.headersList.append("user-agent", defaultUserAgent, true);
99107
+ appendRequestOriginHeader(httpRequest2);
99108
+ appendFetchMetadata(httpRequest2);
99109
+ if (!httpRequest2.headersList.contains("user-agent", true)) {
99110
+ httpRequest2.headersList.append("user-agent", defaultUserAgent, true);
99111
99111
  }
99112
- if (httpRequest.cache === "default" && (httpRequest.headersList.contains("if-modified-since", true) || httpRequest.headersList.contains("if-none-match", true) || httpRequest.headersList.contains("if-unmodified-since", true) || httpRequest.headersList.contains("if-match", true) || httpRequest.headersList.contains("if-range", true))) {
99113
- httpRequest.cache = "no-store";
99112
+ if (httpRequest2.cache === "default" && (httpRequest2.headersList.contains("if-modified-since", true) || httpRequest2.headersList.contains("if-none-match", true) || httpRequest2.headersList.contains("if-unmodified-since", true) || httpRequest2.headersList.contains("if-match", true) || httpRequest2.headersList.contains("if-range", true))) {
99113
+ httpRequest2.cache = "no-store";
99114
99114
  }
99115
- if (httpRequest.cache === "no-cache" && !httpRequest.preventNoCacheCacheControlHeaderModification && !httpRequest.headersList.contains("cache-control", true)) {
99116
- httpRequest.headersList.append("cache-control", "max-age=0", true);
99115
+ if (httpRequest2.cache === "no-cache" && !httpRequest2.preventNoCacheCacheControlHeaderModification && !httpRequest2.headersList.contains("cache-control", true)) {
99116
+ httpRequest2.headersList.append("cache-control", "max-age=0", true);
99117
99117
  }
99118
- if (httpRequest.cache === "no-store" || httpRequest.cache === "reload") {
99119
- if (!httpRequest.headersList.contains("pragma", true)) {
99120
- httpRequest.headersList.append("pragma", "no-cache", true);
99118
+ if (httpRequest2.cache === "no-store" || httpRequest2.cache === "reload") {
99119
+ if (!httpRequest2.headersList.contains("pragma", true)) {
99120
+ httpRequest2.headersList.append("pragma", "no-cache", true);
99121
99121
  }
99122
- if (!httpRequest.headersList.contains("cache-control", true)) {
99123
- httpRequest.headersList.append("cache-control", "no-cache", true);
99122
+ if (!httpRequest2.headersList.contains("cache-control", true)) {
99123
+ httpRequest2.headersList.append("cache-control", "no-cache", true);
99124
99124
  }
99125
99125
  }
99126
- if (httpRequest.headersList.contains("range", true)) {
99127
- httpRequest.headersList.append("accept-encoding", "identity", true);
99126
+ if (httpRequest2.headersList.contains("range", true)) {
99127
+ httpRequest2.headersList.append("accept-encoding", "identity", true);
99128
99128
  }
99129
- if (!httpRequest.headersList.contains("accept-encoding", true)) {
99130
- if (urlHasHttpsScheme(requestCurrentURL(httpRequest))) {
99131
- httpRequest.headersList.append("accept-encoding", "br, gzip, deflate, zstd", true);
99129
+ if (!httpRequest2.headersList.contains("accept-encoding", true)) {
99130
+ if (urlHasHttpsScheme(requestCurrentURL(httpRequest2))) {
99131
+ httpRequest2.headersList.append("accept-encoding", "br, gzip, deflate, zstd", true);
99132
99132
  } else {
99133
- httpRequest.headersList.append("accept-encoding", "gzip, deflate", true);
99133
+ httpRequest2.headersList.append("accept-encoding", "gzip, deflate", true);
99134
99134
  }
99135
99135
  }
99136
- httpRequest.headersList.delete("host", true);
99136
+ httpRequest2.headersList.delete("host", true);
99137
99137
  if (includeCredentials) {
99138
- if (!httpRequest.headersList.contains("authorization", true)) {
99138
+ if (!httpRequest2.headersList.contains("authorization", true)) {
99139
99139
  let authorizationValue = null;
99140
- if (hasAuthenticationEntry(httpRequest) && (httpRequest.useURLCredentials === void 0 || !includesCredentials(requestCurrentURL(httpRequest)))) {
99141
- } else if (includesCredentials(requestCurrentURL(httpRequest)) && isAuthenticationFetch) {
99142
- const { username, password } = requestCurrentURL(httpRequest);
99140
+ if (hasAuthenticationEntry(httpRequest2) && (httpRequest2.useURLCredentials === void 0 || !includesCredentials(requestCurrentURL(httpRequest2)))) {
99141
+ } else if (includesCredentials(requestCurrentURL(httpRequest2)) && isAuthenticationFetch) {
99142
+ const { username, password } = requestCurrentURL(httpRequest2);
99143
99143
  authorizationValue = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
99144
99144
  }
99145
99145
  if (authorizationValue !== null) {
99146
- httpRequest.headersList.append("Authorization", authorizationValue, false);
99146
+ httpRequest2.headersList.append("Authorization", authorizationValue, false);
99147
99147
  }
99148
99148
  }
99149
99149
  }
99150
99150
  if (httpCache == null) {
99151
- httpRequest.cache = "no-store";
99151
+ httpRequest2.cache = "no-store";
99152
99152
  }
99153
- if (httpRequest.cache !== "no-store" && httpRequest.cache !== "reload") {
99153
+ if (httpRequest2.cache !== "no-store" && httpRequest2.cache !== "reload") {
99154
99154
  }
99155
99155
  if (response == null) {
99156
- if (httpRequest.cache === "only-if-cached") {
99156
+ if (httpRequest2.cache === "only-if-cached") {
99157
99157
  return makeNetworkError("only if cached");
99158
99158
  }
99159
99159
  const forwardResponse = await httpNetworkFetch(
@@ -99161,7 +99161,7 @@ var require_fetch = __commonJS({
99161
99161
  includeCredentials,
99162
99162
  isNewConnectionFetch
99163
99163
  );
99164
- if (!safeMethodsSet.has(httpRequest.method) && forwardResponse.status >= 200 && forwardResponse.status <= 399) {
99164
+ if (!safeMethodsSet.has(httpRequest2.method) && forwardResponse.status >= 200 && forwardResponse.status <= 399) {
99165
99165
  }
99166
99166
  if (revalidatingFlag && forwardResponse.status === 304) {
99167
99167
  }
@@ -99169,12 +99169,12 @@ var require_fetch = __commonJS({
99169
99169
  response = forwardResponse;
99170
99170
  }
99171
99171
  }
99172
- response.urlList = [...httpRequest.urlList];
99173
- if (httpRequest.headersList.contains("range", true)) {
99172
+ response.urlList = [...httpRequest2.urlList];
99173
+ if (httpRequest2.headersList.contains("range", true)) {
99174
99174
  response.rangeRequested = true;
99175
99175
  }
99176
99176
  response.requestIncludesCredentials = includeCredentials;
99177
- if (response.status === 401 && httpRequest.responseTainting !== "cors" && includeCredentials && (request2.useURLCredentials !== void 0 || isTraversableNavigable(request2.traversableForUserPrompts))) {
99177
+ if (response.status === 401 && httpRequest2.responseTainting !== "cors" && includeCredentials && (request2.useURLCredentials !== void 0 || isTraversableNavigable(request2.traversableForUserPrompts))) {
99178
99178
  if (request2.body != null) {
99179
99179
  if (request2.body.source == null) {
99180
99180
  return response;
@@ -99471,7 +99471,7 @@ var require_fetch = __commonJS({
99471
99471
  }
99472
99472
  }
99473
99473
  }
99474
- this.body = new Readable3({ read: () => controller.resume() });
99474
+ this.body = new Readable4({ read: () => controller.resume() });
99475
99475
  const onError = (err) => this.onResponseError(controller, err);
99476
99476
  resolve2({
99477
99477
  status: status2,
@@ -243362,7 +243362,7 @@ function fingerprint(view) {
243362
243362
  import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
243363
243363
  import { createHmac } from "crypto";
243364
243364
  import { createServer } from "http";
243365
- import { finished, Readable } from "stream";
243365
+ import { finished, Readable as Readable2 } from "stream";
243366
243366
  import { pipeline } from "stream/promises";
243367
243367
  import { homedir as homedir13 } from "os";
243368
243368
  import { join as join17 } from "path";
@@ -263515,6 +263515,11 @@ ${lines.join("\n")}`;
263515
263515
  };
263516
263516
 
263517
263517
  // src/local-guard.ts
263518
+ import { request as httpRequest } from "http";
263519
+ import { request as httpsRequest } from "https";
263520
+ import { lookup as dnsLookup } from "dns/promises";
263521
+ import { isIP } from "net";
263522
+ import { Readable } from "stream";
263518
263523
  import { closeSync as closeSync3, fstatSync as fstatSync2, openSync as openSync3, readSync as readSync2, realpathSync } from "fs";
263519
263524
  import { homedir as homedir11 } from "os";
263520
263525
  import { join as join15 } from "path";
@@ -263592,18 +263597,86 @@ function isBlockedSsrfHost(hostname) {
263592
263597
  }
263593
263598
  return false;
263594
263599
  }
263600
+ var systemResolver = (hostname) => dnsLookup(hostname, { all: true, verbatim: true });
263601
+ async function resolvePublicAddresses(hostname, resolve2 = systemResolver) {
263602
+ const h = hostname.replace(/^\[/, "").replace(/\]$/, "");
263603
+ const family = isIP(h);
263604
+ const addrs = family ? [{ address: h, family }] : await resolve2(h);
263605
+ if (addrs.length === 0) throw new Error(`could not resolve ${hostname}`);
263606
+ const blocked = addrs.find((a) => isBlockedSsrfHost(a.address));
263607
+ if (blocked) {
263608
+ throw new Error(
263609
+ `refusing to fetch ${hostname}: it resolves to a private/loopback/metadata address (${blocked.address})`
263610
+ );
263611
+ }
263612
+ return addrs;
263613
+ }
263614
+ function pinnedLookup(addrs) {
263615
+ return (_hostname, options, callback) => {
263616
+ if (options.all) {
263617
+ callback(null, addrs);
263618
+ } else {
263619
+ callback(null, addrs[0].address, addrs[0].family);
263620
+ }
263621
+ };
263622
+ }
263623
+ var NULL_BODY_STATUSES = /* @__PURE__ */ new Set([101, 103, 204, 205, 304]);
263624
+ function pinnedFetch(url2, addrs, init2 = {}) {
263625
+ const method = (init2.method ?? "GET").toUpperCase();
263626
+ const body = init2.body;
263627
+ if (body != null && typeof body !== "string" && !(body instanceof Uint8Array)) {
263628
+ return Promise.reject(new TypeError("pinnedFetch supports only string or byte bodies"));
263629
+ }
263630
+ const headers = Object.fromEntries(new Headers(init2.headers));
263631
+ const send = url2.protocol === "https:" ? httpsRequest : httpRequest;
263632
+ return new Promise((resolve2, reject) => {
263633
+ const req = send(
263634
+ url2,
263635
+ { method, headers, lookup: pinnedLookup(addrs), signal: init2.signal ?? void 0 },
263636
+ (res) => {
263637
+ const status2 = res.statusCode ?? 0;
263638
+ if (status2 < 200 || status2 > 599) {
263639
+ res.destroy();
263640
+ reject(new Error(`unexpected HTTP status ${status2} from ${url2.host}`));
263641
+ return;
263642
+ }
263643
+ const resHeaders = new Headers();
263644
+ for (let i = 0; i + 1 < res.rawHeaders.length; i += 2) {
263645
+ resHeaders.append(res.rawHeaders[i], res.rawHeaders[i + 1]);
263646
+ }
263647
+ const noBody = method === "HEAD" || NULL_BODY_STATUSES.has(status2);
263648
+ if (noBody) res.resume();
263649
+ resolve2(
263650
+ new Response(noBody ? null : Readable.toWeb(res), {
263651
+ status: status2,
263652
+ statusText: res.statusMessage,
263653
+ headers: resHeaders
263654
+ })
263655
+ );
263656
+ }
263657
+ );
263658
+ req.on("error", reject);
263659
+ req.end(body ?? void 0);
263660
+ });
263661
+ }
263595
263662
  async function ssrfSafeFetch(url2, init2 = {}, maxHops = 5) {
263596
- const { allowPrivate, ...fetchInit } = init2;
263663
+ const { allowPrivate, viaUpstreamProxy, resolve: resolve2, ...fetchInit } = init2;
263597
263664
  let current = url2;
263598
263665
  for (let hop = 0; hop <= maxHops; hop++) {
263599
263666
  const u = new URL(current);
263600
263667
  if (u.protocol !== "http:" && u.protocol !== "https:") {
263601
263668
  throw new Error(`refusing to fetch non-http(s) URL (${u.protocol})`);
263602
263669
  }
263603
- if (!allowPrivate && isBlockedSsrfHost(u.hostname)) {
263604
- throw new Error(`refusing to fetch a private/loopback/metadata address: ${u.hostname}`);
263670
+ let res;
263671
+ if (allowPrivate) {
263672
+ res = await fetch(current, { ...fetchInit, redirect: "manual" });
263673
+ } else {
263674
+ if (isBlockedSsrfHost(u.hostname)) {
263675
+ throw new Error(`refusing to fetch a private/loopback/metadata address: ${u.hostname}`);
263676
+ }
263677
+ const addrs = await resolvePublicAddresses(u.hostname, resolve2);
263678
+ res = viaUpstreamProxy ? await fetch(current, { ...fetchInit, redirect: "manual" }) : await pinnedFetch(u, addrs, fetchInit);
263605
263679
  }
263606
- const res = await fetch(current, { ...fetchInit, redirect: "manual" });
263607
263680
  if (res.status >= 300 && res.status < 400) {
263608
263681
  const loc = res.headers.get("location");
263609
263682
  if (!loc) return res;
@@ -265620,7 +265693,7 @@ async function proxyPaidApiRequest(req, res, apiBase, payFetch, getActualPayment
265620
265693
  if (accountPassthrough) {
265621
265694
  if (upstream.body) {
265622
265695
  await pipeline(
265623
- Readable.fromWeb(upstream.body),
265696
+ Readable2.fromWeb(upstream.body),
265624
265697
  res,
265625
265698
  { signal: clientAbort.signal }
265626
265699
  );
@@ -266556,7 +266629,8 @@ async function startProxy(options) {
266556
266629
  } else if (val.startsWith("https://") || val.startsWith("http://")) {
266557
266630
  const imgResp = await ssrfSafeFetch(val, {
266558
266631
  signal: clientAbort.signal,
266559
- allowPrivate: process.env.CLAWROUTER_ALLOW_PRIVATE_FETCH === "1"
266632
+ allowPrivate: process.env.CLAWROUTER_ALLOW_PRIVATE_FETCH === "1",
266633
+ viaUpstreamProxy: Boolean(upstreamProxy)
266560
266634
  });
266561
266635
  if (!imgResp.ok)
266562
266636
  throw new Error(`Failed to download ${field} from ${val}: HTTP ${imgResp.status}`);
@@ -274237,7 +274311,7 @@ import { EventEmitter } from "events";
274237
274311
 
274238
274312
  // node_modules/axios/lib/helpers/formDataToStream.js
274239
274313
  import util2 from "util";
274240
- import { Readable as Readable2 } from "stream";
274314
+ import { Readable as Readable3 } from "stream";
274241
274315
 
274242
274316
  // node_modules/axios/lib/helpers/readBlob.js
274243
274317
  var { asyncIterator } = Symbol;
@@ -274327,7 +274401,7 @@ var formDataToStream = (form, headersHandler, options) => {
274327
274401
  computedHeaders["Content-Length"] = contentLength;
274328
274402
  }
274329
274403
  headersHandler && headersHandler(computedHeaders);
274330
- return Readable2.from(
274404
+ return Readable3.from(
274331
274405
  (async function* () {
274332
274406
  for (const part of parts) {
274333
274407
  yield boundaryBytes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blockrun/clawrouter",
3
- "version": "0.12.298",
3
+ "version": "0.12.299",
4
4
  "description": "Smart LLM router — save 84% on inference costs. 90 models (7 free). Pay by credit card (API key) or USDC (x402 wallet).",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",