@heroku/heroku-fetch 0.3.0 → 0.4.1

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.
package/README.md CHANGED
@@ -56,12 +56,20 @@ const dataClient = new HerokuApiClient({
56
56
  });
57
57
 
58
58
  // The data client's host comes from HEROKU_DATA_HOST, then
59
- // HEROKU_POSTGRESQL_HOST, then postgres-api.heroku.com. A bare hostname
59
+ // HEROKU_POSTGRESQL_HOST, then https://api.data.heroku.com. A bare hostname
60
60
  // gets https://. An explicit baseUrl takes precedence.
61
61
  //
62
- // This applies to every service host variable below: a plain http:// value
63
- // is accepted only for localhost, 127.0.0.1 or [::1]. Any other http:// host
64
- // throws when the client is built, so the token is never sent unencrypted.
62
+ // Every environment-configured service host must use heroku.com, herokai.com,
63
+ // herokuspace.com, herokudev.com, or the loopback hosts localhost, 127.0.0.1
64
+ // and [::1]. Remote hosts require HTTPS; HTTP is allowed only for loopback.
65
+ // This applies to HEROKU_DATA_HOST, HEROKU_POSTGRESQL_HOST,
66
+ // HEROKU_METRICS_HOST, HEROKU_NOTIFICATIONS_HOST, HEROKU_PARTICLEBOARD_URL
67
+ // and HEROKU_REPOSITORIES_HOST.
68
+ // Configured URLs are reduced to their origin; user information, non-root
69
+ // paths, queries and fragments are rejected. Explicit baseUrl values remain
70
+ // the caller's responsibility. In particular, *.herokuapp.com is not trusted
71
+ // for environment-configured hosts; internal staging setups should use an
72
+ // approved Heroku-owned hostname instead.
65
73
 
66
74
  // Metrics. The host comes from HEROKU_METRICS_HOST, then
67
75
  // api.metrics.heroku.com. An explicit baseUrl takes precedence.
@@ -1,37 +1,57 @@
1
1
  const HEROKU_V3_ACCEPT = 'application/vnd.heroku+json; version=3';
2
2
  const LOOPBACK_HOSTNAMES = new Set(['127.0.0.1', '[::1]', 'localhost']);
3
+ const TRUSTED_HEROKU_DOMAINS = ['heroku.com', 'herokai.com', 'herokuspace.com', 'herokudev.com'];
3
4
  /**
4
5
  * The base URL from the first of `variables` that is set, else `fallback`.
5
6
  * Values are trimmed, and blank ones are skipped. A bare hostname gets `https://`;
6
- * a value with an `http://` or `https://` scheme, in any case, is used as-is.
7
+ * a value with an `http://` or `https://` scheme is parsed as given.
7
8
  * `process` and `process.env` are guarded for browser builds.
8
9
  *
9
- * Plain `http://` is allowed only for loopback hosts (local development), so the
10
- * client's bearer token is never sent unencrypted to a remote host. Otherwise it
11
- * throws, naming the variable but not echoing its value.
10
+ * Hostnames must be in an approved Heroku domain or loopback. Plain `http://` is
11
+ * allowed only for loopback hosts (local development), so the client's bearer
12
+ * token is never sent unencrypted to a remote host. Errors name the variable but
13
+ * never echo its value.
12
14
  */
