@drsamonline/prober 0.12.8 → 0.12.9

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +67 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drsamonline/prober",
3
- "version": "0.12.8",
3
+ "version": "0.12.9",
4
4
  "description": "Canary prober: liveness, readiness and suitability checks that decide what may be routed to.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/index.js CHANGED
@@ -20,6 +20,46 @@
20
20
  import { poolKey } from '@drsamonline/catalog';
21
21
 
22
22
  /** Bump when a fixture changes, so probe history stays comparable. */
23
+ /**
24
+ * Is this URL something a probe should touch?
25
+ *
26
+ * HTTPS only, and never a loopback, link-local, private or metadata address:
27
+ * those are the user's own machine or their cloud account, not a provider. The
28
+ * one exception is an explicit loopback probe, which `sui local` asks for by
29
+ * passing `allowLocal`.
30
+ *
31
+ * @param {string} baseUrl
32
+ * @param {{allowLocal?: boolean}} [opts]
33
+ * @returns {{ok: boolean, reason?: string}}
34
+ */
35
+ export function probeAddressAllowed(baseUrl, opts = {}) {
36
+ let url;
37
+ try {
38
+ url = new URL(baseUrl);
39
+ } catch {
40
+ return { ok: false, reason: `not a URL: ${baseUrl}` };
41
+ }
42
+
43
+ const host = url.hostname.replace(/^\[|\]$/g, '').toLowerCase();
44
+ const isLoopback = host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '0.0.0.0';
45
+
46
+ if (url.protocol !== 'https:' && !(opts.allowLocal && isLoopback)) {
47
+ return { ok: false, reason: `refusing to probe ${url.protocol}// — a probe needs https, unless it is an explicit loopback check` };
48
+ }
49
+ if (isLoopback && !opts.allowLocal) {
50
+ return { ok: false, reason: 'refusing to probe a loopback address — use `sui local` for a model on this machine' };
51
+ }
52
+ // link-local (cloud metadata), private ranges, and bare IPv6 ULA
53
+ if (/^169\.254\./.test(host)) return { ok: false, reason: 'refusing to probe a link-local address (cloud metadata lives there)' };
54
+ if (/^10\./.test(host)) return { ok: false, reason: 'refusing to probe a private address' };
55
+ if (/^192\.168\./.test(host)) return { ok: false, reason: 'refusing to probe a private address' };
56
+ if (/^172\.(1[6-9]|2\d|3[01])\./.test(host)) return { ok: false, reason: 'refusing to probe a private address' };
57
+ if (/^(fc|fd)[0-9a-f]{2}:/.test(host)) return { ok: false, reason: 'refusing to probe a private address' };
58
+ if (host.endsWith('.local') || host.endsWith('.internal')) return { ok: false, reason: 'refusing to probe an internal hostname' };
59
+
60
+ return { ok: true };
61
+ }
62
+
23
63
  export const PROBE_FIXTURE_VERSION = '1.0.0';
24
64
 
25
65
  export const FIXTURES = {
@@ -68,6 +108,29 @@ export async function probeTarget(entry, opts = {}) {
68
108
  /** @type {{liveness: any, readiness: any, suitability: Record<string, any>}} */
69
109
  const levels = { liveness: null, readiness: null, suitability: {} };
70
110
 
111
+ // A catalogue entry is data, and data can name any address. The prober fetches
112
+ // what it is told, so without this guard a seeded or imported base_url of
113
+ // http://169.254.169.254/ (cloud metadata) or http://127.0.0.1:6379 would be
114
+ // fetched on the user's machine and the result reported as a provider's health.
115
+ // `allowLocal` is for a loopback probe that something asked for explicitly —
116
+ // the test fixtures and `sui local`. Production code never sets it.
117
+ const reachable = base ? probeAddressAllowed(base, { allowLocal: opts.allowLocal === true }) : null;
118
+ if (base && !reachable.ok) {
119
+ return {
120
+ pool_key: key,
121
+ provider: entry.provider,
122
+ checkedAt,
123
+ fixtureVersion: PROBE_FIXTURE_VERSION,
124
+ ok: false,
125
+ verified: false,
126
+ latencyMs: 0,
127
+ levels,
128
+ reasons: [reachable.reason],
129
+ fixtures: {},
130
+ };
131
+ }
132
+
133
+
71
134
  if (!base) {
72
135
  return {
73
136
  pool_key: key,
@@ -385,6 +448,9 @@ export class Prober {
385
448
  this.timeoutMs = opts.timeoutMs;
386
449
  this.now = opts.now ?? Date.now;
387
450
  this.onResult = opts.onResult ?? null;
451
+ // explicit opt-in for a loopback probe (tests and `sui local`); the default
452
+ // refuses the user's own machine and their cloud metadata
453
+ this.allowLocal = opts.allowLocal === true;
388
454
 
389
455
  /** @type {Map<string, any>} */
390
456
  this.state = new Map();
@@ -409,7 +475,7 @@ export class Prober {
409
475
  async runOnce() {
410
476
  this.runs += 1;
411
477
  const results = await Promise.all(
412
- this.entries.map((entry) => probeTarget(entry, { fetchImpl: this.fetchImpl, timeoutMs: this.timeoutMs, now: this.now })),
478
+ this.entries.map((entry) => probeTarget(entry, { fetchImpl: this.fetchImpl, timeoutMs: this.timeoutMs, now: this.now, allowLocal: this.allowLocal })),
413
479
  );
414
480
 
415
481
  for (const result of results) {