@iso4/fetch 0.1.0 → 0.1.2

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/README.md +11 -10
  2. package/dist/index.mjs +24 -10
  3. package/package.json +7 -7
package/README.md CHANGED
@@ -249,16 +249,17 @@ middleware body that is not bytes or text is JSON-encoded. Each call to
249
249
 
250
250
  ## Security defaults
251
251
 
252
- | Threat | Mitigation |
253
- | --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
254
- | SSRF / private IP | DNS resolved at connection time (allowed requests only); loopback, RFC1918, link-local blocked; the connected socket's address is checked again |
255
- | DNS rebinding | undici DNS interceptor pins the connection to the resolved IP |
256
- | Redirect bypass | No auto-follow by default; allow/deny re-checked on each hop |
257
- | Credential leak on redirect | `authorization`/`cookie`/`proxy-authorization` dropped on a cross-origin hop |
258
- | Response amplification | Body streamed with `maxBodyBytes` cap |
259
- | Host auth leakage | Isolated undici `Agent` — no shared pool, cookies, or auth with the host app |
260
- | Path traversal | `.`/`..` (and the `%2e` forms) normalised by the URL parser before matching |
261
- | Path/allowlist consistency | Paths matched literally, never decoded, so the approved path is sent verbatim |
252
+ | Threat | Mitigation |
253
+ | ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
254
+ | SSRF / private IP | DNS resolved at connection time (allowed requests only); loopback, RFC1918, link-local blocked; the connected socket's address is checked again |
255
+ | DNS rebinding | undici DNS interceptor pins the connection to the resolved IP |
256
+ | Redirect bypass | No auto-follow by default; allow/deny re-checked on each hop |
257
+ | Credential leak on redirect | `authorization`/`cookie`/`proxy-authorization` dropped on a cross-origin hop |
258
+ | Response amplification | Body streamed with `maxBodyBytes` cap |
259
+ | Host auth leakage | Isolated undici `Agent` — no shared pool, cookies, or auth with the host app |
260
+ | Path traversal | `.`/`..` (and the `%2e` forms) normalised by the URL parser before matching |
261
+ | Path/allowlist consistency | Paths matched literally, never decoded, so the approved path is sent verbatim |
262
+ | Credentials in error messages | URLs with credentials refused; invalid-URL and invalid-header errors use fixed messages that never quote the URL or value |
262
263
 
263
264
  Paths are matched and forwarded **literally** — iso4 never percent-decodes
264
265
  them, so the path the allowlist approves is byte-for-byte the path that is sent.
package/dist/index.mjs CHANGED
@@ -242,7 +242,18 @@ const REDIRECT_STATUSES = new Set([
242
242
  307,
243
243
  308
244
244
  ]);
245
+ const INVALID_URL = "fetch: invalid URL";
246
+ const URL_CREDENTIALS = "fetch: URL must not contain credentials";
247
+ const INVALID_HEADER = "fetch: invalid header name or value";
245
248
  async function httpRequest(url, method, headers, body, agent, maxBodyBytes, signal) {
249
+ const target = URL.parse(url);
250
+ if (target === null) throw new TypeError(INVALID_URL);
251
+ if (target.username !== "" || target.password !== "") throw new TypeError(URL_CREDENTIALS);
252
+ try {
253
+ new Headers(headers);
254
+ } catch {
255
+ throw new TypeError(INVALID_HEADER);
256
+ }
246
257
  const response = await fetch(url, {
247
258
  method,
248
259
  headers,
@@ -360,6 +371,16 @@ function createSafeFetch(options) {
360
371
  function createSafeFetcher(options) {
361
372
  const run = buildSafeFetchHandler(options);
362
373
  return async (input, init) => {
374
+ if (!(input instanceof Request)) {
375
+ const target = URL.parse(input.toString());
376
+ if (target === null) throw new TypeError(INVALID_URL);
377
+ if (target.username !== "" || target.password !== "") throw new TypeError(URL_CREDENTIALS);
378
+ }
379
+ try {
380
+ new Headers(init?.headers);
381
+ } catch {
382
+ throw new TypeError(INVALID_HEADER);
383
+ }
363
384
  const request = new Request(input, init);
364
385
  const headers = {};
365
386
  request.headers.forEach((value, key) => {
@@ -484,16 +505,9 @@ function buildSafeFetchHandler(options) {
484
505
  });
485
506
  }
486
507
  return async (hostRequest) => {
487
- let parsedUrl;
488
- try {
489
- parsedUrl = new URL(hostRequest.url);
490
- } catch {
491
- throw new Error(`fetch: invalid URL "${hostRequest.url}"`);
492
- }
493
- if (parsedUrl.username !== "" || parsedUrl.password !== "") {
494
- parsedUrl.username = "";
495
- parsedUrl.password = "";
496
- }
508
+ const parsedUrl = URL.parse(hostRequest.url);
509
+ if (parsedUrl === null) throw new TypeError(INVALID_URL);
510
+ if (parsedUrl.username !== "" || parsedUrl.password !== "") throw new TypeError(URL_CREDENTIALS);
497
511
  const baseHeaders = { ...hostRequest.headers };
498
512
  if (!allowCompressed) baseHeaders["accept-encoding"] = "identity";
499
513
  delete baseHeaders["host"];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iso4/fetch",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "description": "Hardened FetchHandler for the @iso4/sandbox runtime: DNS pinning, host allowlist, private-IP blocking, no auto-redirect",
6
6
  "keywords": [
@@ -38,16 +38,16 @@
38
38
  ],
39
39
  "dependencies": {
40
40
  "rou3": "^0.8.1",
41
- "undici": "^6.25.0"
41
+ "undici": "^6.29.1"
42
42
  },
43
43
  "devDependencies": {
44
- "@changesets/cli": "^2.27.10",
45
- "@schplitt/eslint-config": "^1.5.0",
46
- "eslint": "^10.2.1",
44
+ "@changesets/cli": "^2.31.1",
45
+ "@schplitt/eslint-config": "^1.5.4",
46
+ "eslint": "^10.15.0",
47
47
  "tsdown": "^0.21.10",
48
- "tsnapi": "^1.2.0",
48
+ "tsnapi": "^1.6.1",
49
49
  "typescript": "^6.0.3",
50
- "vitest": "^4.1.5"
50
+ "vitest": "^4.1.12"
51
51
  },
52
52
  "engines": {
53
53
  "node": ">=24.0.0"