13
15
  function envBaseUrl(variables, fallback) {
14
16
  const env = typeof process === 'undefined' ? undefined : process.env;
15
17
  for (const variable of variables) {
16
- const host = env?.[variable]?.trim();
17
- if (!host)
18
+ const value = env?.[variable]?.trim();
19
+ if (!value)
18
20
  continue;
19
- if (!/^https?:\/\//i.test(host))
20
- return `https://${host}`;
21
- if (/^http:/i.test(host) && !isLoopback(host)) {
22
- throw new Error(`${variable} must use https:// unless it points at localhost`);
23
- }
24
- return host;
21
+ return validatedEnvBaseUrl(variable, value);
25
22
  }
26
23
  return fallback;
27
24
  }
28
- function isLoopback(url) {
25
+ function validatedEnvBaseUrl(variable, value) {
26
+ const explicitScheme = /^[a-z][a-z\d+.-]*:\/\//i.test(value);
27
+ const candidate = explicitScheme ? value : `https://${value}`;
28
+ let url;
29
29
  try {
30
- return LOOPBACK_HOSTNAMES.has(new URL(url).hostname);
30
+ url = new URL(candidate);
31
31
  }
32
32
  catch {
33
- return false;
33
+ throw new Error(`${variable} must be a valid Heroku service URL`);
34
34
  }
35
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
36
+ throw new Error(`${variable} must use http:// or https://`);
37
+ }
38
+ // URL.username/password omit an empty `@`, so also check the input for that
39
+ // delimiter before accepting the parsed URL.
40
+ if (url.username || url.password || candidate.includes('@')) {
41
+ throw new Error(`${variable} must not include user information`);
42
+ }
43
+ if (url.pathname !== '/' || url.search || url.hash || candidate.includes('?') || candidate.includes('#')) {
44
+ throw new Error(`${variable} must not include a path, query, or fragment`);
45
+ }
46
+ const loopback = LOOPBACK_HOSTNAMES.has(url.hostname);
47
+ if (url.protocol === 'http:' && !loopback) {
48
+ throw new Error(`${variable} must use https:// unless it points at localhost`);
49
+ }
50
+ const approvedDomain = TRUSTED_HEROKU_DOMAINS.some(domain => url.hostname === domain || url.hostname.endsWith(`.${domain}`));
51
+ if (!loopback && !approvedDomain) {
52
+ throw new Error(`${variable} must use a heroku.com, herokai.com, herokuspace.com, or herokudev.com host`);
53
+ }
54
+ return url.origin;
35
55
  }
36
56
  /**
37
57
  * Per-service defaults. Services whose host can be overridden by an environment
@@ -50,7 +70,7 @@ export const SERVICE_CONFIGS = {
50
70
  },
51
71
  data: {
52
72
  get baseUrl() {
53
- return envBaseUrl(['HEROKU_DATA_HOST', 'HEROKU_POSTGRESQL_HOST'], 'https://postgres-api.heroku.com');
73
+ return envBaseUrl(['HEROKU_DATA_HOST', 'HEROKU_POSTGRESQL_HOST'], 'https://api.data.heroku.com');
54
74
  },
55
75
  defaultAccept: HEROKU_V3_ACCEPT,
56
76
  },
package/dist/redact.d.ts CHANGED
@@ -7,8 +7,8 @@
7
7
  /**
8
8
  * Scrub a value for debug logging. Handles `Error`, `Headers`, plain objects,
9
9
  * and primitives (passed through unchanged — a top-level string is not
10
- * pattern-scrubbed; `VALUE_PATTERNS` only apply to strings nested inside a
11
- * scrubbed object/Error). Fail-closed: any failure returns `'[unscrubbable]'`
10
+ * pattern-scrubbed; `CREDENTIAL_PATTERNS` only apply to strings nested inside
11
+ * a scrubbed object/Error). Fail-closed: any failure returns `'[unscrubbable]'`
12
12
  * rather than risking the raw value. `depth` is internal, for `cause`
13
13
  * recursion.
14
14
  */
package/dist/redact.js CHANGED
@@ -17,16 +17,6 @@ const EXTRA_FIELDS = [
17
17
  'otp',
18
18
  'sso',
19
19
  ];
20
- /**
21
- * Redact inline `Bearer`/`Basic` credentials in string leaf values reached
22
- * while scrubbing an object or Error (e.g. an error message). Bare UUIDs/JWTs
23
- * are intentionally not matched — they're indistinguishable from benign
24
- * resource IDs. Note these patterns don't apply to a top-level string passed
25
- * straight to `scrubForLog`.
26
- */
27
- const VALUE_PATTERNS = [
28
- /\b(?:Bearer|Basic)\s+[\w.~+/=-]+/gi,
29
- ];
30
20
  /** Cap on `error.cause` recursion depth; guards against cyclic chains. */
31
21
  const MAX_CAUSE_DEPTH = 8;
32
22
  let scrubberPromise;
@@ -34,13 +24,18 @@ let scrubberPromise;
34
24
  async function getScrubber() {
35
25
  if (!scrubberPromise) {
36
26
  scrubberPromise = (async () => {
37
- const [{ Scrubber }, { HEROKU_FIELDS }] = await Promise.all([
27
+ const [{ Scrubber }, { HEROKU_FIELDS }, { CREDENTIAL_PATTERNS }] = await Promise.all([
38
28
  import('@heroku/js-blanket/core/scrubber'),
39
29
  import('@heroku/js-blanket/core/presets'),
30
+ import('@heroku/js-blanket/core/patterns'),
40
31
  ]);
32
+ // CREDENTIAL_PATTERNS redact credentials in string values reached while
33
+ // scrubbing an object or Error, such as an error message: Heroku API
34
+ // tokens, Authorization values, Bearer/Basic credentials, URL passwords
35
+ // and PEM private keys. Each keeps its context (`Bearer [REDACTED]`).
41
36
  return new Scrubber({
42
37
  fields: [...HEROKU_FIELDS, ...EXTRA_FIELDS],
43
- patterns: [...VALUE_PATTERNS],
38
+ patterns: CREDENTIAL_PATTERNS,
44
39
  replacement: REPLACEMENT,
45
40
  });
46
41
  })();
@@ -122,8 +117,8 @@ async function scrubError(error, depth) {
122
117
  /**
123
118
  * Scrub a value for debug logging. Handles `Error`, `Headers`, plain objects,
124
119
  * and primitives (passed through unchanged — a top-level string is not
125
- * pattern-scrubbed; `VALUE_PATTERNS` only apply to strings nested inside a
126
- * scrubbed object/Error). Fail-closed: any failure returns `'[unscrubbable]'`
120
+ * pattern-scrubbed; `CREDENTIAL_PATTERNS` only apply to strings nested inside
121
+ * a scrubbed object/Error). Fail-closed: any failure returns `'[unscrubbable]'`
127
122
  * rather than risking the raw value. `depth` is internal, for `cause`
128
123
  * recursion.
129
124
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heroku/heroku-fetch",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "A JavaScript/TypeScript API client for Heroku APIs",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -50,7 +50,7 @@
50
50
  },
51
51
  "homepage": "https://github.com/heroku/heroku-fetch#readme",
52
52
  "dependencies": {
53
- "@heroku/js-blanket": "^1.2.0",
53
+ "@heroku/js-blanket": "^1.4.0",
54
54
  "debug": "^4.3.4",
55
55
  "ky": "^2.2.0",
56
56
  "undici": "^8.11.2"