@asterxsk/kiln 0.1.0 → 0.2.0

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 (203) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +170 -170
  3. package/agent/AGENTS.md +67 -67
  4. package/agent/README.md +5 -5
  5. package/agent/extensions/AGENTS.md +68 -68
  6. package/agent/extensions/ask-user/index.ts +418 -418
  7. package/agent/extensions/ask-user/package-lock.json +769 -769
  8. package/agent/extensions/ask-user/package.json +19 -19
  9. package/agent/extensions/ask-user/prompt.ts +45 -45
  10. package/agent/extensions/ask-user/tsconfig.json +7 -7
  11. package/agent/extensions/background-terminals/docs/implementation-guide.md +942 -942
  12. package/agent/extensions/background-terminals/index.ts +627 -627
  13. package/agent/extensions/background-terminals/manager.test.ts +735 -735
  14. package/agent/extensions/background-terminals/output.test.ts +109 -109
  15. package/agent/extensions/background-terminals/package-lock.json +769 -769
  16. package/agent/extensions/background-terminals/package.json +17 -17
  17. package/agent/extensions/background-terminals/prompt.test.ts +125 -125
  18. package/agent/extensions/background-terminals/ps.test.ts +82 -82
  19. package/agent/extensions/background-terminals/result-delivery.test.ts +44 -44
  20. package/agent/extensions/background-terminals/src/domain.ts +87 -87
  21. package/agent/extensions/background-terminals/src/manager.ts +907 -907
  22. package/agent/extensions/background-terminals/src/output.ts +84 -84
  23. package/agent/extensions/background-terminals/src/prompt.ts +142 -142
  24. package/agent/extensions/background-terminals/src/result-delivery.ts +27 -27
  25. package/agent/extensions/background-terminals/src/runtime.ts +36 -36
  26. package/agent/extensions/background-terminals/src/ui/output-view.ts +79 -79
  27. package/agent/extensions/background-terminals/src/ui/ps.ts +621 -621
  28. package/agent/extensions/background-terminals/tsconfig.json +7 -7
  29. package/agent/extensions/file-search/index.spec.ts +443 -443
  30. package/agent/extensions/file-search/index.ts +459 -459
  31. package/agent/extensions/file-search/package-lock.json +2253 -2253
  32. package/agent/extensions/file-search/package.json +23 -23
  33. package/agent/extensions/file-search/src/args.ts +122 -122
  34. package/agent/extensions/file-search/src/binaries.ts +422 -422
  35. package/agent/extensions/file-search/src/output.ts +126 -126
  36. package/agent/extensions/file-search/src/process.ts +146 -146
  37. package/agent/extensions/file-search/src/prompt.ts +52 -52
  38. package/agent/extensions/file-search/tsconfig.json +7 -7
  39. package/agent/extensions/modelconf/PLAN.md +915 -915
  40. package/agent/extensions/modelconf/index.ts +296 -296
  41. package/agent/extensions/modelconf/src/ui/ModelConfView.ts +1101 -1101
  42. package/agent/extensions/pi-web-access/CHANGELOG.md +690 -690
  43. package/agent/extensions/pi-web-access/LICENSE +21 -21
  44. package/agent/extensions/pi-web-access/README.md +470 -470
  45. package/agent/extensions/pi-web-access/SECURITY.md +5 -5
  46. package/agent/extensions/pi-web-access/activity.ts +101 -101
  47. package/agent/extensions/pi-web-access/auth-fetch.ts +148 -148
  48. package/agent/extensions/pi-web-access/brightdata-unlocker.ts +272 -272
  49. package/agent/extensions/pi-web-access/chrome-cookies.ts +669 -669
  50. package/agent/extensions/pi-web-access/content-find.ts +139 -139
  51. package/agent/extensions/pi-web-access/credential-source.ts +191 -191
  52. package/agent/extensions/pi-web-access/data-uri-sanitize.ts +406 -406
  53. package/agent/extensions/pi-web-access/datalab-pdf-extract.ts +568 -568
  54. package/agent/extensions/pi-web-access/declared-web-links.ts +173 -173
  55. package/agent/extensions/pi-web-access/evidence/CONTRACT-EVIDENCE.md +496 -496
  56. package/agent/extensions/pi-web-access/evidence/contract-probe.mjs +140 -140
  57. package/agent/extensions/pi-web-access/exa.ts +526 -526
  58. package/agent/extensions/pi-web-access/extract.ts +1196 -1196
  59. package/agent/extensions/pi-web-access/feature-config.ts +29 -29
  60. package/agent/extensions/pi-web-access/fetch-params.ts +111 -111
  61. package/agent/extensions/pi-web-access/gemini-adc.ts +298 -298
  62. package/agent/extensions/pi-web-access/gemini-api.ts +353 -353
  63. package/agent/extensions/pi-web-access/gemini-pdf-extract.ts +108 -108
  64. package/agent/extensions/pi-web-access/gemini-url-context.ts +128 -128
  65. package/agent/extensions/pi-web-access/gemini-web-config.ts +101 -101
  66. package/agent/extensions/pi-web-access/gemini-web.ts +487 -487
  67. package/agent/extensions/pi-web-access/github-api.ts +197 -197
  68. package/agent/extensions/pi-web-access/github-extract.ts +746 -746
  69. package/agent/extensions/pi-web-access/github-issue-pr.ts +700 -700
  70. package/agent/extensions/pi-web-access/index.ts +1737 -1737
  71. package/agent/extensions/pi-web-access/package-lock.json +5808 -5808
  72. package/agent/extensions/pi-web-access/package.json +64 -64
  73. package/agent/extensions/pi-web-access/page-query.ts +96 -96
  74. package/agent/extensions/pi-web-access/pdf-extract.ts +409 -409
  75. package/agent/extensions/pi-web-access/promise-try.d.ts +7 -7
  76. package/agent/extensions/pi-web-access/query-rewrite.ts +51 -51
  77. package/agent/extensions/pi-web-access/render-search-error.ts +170 -170
  78. package/agent/extensions/pi-web-access/rsc-extract.ts +338 -338
  79. package/agent/extensions/pi-web-access/source-check.ts +282 -282
  80. package/agent/extensions/pi-web-access/ssrf-protection.ts +526 -526
  81. package/agent/extensions/pi-web-access/storage.ts +521 -521
  82. package/agent/extensions/pi-web-access/summary-model-scope.ts +125 -125
  83. package/agent/extensions/pi-web-access/test/auth-fetch.test.mjs +208 -208
  84. package/agent/extensions/pi-web-access/test/brightdata-unlocker.test.mjs +840 -840
  85. package/agent/extensions/pi-web-access/test/chrome-cookie-extraction.test.mjs +441 -441
  86. package/agent/extensions/pi-web-access/test/config-path.test.mjs +283 -283
  87. package/agent/extensions/pi-web-access/test/content-find.test.mjs +25 -25
  88. package/agent/extensions/pi-web-access/test/credential-source.test.mjs +118 -118
  89. package/agent/extensions/pi-web-access/test/data-uri-sanitize.test.mjs +210 -210
  90. package/agent/extensions/pi-web-access/test/datalab-pdf-extract.test.mjs +552 -552
  91. package/agent/extensions/pi-web-access/test/declared-web-links.test.mjs +212 -212
  92. package/agent/extensions/pi-web-access/test/fetch-answer-storage.test.mjs +40 -40
  93. package/agent/extensions/pi-web-access/test/fetch-cache-storage.test.mjs +334 -334
  94. package/agent/extensions/pi-web-access/test/fetch-content-domain-policy.test.mjs +95 -95
  95. package/agent/extensions/pi-web-access/test/fetch-modes.test.mjs +53 -53
  96. package/agent/extensions/pi-web-access/test/fetch-not-found-guidance.test.mjs +92 -92
  97. package/agent/extensions/pi-web-access/test/fetch-params.test.mjs +86 -86
  98. package/agent/extensions/pi-web-access/test/fetch-render-call.test.mjs +34 -34
  99. package/agent/extensions/pi-web-access/test/fetch-routing.test.mjs +173 -173
  100. package/agent/extensions/pi-web-access/test/gemini-adc-auth.test.mjs +257 -257
  101. package/agent/extensions/pi-web-access/test/gemini-api-transport.test.mjs +170 -170
  102. package/agent/extensions/pi-web-access/test/gemini-pdf-extract.test.mjs +133 -133
  103. package/agent/extensions/pi-web-access/test/gemini-web-cookie-opt-in.test.mjs +178 -178
  104. package/agent/extensions/pi-web-access/test/gemini-web-header-overflow.test.mjs +148 -148
  105. package/agent/extensions/pi-web-access/test/get-search-content.test.mjs +223 -223
  106. package/agent/extensions/pi-web-access/test/github-extract.test.mjs +378 -378
  107. package/agent/extensions/pi-web-access/test/github-issue-pr.test.mjs +565 -565
  108. package/agent/extensions/pi-web-access/test/inline-content-config.test.mjs +99 -99
  109. package/agent/extensions/pi-web-access/test/lazy-extract-load.test.mjs +118 -118
  110. package/agent/extensions/pi-web-access/test/local-video-oversize.test.mjs +52 -52
  111. package/agent/extensions/pi-web-access/test/package-typebox-dependency.test.mjs +50 -50
  112. package/agent/extensions/pi-web-access/test/page-query.test.mjs +51 -51
  113. package/agent/extensions/pi-web-access/test/pdf-config.test.mjs +140 -140
  114. package/agent/extensions/pi-web-access/test/pdf-extract.test.mjs +500 -500
  115. package/agent/extensions/pi-web-access/test/proxy-transport.test.mjs +286 -286
  116. package/agent/extensions/pi-web-access/test/query-rewrite.test.mjs +52 -52
  117. package/agent/extensions/pi-web-access/test/rsc-fallback.test.mjs +102 -102
  118. package/agent/extensions/pi-web-access/test/search-error-render.test.mjs +152 -152
  119. package/agent/extensions/pi-web-access/test/search-providers.test.mjs +274 -274
  120. package/agent/extensions/pi-web-access/test/source-check.test.mjs +179 -179
  121. package/agent/extensions/pi-web-access/test/ssrf-allow-ranges-config.test.mjs +205 -205
  122. package/agent/extensions/pi-web-access/test/ssrf-protection.test.mjs +456 -456
  123. package/agent/extensions/pi-web-access/test/tool-registration-config.test.mjs +182 -182
  124. package/agent/extensions/pi-web-access/test/youtube-extract-errors.test.mjs +64 -64
  125. package/agent/extensions/pi-web-access/tsconfig.json +11 -11
  126. package/agent/extensions/pi-web-access/utils.ts +451 -451
  127. package/agent/extensions/pi-web-access/video-extract.ts +392 -392
  128. package/agent/extensions/pi-web-access/youtube-extract.ts +328 -328
  129. package/agent/extensions/shared/activity-status.ts +31 -31
  130. package/agent/extensions/shared/child-session.test.ts +270 -270
  131. package/agent/extensions/shared/child-session.ts +148 -148
  132. package/agent/extensions/shared/context-utilization.test.ts +48 -48
  133. package/agent/extensions/shared/context-utilization.ts +47 -47
  134. package/agent/extensions/shared/dashboard-state.ts +99 -99
  135. package/agent/extensions/shared/tool-call-timeout.test.ts +117 -117
  136. package/agent/extensions/shared/tool-call-timeout.ts +104 -104
  137. package/agent/extensions/subagents/by-the-way.test.ts +29 -29
  138. package/agent/extensions/subagents/claude.test.ts +119 -119
  139. package/agent/extensions/subagents/codex.test.ts +102 -102
  140. package/agent/extensions/subagents/context-usage.test.ts +107 -107
  141. package/agent/extensions/subagents/docs/design-plan.md +568 -568
  142. package/agent/extensions/subagents/docs/effect-v4-extension-guide.md +354 -354
  143. package/agent/extensions/subagents/docs/effect-v4-notes.md +571 -571
  144. package/agent/extensions/subagents/index.ts +779 -779
  145. package/agent/extensions/subagents/manager.test.ts +276 -276
  146. package/agent/extensions/subagents/package-lock.json +2244 -2244
  147. package/agent/extensions/subagents/package.json +19 -19
  148. package/agent/extensions/subagents/result-delivery.test.ts +27 -27
  149. package/agent/extensions/subagents/src/backend.ts +73 -73
  150. package/agent/extensions/subagents/src/backends/claude.ts +701 -701
  151. package/agent/extensions/subagents/src/backends/codex.ts +1060 -1060
  152. package/agent/extensions/subagents/src/backends/pi.ts +575 -575
  153. package/agent/extensions/subagents/src/backends/stub.ts +300 -300
  154. package/agent/extensions/subagents/src/by-the-way.ts +21 -21
  155. package/agent/extensions/subagents/src/domain.ts +253 -253
  156. package/agent/extensions/subagents/src/format.ts +74 -74
  157. package/agent/extensions/subagents/src/manager.ts +736 -736
  158. package/agent/extensions/subagents/src/prompt.ts +92 -92
  159. package/agent/extensions/subagents/src/result-delivery.ts +20 -20
  160. package/agent/extensions/subagents/src/runtime.ts +53 -53
  161. package/agent/extensions/subagents/src/ui/takeover.ts +583 -583
  162. package/agent/extensions/subagents/src/ui/transcript.ts +201 -201
  163. package/agent/extensions/subagents/takeover.test.ts +29 -29
  164. package/agent/extensions/subagents/tsconfig.json +7 -7
  165. package/agent/extensions/todo/AGENTS.md +38 -38
  166. package/agent/extensions/todo/LICENSE +21 -21
  167. package/agent/extensions/todo/config.ts +55 -55
  168. package/agent/extensions/todo/index.ts +151 -151
  169. package/agent/extensions/todo/locales/de.json +17 -17
  170. package/agent/extensions/todo/locales/en.json +15 -15
  171. package/agent/extensions/todo/locales/es.json +17 -17
  172. package/agent/extensions/todo/locales/fr.json +17 -17
  173. package/agent/extensions/todo/locales/pt-BR.json +17 -17
  174. package/agent/extensions/todo/locales/pt.json +17 -17
  175. package/agent/extensions/todo/locales/ru.json +17 -17
  176. package/agent/extensions/todo/locales/uk.json +17 -17
  177. package/agent/extensions/todo/locales/zh.json +17 -17
  178. package/agent/extensions/todo/package-lock.json +3358 -3358
  179. package/agent/extensions/todo/package.json +67 -67
  180. package/agent/extensions/todo/state/i18n-bridge.ts +64 -64
  181. package/agent/extensions/todo/state/invariants.ts +20 -20
  182. package/agent/extensions/todo/state/replay.ts +38 -38
  183. package/agent/extensions/todo/state/selectors.ts +107 -107
  184. package/agent/extensions/todo/state/state-reducer.ts +326 -326
  185. package/agent/extensions/todo/state/state.ts +18 -18
  186. package/agent/extensions/todo/state/store.ts +82 -82
  187. package/agent/extensions/todo/state/task-graph.ts +57 -57
  188. package/agent/extensions/todo/todo-overlay.ts +200 -200
  189. package/agent/extensions/todo/todo.ts +155 -155
  190. package/agent/extensions/todo/tool/response-envelope.ts +109 -109
  191. package/agent/extensions/todo/tool/types.ts +206 -206
  192. package/agent/extensions/todo/view/format.ts +177 -177
  193. package/agent/install.ps1 +637 -527
  194. package/agent/install.sh +620 -511
  195. package/agent/keybindings.json +7 -7
  196. package/bin/kiln.js +124 -11
  197. package/package.json +8 -2
  198. package/agent/extensions/taste/index.ts +0 -443
  199. package/agent/extensions/taste/install.ps1 +0 -23
  200. package/agent/extensions/taste/install.sh +0 -21
  201. /package/agent/extensions/{status line → statusline}/index.ts +0 -0
  202. /package/agent/extensions/{status line → statusline}/install.ps1 +0 -0
  203. /package/agent/extensions/{status line → statusline}/install.sh +0 -0
@@ -1,456 +1,456 @@
1
- import assert from "node:assert/strict";
2
- import { test } from "node:test";
3
-
4
- import { fetchRemoteUrl, validateRemoteUrl } from "../ssrf-protection.ts";
5
- import { runWithProxy } from "../utils.ts";
6
-
7
- const publicLookup = async () => [{ address: "93.184.216.34", family: 4 }];
8
-
9
- async function rejectsInternal(url) {
10
- await assert.rejects(
11
- validateRemoteUrl(url, { lookup: publicLookup }),
12
- /internal|Blocked/,
13
- `${url} should be blocked`,
14
- );
15
- }
16
-
17
- test("validateRemoteUrl blocks localhost, loopback, link-local, private, and metadata targets", async () => {
18
- await rejectsInternal("http://localhost/");
19
- await rejectsInternal("http://127.0.0.1/");
20
- await rejectsInternal("http://10.0.0.1/");
21
- await rejectsInternal("http://172.16.0.1/");
22
- await rejectsInternal("http://192.168.1.1/");
23
- await rejectsInternal("http://169.254.169.254/latest/meta-data/");
24
- await rejectsInternal("http://0.0.0.0/");
25
- await rejectsInternal("http://[::1]/");
26
- await rejectsInternal("http://[fe80::1]/");
27
- await rejectsInternal("http://[fd00::1]/");
28
- await rejectsInternal("http://[::ffff:127.0.0.1]/");
29
- });
30
-
31
- test("validateRemoteUrl blocks encoded and alternate loopback IPv4 forms", async () => {
32
- await rejectsInternal("http://2130706433/");
33
- await rejectsInternal("http://0177.0.0.1/");
34
- await rejectsInternal("http://0x7f.0.0.1/");
35
- await rejectsInternal("http://127.1/");
36
- });
37
-
38
- test("validateRemoteUrl blocks hostnames that resolve to private addresses", async () => {
39
- await assert.rejects(
40
- validateRemoteUrl("https://example.test/", {
41
- lookup: async () => [{ address: "192.168.0.2", family: 4 }],
42
- }),
43
- /Blocked internal address for example\.test: 192\.168\.0\.2/,
44
- );
45
-
46
- await assert.rejects(
47
- validateRemoteUrl("https://example.test/", {
48
- lookup: async () => [{ address: "93.184.216.34", family: 4 }, { address: "fd00::1", family: 6 }],
49
- }),
50
- /Blocked internal address for example\.test: fd00::1/,
51
- );
52
- });
53
-
54
- test("loopback opt-in does not allow arbitrary hostnames that resolve to loopback", async () => {
55
- await assert.rejects(
56
- validateRemoteUrl("https://example.test/", {
57
- allowLoopback: true,
58
- lookup: async () => [{ address: "127.0.0.1", family: 4 }],
59
- }),
60
- /Blocked internal address for example\.test: 127\.0\.0\.1/,
61
- );
62
- });
63
-
64
- test("validateRemoteUrl permits public HTTP and HTTPS targets", async () => {
65
- assert.equal((await validateRemoteUrl("https://example.com/path", { lookup: publicLookup })).hostname, "example.com");
66
- assert.equal((await validateRemoteUrl("http://93.184.216.34/")).hostname, "93.184.216.34");
67
- assert.equal((await validateRemoteUrl("https://[2606:2800:220:1:248:1893:25c8:1946]/")).hostname, "[2606:2800:220:1:248:1893:25c8:1946]");
68
- });
69
-
70
- test("domain policy allows exact and subdomain matches, and is off by default", async () => {
71
- assert.equal((await validateRemoteUrl("https://example.com/", { lookup: publicLookup })).hostname, "example.com");
72
- assert.equal((await validateRemoteUrl("https://www.example.com/", {
73
- lookup: publicLookup,
74
- domainPolicy: { allow: ["example.com"], deny: [] },
75
- })).hostname, "www.example.com");
76
- await assert.rejects(
77
- validateRemoteUrl("https://other.test/", {
78
- lookup: publicLookup,
79
- domainPolicy: { allow: ["example.com"], deny: [] },
80
- }),
81
- /Hostname not allowed by fetch_content domain policy/,
82
- );
83
- });
84
-
85
- test("domain policy deny wins over allow", async () => {
86
- await assert.rejects(
87
- validateRemoteUrl("https://private.example.com/", {
88
- lookup: publicLookup,
89
- domainPolicy: { allow: ["example.com"], deny: ["private.example.com"] },
90
- }),
91
- /Blocked hostname by fetch_content domain policy/,
92
- );
93
- });
94
-
95
- test("domain policy validates redirect targets before following", async () => {
96
- const requested = [];
97
- const fetchImpl = async (url) => {
98
- requested.push(url.toString());
99
- return new Response("", { status: 302, headers: { location: "https://denied.example/next" } });
100
- };
101
-
102
- await assert.rejects(
103
- fetchRemoteUrl("https://allowed.example/", {}, {
104
- lookup: publicLookup,
105
- fetch: fetchImpl,
106
- domainPolicy: { allow: ["allowed.example"], deny: ["denied.example"] },
107
- }),
108
- /Blocked hostname by fetch_content domain policy/,
109
- );
110
- assert.deepEqual(requested, ["https://allowed.example/"]);
111
- });
112
-
113
- test("domain policy never relaxes SSRF protection", async () => {
114
- await assert.rejects(
115
- validateRemoteUrl("http://127.0.0.1/", {
116
- domainPolicy: { allow: ["127.0.0.1"], deny: [] },
117
- }),
118
- /Blocked internal address/,
119
- );
120
- await assert.rejects(
121
- validateRemoteUrl("https://internal.example/", {
122
- lookup: async () => [{ address: "10.0.0.5", family: 4 }],
123
- domainPolicy: { allow: ["example"], deny: [] },
124
- }),
125
- /Blocked internal address/,
126
- );
127
- });
128
-
129
- test("fetchRemoteUrl validates redirect targets before following", async () => {
130
- const requested = [];
131
- const fetchImpl = async (url) => {
132
- requested.push(url.toString());
133
- return new Response("", {
134
- status: 302,
135
- headers: { location: "http://127.0.0.1/admin" },
136
- });
137
- };
138
-
139
- await assert.rejects(
140
- fetchRemoteUrl("https://example.com/", {}, { lookup: publicLookup, fetch: fetchImpl }),
141
- /Blocked internal address/,
142
- );
143
- assert.deepEqual(requested, ["https://example.com/"]);
144
- });
145
-
146
- test("fetchRemoteUrl follows validated public redirects manually", async () => {
147
- const requested = [];
148
- const fetchImpl = async (url) => {
149
- requested.push(url.toString());
150
- if (requested.length === 1) {
151
- return new Response("", {
152
- status: 301,
153
- headers: { location: "/next" },
154
- });
155
- }
156
- return new Response("ok", { status: 200 });
157
- };
158
-
159
- const response = await fetchRemoteUrl("https://example.com/start", {}, { lookup: publicLookup, fetch: fetchImpl });
160
- assert.equal(response.status, 200);
161
- assert.equal(await response.text(), "ok");
162
- assert.deepEqual(requested, ["https://example.com/start", "https://example.com/next"]);
163
- });
164
-
165
- test("fake-IP block errors point to the allowRanges opt-in", async () => {
166
- const fakeIpLookup = async () => [{ address: "198.18.0.56", family: 4 }];
167
-
168
- await assert.rejects(
169
- validateRemoteUrl("https://example.test/", { lookup: fakeIpLookup }),
170
- /Blocked internal address for example\.test: 198\.18\.0\.56\..*TUN\/fake-IP proxies.*ssrf\.allowRanges.*198\.18\.0\.0\/15/,
171
- );
172
- });
173
-
174
- test("allowRanges exempts a synthetic fake-IP range (e.g. 198.18.0.0/15)", async () => {
175
- const fakeIpLookup = async () => [{ address: "198.18.0.56", family: 4 }];
176
-
177
- // Without the exemption this is blocked (the fake-IP proxy case).
178
- await assert.rejects(
179
- validateRemoteUrl("https://example.test/", { lookup: fakeIpLookup }),
180
- /Blocked internal address for example\.test: 198\.18\.0\.56/,
181
- );
182
-
183
- // With allowRanges it passes.
184
- const url = await validateRemoteUrl("https://example.test/", {
185
- lookup: fakeIpLookup,
186
- allowRanges: ["198.18.0.0/15"],
187
- });
188
- assert.equal(url.hostname, "example.test");
189
-
190
- // A bare literal IP in the range is also exempted.
191
- assert.equal((await validateRemoteUrl("http://198.18.0.99/", { allowRanges: ["198.18.0.0/15"] })).hostname, "198.18.0.99");
192
- });
193
-
194
- test("allowRanges works for IPv6 ranges", async () => {
195
- const fakeIp6Lookup = async () => [{ address: "fd00::1", family: 6 }];
196
-
197
- await assert.rejects(
198
- validateRemoteUrl("https://example.test/", { lookup: fakeIp6Lookup }),
199
- /Blocked internal address for example\.test: fd00::1/,
200
- );
201
-
202
- const url = await validateRemoteUrl("https://example.test/", {
203
- lookup: fakeIp6Lookup,
204
- allowRanges: ["fd00::/8"],
205
- });
206
- assert.equal(url.hostname, "example.test");
207
- });
208
-
209
- test("allowRanges does not relax protection outside the listed range", async () => {
210
- // 10.0.0.1 is private; an unrelated exemption must NOT cover it.
211
- await assert.rejects(
212
- validateRemoteUrl("https://example.test/", {
213
- lookup: async () => [{ address: "10.0.0.1", family: 4 }],
214
- allowRanges: ["198.18.0.0/15"],
215
- }),
216
- /Blocked internal address for example\.test: 10\.0\.0\.1/,
217
- );
218
-
219
- // Exact /32 boundary: allowed in-range, blocked just outside.
220
- assert.equal((await validateRemoteUrl("http://198.18.0.0/", { allowRanges: ["198.18.0.0/31"] })).hostname, "198.18.0.0");
221
- assert.equal((await validateRemoteUrl("http://198.18.0.1/", { allowRanges: ["198.18.0.0/31"] })).hostname, "198.18.0.1");
222
- await assert.rejects(
223
- validateRemoteUrl("http://198.18.0.2/", { allowRanges: ["198.18.0.0/31"] }),
224
- /Blocked internal address/,
225
- );
226
- });
227
-
228
- test("allowRanges accepts a bare host (no prefix) and treats it as /32", async () => {
229
- assert.equal((await validateRemoteUrl("http://198.18.1.2/", { allowRanges: ["198.18.1.2"] })).hostname, "198.18.1.2");
230
- });
231
-
232
- test("allowRanges rejects an empty or non-numeric CIDR prefix instead of treating it as /0", async () => {
233
- // Regression: a trailing slash with no prefix (e.g. "198.18.0.0/") must NOT
234
- // become /0, which would exempt every address from the SSRF guard.
235
- for (const bad of ["198.18.0.0/", "198.18.0.0/ ", "fd00::/", "10.0.0.0/abc", "10.0.0.0/ 8"]) {
236
- await assert.rejects(
237
- validateRemoteUrl("http://198.18.0.5/", { allowRanges: [bad] }),
238
- /Invalid CIDR notation in ssrf\.allowRanges/,
239
- `${bad} should be rejected`,
240
- );
241
- }
242
-
243
- // The dangerous outcome is prevented: a metadata/private IP is not exempted
244
- // by a malformed "/" entry; the misconfiguration surfaces as an error.
245
- await assert.rejects(
246
- validateRemoteUrl("http://169.254.169.254/", { allowRanges: ["198.18.0.0/"] }),
247
- /Invalid CIDR notation in ssrf\.allowRanges/,
248
- );
249
- });
250
-
251
- test("allowRanges rejects all-address /0 CIDRs", async () => {
252
- await assert.rejects(
253
- validateRemoteUrl("http://169.254.169.254/", { allowRanges: ["0.0.0.0/0"] }),
254
- /Invalid CIDR notation in ssrf\.allowRanges/,
255
- );
256
- await assert.rejects(
257
- validateRemoteUrl("http://[fd00::1]/", { allowRanges: ["::/0"] }),
258
- /Invalid CIDR notation in ssrf\.allowRanges/,
259
- );
260
- });
261
-
262
- test("invalid allowRanges entries throw a descriptive error", async () => {
263
- for (const bad of ["not-an-ip", "198.18.0.0/33", "198.18.0.0/-1", "999.0.0.0/8", "fd00::/129"]) {
264
- await assert.rejects(
265
- validateRemoteUrl("http://198.18.0.5/", { allowRanges: [bad] }),
266
- new RegExp(`Invalid CIDR notation in ssrf\.allowRanges: "${bad.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\\$&")}"`),
267
- );
268
- }
269
- await assert.rejects(
270
- validateRemoteUrl("http://198.18.0.5/", { allowRanges: "198.18.0.0/15" }),
271
- /ssrf\.allowRanges must be an array/,
272
- );
273
- });
274
-
275
- test("allowRanges flows through fetchRemoteUrl and its redirect targets", async () => {
276
- const requested = [];
277
- const fetchImpl = async (url) => {
278
- requested.push(url.toString());
279
- if (requested.length === 1) {
280
- return new Response("", { status: 302, headers: { location: "http://198.18.0.99/admin" } });
281
- }
282
- return new Response("ok", { status: 200 });
283
- };
284
-
285
- const response = await fetchRemoteUrl(
286
- "https://example.com/",
287
- {},
288
- { lookup: publicLookup, fetch: fetchImpl, allowRanges: ["198.18.0.0/15"] },
289
- );
290
- assert.equal(response.status, 200);
291
- assert.deepEqual(requested, ["https://example.com/", "http://198.18.0.99/admin"]);
292
- });
293
-
294
- test("trustEnvProxy skips hostname DNS only for a configured proxy", async () => {
295
- const previous = {
296
- HTTP_PROXY: process.env.HTTP_PROXY,
297
- http_proxy: process.env.http_proxy,
298
- HTTPS_PROXY: process.env.HTTPS_PROXY,
299
- https_proxy: process.env.https_proxy,
300
- ALL_PROXY: process.env.ALL_PROXY,
301
- all_proxy: process.env.all_proxy,
302
- NO_PROXY: process.env.NO_PROXY,
303
- no_proxy: process.env.no_proxy,
304
- };
305
- try {
306
- for (const key of Object.keys(previous)) delete process.env[key];
307
- process.env.HTTPS_PROXY = "http://proxy.example.test:8080";
308
- let lookups = 0;
309
- const lookup = async () => {
310
- lookups++;
311
- throw new Error("DNS should not run for proxied host");
312
- };
313
-
314
- await validateRemoteUrl("https://public.example.test/", { trustEnvProxy: true, lookup });
315
- assert.equal(lookups, 0);
316
-
317
- await assert.rejects(
318
- validateRemoteUrl("https://127.0.0.1/", { trustEnvProxy: true, lookup }),
319
- /Blocked internal address/,
320
- );
321
- await assert.rejects(
322
- validateRemoteUrl("https://localhost/", { trustEnvProxy: true, lookup }),
323
- /Blocked internal hostname/,
324
- );
325
- } finally {
326
- for (const [key, value] of Object.entries(previous)) {
327
- if (value === undefined) delete process.env[key];
328
- else process.env[key] = value;
329
- }
330
- }
331
- });
332
-
333
- test("trustEnvProxy still validates DNS when a curl proxy is active", async () => {
334
- const previous = {
335
- HTTPS_PROXY: process.env.HTTPS_PROXY,
336
- NO_PROXY: process.env.NO_PROXY,
337
- no_proxy: process.env.no_proxy,
338
- };
339
- try {
340
- process.env.HTTPS_PROXY = "http://env-proxy.example.test:8080";
341
- delete process.env.NO_PROXY;
342
- delete process.env.no_proxy;
343
- let lookups = 0;
344
- await assert.rejects(
345
- runWithProxy("http://curl-proxy.example.test:8080", () => validateRemoteUrl("https://public.example.test/", {
346
- trustEnvProxy: true,
347
- lookup: async () => {
348
- lookups++;
349
- return [{ address: "10.0.0.10", family: 4 }];
350
- },
351
- })),
352
- /Blocked internal address/,
353
- );
354
- assert.equal(lookups, 1);
355
- } finally {
356
- for (const [key, value] of Object.entries(previous)) {
357
- if (value === undefined) delete process.env[key];
358
- else process.env[key] = value;
359
- }
360
- }
361
- });
362
-
363
- test("trustEnvProxy still validates DNS when empty proxy forces direct access", async () => {
364
- const previous = {
365
- HTTPS_PROXY: process.env.HTTPS_PROXY,
366
- NO_PROXY: process.env.NO_PROXY,
367
- no_proxy: process.env.no_proxy,
368
- };
369
- try {
370
- process.env.HTTPS_PROXY = "http://env-proxy.example.test:8080";
371
- delete process.env.NO_PROXY;
372
- delete process.env.no_proxy;
373
- let lookups = 0;
374
- await assert.rejects(
375
- runWithProxy("", () => validateRemoteUrl("https://public.example.test/", {
376
- trustEnvProxy: true,
377
- lookup: async () => {
378
- lookups++;
379
- return [{ address: "10.0.0.10", family: 4 }];
380
- },
381
- })),
382
- /Blocked internal address/,
383
- );
384
- assert.equal(lookups, 1);
385
- } finally {
386
- for (const [key, value] of Object.entries(previous)) {
387
- if (value === undefined) delete process.env[key];
388
- else process.env[key] = value;
389
- }
390
- }
391
- });
392
-
393
- test("trustEnvProxy ignores invalid proxy environment values", async () => {
394
- const previous = {
395
- HTTP_PROXY: process.env.HTTP_PROXY,
396
- http_proxy: process.env.http_proxy,
397
- HTTPS_PROXY: process.env.HTTPS_PROXY,
398
- https_proxy: process.env.https_proxy,
399
- ALL_PROXY: process.env.ALL_PROXY,
400
- all_proxy: process.env.all_proxy,
401
- NO_PROXY: process.env.NO_PROXY,
402
- no_proxy: process.env.no_proxy,
403
- };
404
- try {
405
- for (const key of Object.keys(previous)) delete process.env[key];
406
- for (const value of ["garbage", "file:///tmp/proxy", " "]) {
407
- process.env.HTTPS_PROXY = value;
408
- let lookups = 0;
409
- const url = await validateRemoteUrl("https://public.example.test/", {
410
- trustEnvProxy: true,
411
- lookup: async () => {
412
- lookups++;
413
- return [{ address: "93.184.216.34", family: 4 }];
414
- },
415
- });
416
- assert.equal(url.hostname, "public.example.test");
417
- assert.equal(lookups, 1);
418
- }
419
- } finally {
420
- for (const [key, value] of Object.entries(previous)) {
421
- if (value === undefined) delete process.env[key];
422
- else process.env[key] = value;
423
- }
424
- }
425
- });
426
-
427
- test("trustEnvProxy still performs DNS for NO_PROXY hosts", async () => {
428
- const previous = {
429
- HTTPS_PROXY: process.env.HTTPS_PROXY,
430
- NO_PROXY: process.env.NO_PROXY,
431
- };
432
- try {
433
- process.env.HTTPS_PROXY = "http://proxy.example.test:8080";
434
- process.env.NO_PROXY = "public.example.test:443, .internal.example.test";
435
- let lookups = 0;
436
- const lookup = async () => {
437
- lookups++;
438
- return [{ address: "93.184.216.34", family: 4 }];
439
- };
440
-
441
- await validateRemoteUrl("https://public.example.test/", { trustEnvProxy: true, lookup });
442
- await validateRemoteUrl("https://api.internal.example.test/", { trustEnvProxy: true, lookup });
443
- assert.equal(lookups, 2);
444
-
445
- await validateRemoteUrl("https://public.example.test:8443/", { trustEnvProxy: true, lookup });
446
- assert.equal(lookups, 2);
447
-
448
- await validateRemoteUrl("https://other.example.test/", { trustEnvProxy: true, lookup });
449
- assert.equal(lookups, 2);
450
- } finally {
451
- for (const [key, value] of Object.entries(previous)) {
452
- if (value === undefined) delete process.env[key];
453
- else process.env[key] = value;
454
- }
455
- }
456
- });
1
+ import assert from "node:assert/strict";
2
+ import { test } from "node:test";
3
+
4
+ import { fetchRemoteUrl, validateRemoteUrl } from "../ssrf-protection.ts";
5
+ import { runWithProxy } from "../utils.ts";
6
+
7
+ const publicLookup = async () => [{ address: "93.184.216.34", family: 4 }];
8
+
9
+ async function rejectsInternal(url) {
10
+ await assert.rejects(
11
+ validateRemoteUrl(url, { lookup: publicLookup }),
12
+ /internal|Blocked/,
13
+ `${url} should be blocked`,
14
+ );
15
+ }
16
+
17
+ test("validateRemoteUrl blocks localhost, loopback, link-local, private, and metadata targets", async () => {
18
+ await rejectsInternal("http://localhost/");
19
+ await rejectsInternal("http://127.0.0.1/");
20
+ await rejectsInternal("http://10.0.0.1/");
21
+ await rejectsInternal("http://172.16.0.1/");
22
+ await rejectsInternal("http://192.168.1.1/");
23
+ await rejectsInternal("http://169.254.169.254/latest/meta-data/");
24
+ await rejectsInternal("http://0.0.0.0/");
25
+ await rejectsInternal("http://[::1]/");
26
+ await rejectsInternal("http://[fe80::1]/");
27
+ await rejectsInternal("http://[fd00::1]/");
28
+ await rejectsInternal("http://[::ffff:127.0.0.1]/");
29
+ });
30
+
31
+ test("validateRemoteUrl blocks encoded and alternate loopback IPv4 forms", async () => {
32
+ await rejectsInternal("http://2130706433/");
33
+ await rejectsInternal("http://0177.0.0.1/");
34
+ await rejectsInternal("http://0x7f.0.0.1/");
35
+ await rejectsInternal("http://127.1/");
36
+ });
37
+
38
+ test("validateRemoteUrl blocks hostnames that resolve to private addresses", async () => {
39
+ await assert.rejects(
40
+ validateRemoteUrl("https://example.test/", {
41
+ lookup: async () => [{ address: "192.168.0.2", family: 4 }],
42
+ }),
43
+ /Blocked internal address for example\.test: 192\.168\.0\.2/,
44
+ );
45
+
46
+ await assert.rejects(
47
+ validateRemoteUrl("https://example.test/", {
48
+ lookup: async () => [{ address: "93.184.216.34", family: 4 }, { address: "fd00::1", family: 6 }],
49
+ }),
50
+ /Blocked internal address for example\.test: fd00::1/,
51
+ );
52
+ });
53
+
54
+ test("loopback opt-in does not allow arbitrary hostnames that resolve to loopback", async () => {
55
+ await assert.rejects(
56
+ validateRemoteUrl("https://example.test/", {
57
+ allowLoopback: true,
58
+ lookup: async () => [{ address: "127.0.0.1", family: 4 }],
59
+ }),
60
+ /Blocked internal address for example\.test: 127\.0\.0\.1/,
61
+ );
62
+ });
63
+
64
+ test("validateRemoteUrl permits public HTTP and HTTPS targets", async () => {
65
+ assert.equal((await validateRemoteUrl("https://example.com/path", { lookup: publicLookup })).hostname, "example.com");
66
+ assert.equal((await validateRemoteUrl("http://93.184.216.34/")).hostname, "93.184.216.34");
67
+ assert.equal((await validateRemoteUrl("https://[2606:2800:220:1:248:1893:25c8:1946]/")).hostname, "[2606:2800:220:1:248:1893:25c8:1946]");
68
+ });
69
+
70
+ test("domain policy allows exact and subdomain matches, and is off by default", async () => {
71
+ assert.equal((await validateRemoteUrl("https://example.com/", { lookup: publicLookup })).hostname, "example.com");
72
+ assert.equal((await validateRemoteUrl("https://www.example.com/", {
73
+ lookup: publicLookup,
74
+ domainPolicy: { allow: ["example.com"], deny: [] },
75
+ })).hostname, "www.example.com");
76
+ await assert.rejects(
77
+ validateRemoteUrl("https://other.test/", {
78
+ lookup: publicLookup,
79
+ domainPolicy: { allow: ["example.com"], deny: [] },
80
+ }),
81
+ /Hostname not allowed by fetch_content domain policy/,
82
+ );
83
+ });
84
+
85
+ test("domain policy deny wins over allow", async () => {
86
+ await assert.rejects(
87
+ validateRemoteUrl("https://private.example.com/", {
88
+ lookup: publicLookup,
89
+ domainPolicy: { allow: ["example.com"], deny: ["private.example.com"] },
90
+ }),
91
+ /Blocked hostname by fetch_content domain policy/,
92
+ );
93
+ });
94
+
95
+ test("domain policy validates redirect targets before following", async () => {
96
+ const requested = [];
97
+ const fetchImpl = async (url) => {
98
+ requested.push(url.toString());
99
+ return new Response("", { status: 302, headers: { location: "https://denied.example/next" } });
100
+ };
101
+
102
+ await assert.rejects(
103
+ fetchRemoteUrl("https://allowed.example/", {}, {
104
+ lookup: publicLookup,
105
+ fetch: fetchImpl,
106
+ domainPolicy: { allow: ["allowed.example"], deny: ["denied.example"] },
107
+ }),
108
+ /Blocked hostname by fetch_content domain policy/,
109
+ );
110
+ assert.deepEqual(requested, ["https://allowed.example/"]);
111
+ });
112
+
113
+ test("domain policy never relaxes SSRF protection", async () => {
114
+ await assert.rejects(
115
+ validateRemoteUrl("http://127.0.0.1/", {
116
+ domainPolicy: { allow: ["127.0.0.1"], deny: [] },
117
+ }),
118
+ /Blocked internal address/,
119
+ );
120
+ await assert.rejects(
121
+ validateRemoteUrl("https://internal.example/", {
122
+ lookup: async () => [{ address: "10.0.0.5", family: 4 }],
123
+ domainPolicy: { allow: ["example"], deny: [] },
124
+ }),
125
+ /Blocked internal address/,
126
+ );
127
+ });
128
+
129
+ test("fetchRemoteUrl validates redirect targets before following", async () => {
130
+ const requested = [];
131
+ const fetchImpl = async (url) => {
132
+ requested.push(url.toString());
133
+ return new Response("", {
134
+ status: 302,
135
+ headers: { location: "http://127.0.0.1/admin" },
136
+ });
137
+ };
138
+
139
+ await assert.rejects(
140
+ fetchRemoteUrl("https://example.com/", {}, { lookup: publicLookup, fetch: fetchImpl }),
141
+ /Blocked internal address/,
142
+ );
143
+ assert.deepEqual(requested, ["https://example.com/"]);
144
+ });
145
+
146
+ test("fetchRemoteUrl follows validated public redirects manually", async () => {
147
+ const requested = [];
148
+ const fetchImpl = async (url) => {
149
+ requested.push(url.toString());
150
+ if (requested.length === 1) {
151
+ return new Response("", {
152
+ status: 301,
153
+ headers: { location: "/next" },
154
+ });
155
+ }
156
+ return new Response("ok", { status: 200 });
157
+ };
158
+
159
+ const response = await fetchRemoteUrl("https://example.com/start", {}, { lookup: publicLookup, fetch: fetchImpl });
160
+ assert.equal(response.status, 200);
161
+ assert.equal(await response.text(), "ok");
162
+ assert.deepEqual(requested, ["https://example.com/start", "https://example.com/next"]);
163
+ });
164
+
165
+ test("fake-IP block errors point to the allowRanges opt-in", async () => {
166
+ const fakeIpLookup = async () => [{ address: "198.18.0.56", family: 4 }];
167
+
168
+ await assert.rejects(
169
+ validateRemoteUrl("https://example.test/", { lookup: fakeIpLookup }),
170
+ /Blocked internal address for example\.test: 198\.18\.0\.56\..*TUN\/fake-IP proxies.*ssrf\.allowRanges.*198\.18\.0\.0\/15/,
171
+ );
172
+ });
173
+
174
+ test("allowRanges exempts a synthetic fake-IP range (e.g. 198.18.0.0/15)", async () => {
175
+ const fakeIpLookup = async () => [{ address: "198.18.0.56", family: 4 }];
176
+
177
+ // Without the exemption this is blocked (the fake-IP proxy case).
178
+ await assert.rejects(
179
+ validateRemoteUrl("https://example.test/", { lookup: fakeIpLookup }),
180
+ /Blocked internal address for example\.test: 198\.18\.0\.56/,
181
+ );
182
+
183
+ // With allowRanges it passes.
184
+ const url = await validateRemoteUrl("https://example.test/", {
185
+ lookup: fakeIpLookup,
186
+ allowRanges: ["198.18.0.0/15"],
187
+ });
188
+ assert.equal(url.hostname, "example.test");
189
+
190
+ // A bare literal IP in the range is also exempted.
191
+ assert.equal((await validateRemoteUrl("http://198.18.0.99/", { allowRanges: ["198.18.0.0/15"] })).hostname, "198.18.0.99");
192
+ });
193
+
194
+ test("allowRanges works for IPv6 ranges", async () => {
195
+ const fakeIp6Lookup = async () => [{ address: "fd00::1", family: 6 }];
196
+
197
+ await assert.rejects(
198
+ validateRemoteUrl("https://example.test/", { lookup: fakeIp6Lookup }),
199
+ /Blocked internal address for example\.test: fd00::1/,
200
+ );
201
+
202
+ const url = await validateRemoteUrl("https://example.test/", {
203
+ lookup: fakeIp6Lookup,
204
+ allowRanges: ["fd00::/8"],
205
+ });
206
+ assert.equal(url.hostname, "example.test");
207
+ });
208
+
209
+ test("allowRanges does not relax protection outside the listed range", async () => {
210
+ // 10.0.0.1 is private; an unrelated exemption must NOT cover it.
211
+ await assert.rejects(
212
+ validateRemoteUrl("https://example.test/", {
213
+ lookup: async () => [{ address: "10.0.0.1", family: 4 }],
214
+ allowRanges: ["198.18.0.0/15"],
215
+ }),
216
+ /Blocked internal address for example\.test: 10\.0\.0\.1/,
217
+ );
218
+
219
+ // Exact /32 boundary: allowed in-range, blocked just outside.
220
+ assert.equal((await validateRemoteUrl("http://198.18.0.0/", { allowRanges: ["198.18.0.0/31"] })).hostname, "198.18.0.0");
221
+ assert.equal((await validateRemoteUrl("http://198.18.0.1/", { allowRanges: ["198.18.0.0/31"] })).hostname, "198.18.0.1");
222
+ await assert.rejects(
223
+ validateRemoteUrl("http://198.18.0.2/", { allowRanges: ["198.18.0.0/31"] }),
224
+ /Blocked internal address/,
225
+ );
226
+ });
227
+
228
+ test("allowRanges accepts a bare host (no prefix) and treats it as /32", async () => {
229
+ assert.equal((await validateRemoteUrl("http://198.18.1.2/", { allowRanges: ["198.18.1.2"] })).hostname, "198.18.1.2");
230
+ });
231
+
232
+ test("allowRanges rejects an empty or non-numeric CIDR prefix instead of treating it as /0", async () => {
233
+ // Regression: a trailing slash with no prefix (e.g. "198.18.0.0/") must NOT
234
+ // become /0, which would exempt every address from the SSRF guard.
235
+ for (const bad of ["198.18.0.0/", "198.18.0.0/ ", "fd00::/", "10.0.0.0/abc", "10.0.0.0/ 8"]) {
236
+ await assert.rejects(
237
+ validateRemoteUrl("http://198.18.0.5/", { allowRanges: [bad] }),
238
+ /Invalid CIDR notation in ssrf\.allowRanges/,
239
+ `${bad} should be rejected`,
240
+ );
241
+ }
242
+
243
+ // The dangerous outcome is prevented: a metadata/private IP is not exempted
244
+ // by a malformed "/" entry; the misconfiguration surfaces as an error.
245
+ await assert.rejects(
246
+ validateRemoteUrl("http://169.254.169.254/", { allowRanges: ["198.18.0.0/"] }),
247
+ /Invalid CIDR notation in ssrf\.allowRanges/,
248
+ );
249
+ });
250
+
251
+ test("allowRanges rejects all-address /0 CIDRs", async () => {
252
+ await assert.rejects(
253
+ validateRemoteUrl("http://169.254.169.254/", { allowRanges: ["0.0.0.0/0"] }),
254
+ /Invalid CIDR notation in ssrf\.allowRanges/,
255
+ );
256
+ await assert.rejects(
257
+ validateRemoteUrl("http://[fd00::1]/", { allowRanges: ["::/0"] }),
258
+ /Invalid CIDR notation in ssrf\.allowRanges/,
259
+ );
260
+ });
261
+
262
+ test("invalid allowRanges entries throw a descriptive error", async () => {
263
+ for (const bad of ["not-an-ip", "198.18.0.0/33", "198.18.0.0/-1", "999.0.0.0/8", "fd00::/129"]) {
264
+ await assert.rejects(
265
+ validateRemoteUrl("http://198.18.0.5/", { allowRanges: [bad] }),
266
+ new RegExp(`Invalid CIDR notation in ssrf\.allowRanges: "${bad.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\\$&")}"`),
267
+ );
268
+ }
269
+ await assert.rejects(
270
+ validateRemoteUrl("http://198.18.0.5/", { allowRanges: "198.18.0.0/15" }),
271
+ /ssrf\.allowRanges must be an array/,
272
+ );
273
+ });
274
+
275
+ test("allowRanges flows through fetchRemoteUrl and its redirect targets", async () => {
276
+ const requested = [];
277
+ const fetchImpl = async (url) => {
278
+ requested.push(url.toString());
279
+ if (requested.length === 1) {
280
+ return new Response("", { status: 302, headers: { location: "http://198.18.0.99/admin" } });
281
+ }
282
+ return new Response("ok", { status: 200 });
283
+ };
284
+
285
+ const response = await fetchRemoteUrl(
286
+ "https://example.com/",
287
+ {},
288
+ { lookup: publicLookup, fetch: fetchImpl, allowRanges: ["198.18.0.0/15"] },
289
+ );
290
+ assert.equal(response.status, 200);
291
+ assert.deepEqual(requested, ["https://example.com/", "http://198.18.0.99/admin"]);
292
+ });
293
+
294
+ test("trustEnvProxy skips hostname DNS only for a configured proxy", async () => {
295
+ const previous = {
296
+ HTTP_PROXY: process.env.HTTP_PROXY,
297
+ http_proxy: process.env.http_proxy,
298
+ HTTPS_PROXY: process.env.HTTPS_PROXY,
299
+ https_proxy: process.env.https_proxy,
300
+ ALL_PROXY: process.env.ALL_PROXY,
301
+ all_proxy: process.env.all_proxy,
302
+ NO_PROXY: process.env.NO_PROXY,
303
+ no_proxy: process.env.no_proxy,
304
+ };
305
+ try {
306
+ for (const key of Object.keys(previous)) delete process.env[key];
307
+ process.env.HTTPS_PROXY = "http://proxy.example.test:8080";
308
+ let lookups = 0;
309
+ const lookup = async () => {
310
+ lookups++;
311
+ throw new Error("DNS should not run for proxied host");
312
+ };
313
+
314
+ await validateRemoteUrl("https://public.example.test/", { trustEnvProxy: true, lookup });
315
+ assert.equal(lookups, 0);
316
+
317
+ await assert.rejects(
318
+ validateRemoteUrl("https://127.0.0.1/", { trustEnvProxy: true, lookup }),
319
+ /Blocked internal address/,
320
+ );
321
+ await assert.rejects(
322
+ validateRemoteUrl("https://localhost/", { trustEnvProxy: true, lookup }),
323
+ /Blocked internal hostname/,
324
+ );
325
+ } finally {
326
+ for (const [key, value] of Object.entries(previous)) {
327
+ if (value === undefined) delete process.env[key];
328
+ else process.env[key] = value;
329
+ }
330
+ }
331
+ });
332
+
333
+ test("trustEnvProxy still validates DNS when a curl proxy is active", async () => {
334
+ const previous = {
335
+ HTTPS_PROXY: process.env.HTTPS_PROXY,
336
+ NO_PROXY: process.env.NO_PROXY,
337
+ no_proxy: process.env.no_proxy,
338
+ };
339
+ try {
340
+ process.env.HTTPS_PROXY = "http://env-proxy.example.test:8080";
341
+ delete process.env.NO_PROXY;
342
+ delete process.env.no_proxy;
343
+ let lookups = 0;
344
+ await assert.rejects(
345
+ runWithProxy("http://curl-proxy.example.test:8080", () => validateRemoteUrl("https://public.example.test/", {
346
+ trustEnvProxy: true,
347
+ lookup: async () => {
348
+ lookups++;
349
+ return [{ address: "10.0.0.10", family: 4 }];
350
+ },
351
+ })),
352
+ /Blocked internal address/,
353
+ );
354
+ assert.equal(lookups, 1);
355
+ } finally {
356
+ for (const [key, value] of Object.entries(previous)) {
357
+ if (value === undefined) delete process.env[key];
358
+ else process.env[key] = value;
359
+ }
360
+ }
361
+ });
362
+
363
+ test("trustEnvProxy still validates DNS when empty proxy forces direct access", async () => {
364
+ const previous = {
365
+ HTTPS_PROXY: process.env.HTTPS_PROXY,
366
+ NO_PROXY: process.env.NO_PROXY,
367
+ no_proxy: process.env.no_proxy,
368
+ };
369
+ try {
370
+ process.env.HTTPS_PROXY = "http://env-proxy.example.test:8080";
371
+ delete process.env.NO_PROXY;
372
+ delete process.env.no_proxy;
373
+ let lookups = 0;
374
+ await assert.rejects(
375
+ runWithProxy("", () => validateRemoteUrl("https://public.example.test/", {
376
+ trustEnvProxy: true,
377
+ lookup: async () => {
378
+ lookups++;
379
+ return [{ address: "10.0.0.10", family: 4 }];
380
+ },
381
+ })),
382
+ /Blocked internal address/,
383
+ );
384
+ assert.equal(lookups, 1);
385
+ } finally {
386
+ for (const [key, value] of Object.entries(previous)) {
387
+ if (value === undefined) delete process.env[key];
388
+ else process.env[key] = value;
389
+ }
390
+ }
391
+ });
392
+
393
+ test("trustEnvProxy ignores invalid proxy environment values", async () => {
394
+ const previous = {
395
+ HTTP_PROXY: process.env.HTTP_PROXY,
396
+ http_proxy: process.env.http_proxy,
397
+ HTTPS_PROXY: process.env.HTTPS_PROXY,
398
+ https_proxy: process.env.https_proxy,
399
+ ALL_PROXY: process.env.ALL_PROXY,
400
+ all_proxy: process.env.all_proxy,
401
+ NO_PROXY: process.env.NO_PROXY,
402
+ no_proxy: process.env.no_proxy,
403
+ };
404
+ try {
405
+ for (const key of Object.keys(previous)) delete process.env[key];
406
+ for (const value of ["garbage", "file:///tmp/proxy", " "]) {
407
+ process.env.HTTPS_PROXY = value;
408
+ let lookups = 0;
409
+ const url = await validateRemoteUrl("https://public.example.test/", {
410
+ trustEnvProxy: true,
411
+ lookup: async () => {
412
+ lookups++;
413
+ return [{ address: "93.184.216.34", family: 4 }];
414
+ },
415
+ });
416
+ assert.equal(url.hostname, "public.example.test");
417
+ assert.equal(lookups, 1);
418
+ }
419
+ } finally {
420
+ for (const [key, value] of Object.entries(previous)) {
421
+ if (value === undefined) delete process.env[key];
422
+ else process.env[key] = value;
423
+ }
424
+ }
425
+ });
426
+
427
+ test("trustEnvProxy still performs DNS for NO_PROXY hosts", async () => {
428
+ const previous = {
429
+ HTTPS_PROXY: process.env.HTTPS_PROXY,
430
+ NO_PROXY: process.env.NO_PROXY,
431
+ };
432
+ try {
433
+ process.env.HTTPS_PROXY = "http://proxy.example.test:8080";
434
+ process.env.NO_PROXY = "public.example.test:443, .internal.example.test";
435
+ let lookups = 0;
436
+ const lookup = async () => {
437
+ lookups++;
438
+ return [{ address: "93.184.216.34", family: 4 }];
439
+ };
440
+
441
+ await validateRemoteUrl("https://public.example.test/", { trustEnvProxy: true, lookup });
442
+ await validateRemoteUrl("https://api.internal.example.test/", { trustEnvProxy: true, lookup });
443
+ assert.equal(lookups, 2);
444
+
445
+ await validateRemoteUrl("https://public.example.test:8443/", { trustEnvProxy: true, lookup });
446
+ assert.equal(lookups, 2);
447
+
448
+ await validateRemoteUrl("https://other.example.test/", { trustEnvProxy: true, lookup });
449
+ assert.equal(lookups, 2);
450
+ } finally {
451
+ for (const [key, value] of Object.entries(previous)) {
452
+ if (value === undefined) delete process.env[key];
453
+ else process.env[key] = value;
454
+ }
455
+ }
456
+ });