@tpsdev-ai/flair 0.54.2 → 0.55.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/dist/build-info.json +3 -3
- package/dist/cli.js +774 -176
- package/dist/commands/doctor.js +86 -1
- package/dist/commands/federation.js +131 -36
- package/dist/commands/hook.js +20 -10
- package/dist/commands/init.js +193 -67
- package/dist/commands/service.js +16 -12
- package/dist/commands/soul.js +10 -5
- package/dist/commands/status.js +62 -7
- package/dist/commands/upgrade.js +6 -0
- package/dist/doctor-client.js +118 -32
- package/dist/hook-install.js +170 -16
- package/dist/lib/doctor-run.js +49 -3
- package/dist/lib/embed-gpu-doctor.js +63 -0
- package/dist/lib/federation-pair-access.js +118 -0
- package/dist/lib/harper-port-value.js +43 -0
- package/dist/lib/http-bind.js +227 -0
- package/dist/lib/init-admin-pass.js +316 -0
- package/dist/lib/launchd-management.js +16 -0
- package/dist/lib/launchd-repair.js +122 -1
- package/dist/lib/owned-pins.js +54 -1
- package/dist/lib/process-start-time.js +82 -0
- package/dist/lib/scratch-owner.js +124 -10
- package/dist/lib/stale-client-pin.js +26 -0
- package/dist/lib/upgrade-plain-tree.js +293 -48
- package/dist/rem/runner.js +10 -1
- package/dist/resources/AdminInstance.js +8 -2
- package/dist/resources/Memory.js +36 -1
- package/dist/resources/MemoryBootstrap.js +23 -32
- package/dist/resources/MemoryFeed.js +4 -1
- package/dist/resources/MemoryReflect.js +121 -93
- package/dist/resources/SkillScan.js +13 -11
- package/dist/resources/Soul.js +15 -1
- package/dist/resources/XAA.js +16 -1
- package/dist/resources/a2a-url.js +5 -2
- package/dist/resources/client-version-gate.js +144 -0
- package/dist/resources/embed-gpu.js +241 -0
- package/dist/resources/embeddings-boot.js +34 -42
- package/dist/resources/federation-peer-liveness.js +7 -1
- package/dist/resources/health.js +12 -3
- package/dist/resources/memory-reflect-lib.js +66 -0
- package/dist/resources/memory-reflect-models.js +9 -0
- package/dist/resources/migrations/embedding-stamp.js +32 -4
- package/dist/resources/oauth-discovery.js +3 -1
- package/dist/resources/record-type-kit.js +59 -0
- package/dist/resources/scan/skill-markdown.js +166 -0
- package/dist/resources/scan/skill-scanner.js +89 -68
- package/dist/resources/skill-provenance.js +273 -0
- package/dist/resources/skill-write.js +26 -7
- package/dist/src/lib/harper-port-value.js +43 -0
- package/docs/api-reference.md +35 -2
- package/docs/deployment.md +39 -6
- package/docs/federation.md +18 -7
- package/docs/mcp-clients.md +19 -6
- package/docs/releasing.md +79 -13
- package/docs/rem.md +1 -1
- package/docs/secrets-and-keys.md +1 -0
- package/docs/standalone-local.md +8 -5
- package/docs/troubleshooting.md +17 -0
- package/package.json +1 -1
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spoke pair local-identity denial (flair#820).
|
|
3
|
+
*
|
|
4
|
+
* `flair federation pair` starts with a signed GET of /FederationInstance
|
|
5
|
+
* (CLI tooling — peers never call this path during the handshake). That
|
|
6
|
+
* resource is allowAdmin. When the invoking agent is not a runtime admin,
|
|
7
|
+
* Harper returns a raw `error:AccessViolation` JSON body and pair printed
|
|
8
|
+
* it unchanged — operators read a HUB auth failure and spent hours on
|
|
9
|
+
* hub theories. The failing call is LOCAL.
|
|
10
|
+
*
|
|
11
|
+
* Named error + operator sentence live here so the contract can be
|
|
12
|
+
* unit-tested without driving process.exit. Pair wraps only the identity
|
|
13
|
+
* GET; later failures (token, hub POST, local Peer write) keep their
|
|
14
|
+
* own messages unless the body is specifically a missing pairing role.
|
|
15
|
+
*/
|
|
16
|
+
export const FEDERATION_INSTANCE_PATH = "/FederationInstance";
|
|
17
|
+
export const FEDERATION_PAIR_LOCAL_ACCESS_ERROR_NAME = "FederationPairLocalAccessError";
|
|
18
|
+
export const FEDERATION_PAIR_HUB_ACCESS_ERROR_NAME = "FederationPairHubAccessError";
|
|
19
|
+
export const PAIR_INITIATOR_ROLE = "flair_pair_initiator";
|
|
20
|
+
export const PAIR_INITIATOR_FIX_COMMAND = "flair init --remote";
|
|
21
|
+
export const ADMIN_AGENTS_ENV = "FLAIR_ADMIN_AGENTS";
|
|
22
|
+
/** Real admin-grant surface: upserts `role: "admin"` + `admin: true`. */
|
|
23
|
+
export const PRINCIPAL_ADD_ADMIN_COMMAND = "flair principal add";
|
|
24
|
+
export class FederationPairLocalAccessError extends Error {
|
|
25
|
+
status = 403;
|
|
26
|
+
side;
|
|
27
|
+
path = FEDERATION_INSTANCE_PATH;
|
|
28
|
+
constructor(message, side = "LOCAL") {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = FEDERATION_PAIR_LOCAL_ACCESS_ERROR_NAME;
|
|
31
|
+
this.side = side;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export class FederationPairHubAccessError extends Error {
|
|
35
|
+
status;
|
|
36
|
+
side = "HUB";
|
|
37
|
+
constructor(message, status = 403) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = FEDERATION_PAIR_HUB_ACCESS_ERROR_NAME;
|
|
40
|
+
this.status = status;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function errorText(err) {
|
|
44
|
+
if (err instanceof Error)
|
|
45
|
+
return err.message;
|
|
46
|
+
if (typeof err === "object" && err && "message" in err) {
|
|
47
|
+
return String(err.message ?? err);
|
|
48
|
+
}
|
|
49
|
+
return String(err);
|
|
50
|
+
}
|
|
51
|
+
function isNoCredentialsDenial(err) {
|
|
52
|
+
if (typeof err === "object" && err && "noCredentials" in err) {
|
|
53
|
+
return err.noCredentials === true;
|
|
54
|
+
}
|
|
55
|
+
return /no credentials sent/i.test(errorText(err));
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* True only for Harper's AccessViolation body (missing admin on an
|
|
59
|
+
* authenticated call). Bare 403, no-credentials, and rejected-password
|
|
60
|
+
* bodies stay out so their honest text is not replaced.
|
|
61
|
+
*/
|
|
62
|
+
export function isFederationInstanceAccessViolation(err) {
|
|
63
|
+
if (!err || isNoCredentialsDenial(err))
|
|
64
|
+
return false;
|
|
65
|
+
return /AccessViolation/i.test(errorText(err));
|
|
66
|
+
}
|
|
67
|
+
export function principalAddAdminInvocation(agentId) {
|
|
68
|
+
return `${PRINCIPAL_ADD_ADMIN_COMMAND} ${agentId} --admin`;
|
|
69
|
+
}
|
|
70
|
+
export function describeFederationPairLocalAccessError(opts) {
|
|
71
|
+
const side = opts.side ?? "LOCAL";
|
|
72
|
+
const base = opts.url.replace(/\/$/, "");
|
|
73
|
+
const agent = opts.agentId?.trim() ?? "";
|
|
74
|
+
const identity = `pair: cannot read ${side} instance identity ` +
|
|
75
|
+
`(GET ${base}${FEDERATION_INSTANCE_PATH} → 403 AccessViolation). `;
|
|
76
|
+
const hubRole = `Hub pairing role: if pairing later fails because the hub is missing ` +
|
|
77
|
+
`${PAIR_INITIATOR_ROLE}, restore it with \`${PAIR_INITIATOR_FIX_COMMAND}\`.`;
|
|
78
|
+
if (!agent) {
|
|
79
|
+
return (identity +
|
|
80
|
+
`Missing role/grant: the calling agent is not a runtime admin ` +
|
|
81
|
+
`(${FEDERATION_INSTANCE_PATH} is allowAdmin). ` +
|
|
82
|
+
`Fix: add the agent to ${ADMIN_AGENTS_ENV} in the SERVER process env ` +
|
|
83
|
+
`(not just .env). Set FLAIR_AGENT_ID so the grant can name the principal. ` +
|
|
84
|
+
hubRole);
|
|
85
|
+
}
|
|
86
|
+
return (identity +
|
|
87
|
+
`Missing role/grant: agent '${agent}' is not a runtime admin ` +
|
|
88
|
+
`(${FEDERATION_INSTANCE_PATH} is allowAdmin). ` +
|
|
89
|
+
`Fix: add '${agent}' to ${ADMIN_AGENTS_ENV} in the SERVER process env ` +
|
|
90
|
+
`(not just .env), or grant the admin role with \`${principalAddAdminInvocation(agent)}\`. ` +
|
|
91
|
+
hubRole);
|
|
92
|
+
}
|
|
93
|
+
export function rewriteFederationPairLocalAccessError(err, opts) {
|
|
94
|
+
if (!isFederationInstanceAccessViolation(err))
|
|
95
|
+
return err;
|
|
96
|
+
return new FederationPairLocalAccessError(describeFederationPairLocalAccessError(opts), opts.side ?? "LOCAL");
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Hub POST /FederationPair is a pairing-role problem only when the body
|
|
100
|
+
* names `flair_pair_initiator` or a role-not-found. A bare 403 (proxy,
|
|
101
|
+
* wrong host, rejected bootstrap password) keeps its own text.
|
|
102
|
+
*/
|
|
103
|
+
export function isFederationPairHubAccessDenial(_status, body) {
|
|
104
|
+
return /role[- ]?not[- ]?found|flair_pair_initiator/i.test(body);
|
|
105
|
+
}
|
|
106
|
+
export function describeFederationPairHubAccessError(opts) {
|
|
107
|
+
const hub = opts.hubUrl.replace(/\/$/, "");
|
|
108
|
+
return (`pair: HUB rejected the pairing request ` +
|
|
109
|
+
`(POST ${hub}/FederationPair → ${opts.status}). ` +
|
|
110
|
+
`Missing role/grant: the hub may lack ${PAIR_INITIATOR_ROLE}. ` +
|
|
111
|
+
`Fix: re-run \`${PAIR_INITIATOR_FIX_COMMAND}\` on the hub to restore ` +
|
|
112
|
+
`the pairing role, mint a new token, then retry.`);
|
|
113
|
+
}
|
|
114
|
+
export function rewriteFederationPairHubAccessError(status, hubUrl, body) {
|
|
115
|
+
if (!isFederationPairHubAccessDenial(status, body))
|
|
116
|
+
return null;
|
|
117
|
+
return new FederationPairHubAccessError(describeFederationPairHubAccessError({ hubUrl, status }), status);
|
|
118
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* harper-port-value.ts — the ONE parser for a Harper port config value, shared
|
|
3
|
+
* by the CLI (`src/`) and the runtime resources (`resources/`).
|
|
4
|
+
*
|
|
5
|
+
* Moved out of `src/cli.ts` (where it lived as `harperPortValue`), not
|
|
6
|
+
* rewritten: every consumer that interpolates a port into a URL parses it here.
|
|
7
|
+
* A second, hand-rolled parser is the same defect as a second detector — two
|
|
8
|
+
* implementations that can drift, with the drift only visible on the value one
|
|
9
|
+
* of them mishandles.
|
|
10
|
+
*
|
|
11
|
+
* Harper accepts both a bare port (`9926`, all interfaces) and the
|
|
12
|
+
* host-qualified `host:port` form flair writes for the ops API
|
|
13
|
+
* (`127.0.0.1:9925` — see `opsNetworkPortValue`). Both name the same port; only
|
|
14
|
+
* the bind differs, and `detectOpsApiAllInterfacesBind` (lib/ops-api-bind.ts) is
|
|
15
|
+
* what reads the host half. Splits on the LAST colon so an IPv6 literal
|
|
16
|
+
* (`[::1]:9925`) keeps its port.
|
|
17
|
+
*
|
|
18
|
+
* What it checks: ONLY the digits after the last colon. The host half is NOT
|
|
19
|
+
* validated — any host text is accepted and ignored, so a qualified value means
|
|
20
|
+
* "the port is whatever trails the final colon". A value with no colon, an
|
|
21
|
+
* empty string, or a non-numeric tail yields null.
|
|
22
|
+
*
|
|
23
|
+
* Range: a port outside 1..65535 yields null so the caller falls back to its
|
|
24
|
+
* default. `new URL()` rejects a port above 65535, so returning one would hand
|
|
25
|
+
* a URL builder a value that throws instead of a usable loopback URL.
|
|
26
|
+
*/
|
|
27
|
+
const MAX_TCP_PORT = 65535;
|
|
28
|
+
export function harperPortValue(value) {
|
|
29
|
+
if (value === undefined || value === null)
|
|
30
|
+
return null;
|
|
31
|
+
if (typeof value === "number") {
|
|
32
|
+
return Number.isInteger(value) && value > 0 && value <= MAX_TCP_PORT ? value : null;
|
|
33
|
+
}
|
|
34
|
+
const str = String(value).trim();
|
|
35
|
+
if (str === "")
|
|
36
|
+
return null;
|
|
37
|
+
const lastColon = str.lastIndexOf(":");
|
|
38
|
+
const portPart = lastColon > 0 ? str.slice(lastColon + 1) : str;
|
|
39
|
+
if (!/^\d+$/.test(portPart))
|
|
40
|
+
return null;
|
|
41
|
+
const n = Number(portPart);
|
|
42
|
+
return n > 0 && n <= MAX_TCP_PORT ? n : null;
|
|
43
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* http-bind.ts — the ONE constructor for a Harper HTTP bind value.
|
|
3
|
+
*
|
|
4
|
+
* Every place Flair hands Harper an HTTP listener address — `HTTP_PORT` in a
|
|
5
|
+
* spawn env or a launchd plist, and `http.port` inside a `HARPER_SET_CONFIG`
|
|
6
|
+
* payload — goes through `httpBind()`. It returns a SHAPE (`{ bindValue, host,
|
|
7
|
+
* port }`), not a string, because two different consumers need two different
|
|
8
|
+
* halves of the same answer:
|
|
9
|
+
*
|
|
10
|
+
* - the LISTENER wants the host-qualified `host:port` form (Harper's
|
|
11
|
+
* `listenOnPorts` splits a config port value on its LAST colon and binds
|
|
12
|
+
* that host; a bare number binds all interfaces), and
|
|
13
|
+
* - every CORS allow-list entry and every loopback URL builder wants the
|
|
14
|
+
* PORT half on its own.
|
|
15
|
+
*
|
|
16
|
+
* Returning only the qualified string would force every caller to re-split it —
|
|
17
|
+
* i.e. to re-implement the parser this module exists to be the single copy of.
|
|
18
|
+
*
|
|
19
|
+
* ## Why the constructor is not an exposure classifier
|
|
20
|
+
*
|
|
21
|
+
* There is already a predicate for "is this bind exposed?", `LOOPBACK_HOSTS` in
|
|
22
|
+
* `ops-api-bind.ts` (`{127.0.0.1, localhost, ::1}`). This module does NOT reuse
|
|
23
|
+
* it, and must not: that set answers a DIFFERENT question. It classifies
|
|
24
|
+
* whether a bind is narrowed; it does not guarantee that a client on IPv4
|
|
25
|
+
* loopback can reach the listener. The two disagree on exactly the hosts that
|
|
26
|
+
* matter here:
|
|
27
|
+
*
|
|
28
|
+
* - `::1` (IPv6 loopback) passes "is it exposed?" (it is not — it is
|
|
29
|
+
* loopback) but a client dialing `127.0.0.1` does NOT reach it. Harper
|
|
30
|
+
* passes the parsed host straight to `listen` with no loopback translation
|
|
31
|
+
* (`dist/server/threads/threadServer.js`, `listenOnPorts`), so `[::1]:PORT`
|
|
32
|
+
* would satisfy every exposure check, fail every self-call, and leave every
|
|
33
|
+
* bind assertion green.
|
|
34
|
+
* - `localhost` depends on name resolution and may resolve to `::1` on a
|
|
35
|
+
* v6-first host.
|
|
36
|
+
*
|
|
37
|
+
* So this constructor allows a host only when it GUARANTEES IPv4-loopback
|
|
38
|
+
* reachability: `127.0.0.1`, or a wildcard (which includes IPv4 loopback).
|
|
39
|
+
* Every other host — a specific non-loopback interface address, an IPv6-only
|
|
40
|
+
* loopback, a hostname, anything unparseable — is refused. That refusal is
|
|
41
|
+
* load-bearing: all of Flair's credentialed self-calls hardcode
|
|
42
|
+
* `http://127.0.0.1:<port>`, so a bind that excludes IPv4 loopback leaves them
|
|
43
|
+
* pointing at a dead port while every bind check still passes.
|
|
44
|
+
*
|
|
45
|
+
* Deliberate widening is still possible through the escape hatch
|
|
46
|
+
* (`--http-bind` / `FLAIR_HTTP_BIND` / persisted `httpBind`), but only to a
|
|
47
|
+
* WILDCARD — the hatch waives the exposure restriction, never the connectivity
|
|
48
|
+
* requirement.
|
|
49
|
+
*/
|
|
50
|
+
import { harperPortValue } from "./harper-port-value.js";
|
|
51
|
+
/** The only non-wildcard host Flair will bind: it is IPv4 loopback by definition. */
|
|
52
|
+
export const DEFAULT_HTTP_BIND_HOST = "127.0.0.1";
|
|
53
|
+
/**
|
|
54
|
+
* Wildcard hosts. Binding one of these includes IPv4 loopback, so the
|
|
55
|
+
* hardcoded `http://127.0.0.1:<port>` self-calls still land — this is why a
|
|
56
|
+
* wildcard is the only widening the constructor will produce.
|
|
57
|
+
*/
|
|
58
|
+
const WILDCARD_HOSTS = new Set(["0.0.0.0", "::", "0:0:0:0:0:0:0:0"]);
|
|
59
|
+
/**
|
|
60
|
+
* Thrown when a requested bind host cannot be guaranteed reachable on IPv4
|
|
61
|
+
* loopback. Callers refuse BEFORE writing anything (plist, config, spawn env),
|
|
62
|
+
* so a bad host can never be persisted.
|
|
63
|
+
*/
|
|
64
|
+
export class UnreachableHttpBindHostError extends Error {
|
|
65
|
+
host;
|
|
66
|
+
constructor(host) {
|
|
67
|
+
super(`refusing to bind Harper's HTTP listener to "${host}": every credentialed ` +
|
|
68
|
+
`self-call Flair makes hardcodes 127.0.0.1, so a bind that does not include ` +
|
|
69
|
+
`IPv4 loopback would leave them pointing at a dead port. Use 127.0.0.1 ` +
|
|
70
|
+
`(the default), or a wildcard (0.0.0.0 / ::) for deliberate widening.`);
|
|
71
|
+
this.host = host;
|
|
72
|
+
this.name = "UnreachableHttpBindHostError";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** Strip a single pair of surrounding square brackets from an IPv6 literal. */
|
|
76
|
+
function stripBrackets(host) {
|
|
77
|
+
return host.startsWith("[") && host.endsWith("]") ? host.slice(1, -1) : host;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* True when a bind host guarantees an IPv4-loopback client can reach the
|
|
81
|
+
* listener. `127.0.0.1` and a wildcard yes; everything else no. Exported so
|
|
82
|
+
* the escape-hatch validation and the tests name the same predicate.
|
|
83
|
+
*/
|
|
84
|
+
export function guaranteesIpv4Loopback(host) {
|
|
85
|
+
const h = stripBrackets(host.trim());
|
|
86
|
+
return h === DEFAULT_HTTP_BIND_HOST || WILDCARD_HOSTS.has(h);
|
|
87
|
+
}
|
|
88
|
+
function isIpv6Literal(host) {
|
|
89
|
+
return host.includes(":");
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Build the Harper HTTP bind value, refusing any host that does not guarantee
|
|
93
|
+
* IPv4-loopback reachability. An absent/empty host defaults to `127.0.0.1`.
|
|
94
|
+
*
|
|
95
|
+
* `port` must be an integer in 1..65535 (`resolveHttpPort` guarantees this for
|
|
96
|
+
* the production callers; the check is here so a stray value cannot reach a
|
|
97
|
+
* config payload that `new URL()` or Harper's `listen` would later reject).
|
|
98
|
+
*/
|
|
99
|
+
export function httpBind(host, port) {
|
|
100
|
+
const rawHost = host === undefined || host === null ? "" : String(host).trim();
|
|
101
|
+
const resolvedHost = rawHost === "" ? DEFAULT_HTTP_BIND_HOST : rawHost;
|
|
102
|
+
const portNum = typeof port === "number" ? port : Number(String(port).trim());
|
|
103
|
+
if (!Number.isInteger(portNum) || portNum < 1 || portNum > 65535) {
|
|
104
|
+
throw new RangeError(`refusing to build an HTTP bind for port "${String(port)}": must be an integer in 1..65535`);
|
|
105
|
+
}
|
|
106
|
+
if (!guaranteesIpv4Loopback(resolvedHost)) {
|
|
107
|
+
throw new UnreachableHttpBindHostError(resolvedHost);
|
|
108
|
+
}
|
|
109
|
+
const h = stripBrackets(resolvedHost);
|
|
110
|
+
const bindValue = isIpv6Literal(h) ? `[${h}]:${portNum}` : `${h}:${portNum}`;
|
|
111
|
+
return { bindValue, host: h, port: portNum };
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* The CORS allow-list entries for an HTTP bind. Uses the PORT half only:
|
|
115
|
+
* CORS is about the browser-facing origin, and every entry Flair writes is a
|
|
116
|
+
* loopback origin regardless of which interface the listener bound.
|
|
117
|
+
*/
|
|
118
|
+
export function httpCorsAccessList(port) {
|
|
119
|
+
return [`http://127.0.0.1:${port}`, `http://localhost:${port}`];
|
|
120
|
+
}
|
|
121
|
+
// ─── repair: preserving an instance's recorded coordinates ─────────────────
|
|
122
|
+
//
|
|
123
|
+
// `doctor --fix` is the ONE bind emitter that does not go through `httpBind()`:
|
|
124
|
+
// it promises not to move the instance's coordinates, so it PRESERVES what the
|
|
125
|
+
// instance recorded (a bare legacy port stays bare; an already-qualified
|
|
126
|
+
// `host:port` keeps its host). The helpers below are shared between the CLI's
|
|
127
|
+
// plist writer and the pure repair PLANNER, so the planner can refuse an
|
|
128
|
+
// unsupported configuration BEFORE the executor stops anything.
|
|
129
|
+
/**
|
|
130
|
+
* Preserve an instance's recorded HTTP port value VERBATIM for a repair.
|
|
131
|
+
*
|
|
132
|
+
* Throws for a DISABLED value (absent, null or empty) and for an UNSUPPORTED
|
|
133
|
+
* value (a port that cannot be parsed). Callers refuse rather than substituting
|
|
134
|
+
* a default: a default here is a coordinate change that can enable a listener
|
|
135
|
+
* the instance never had.
|
|
136
|
+
*/
|
|
137
|
+
export function preserveHttpPortValue(raw) {
|
|
138
|
+
if (raw === undefined || raw === null || (typeof raw === "string" && raw.trim() === "")) {
|
|
139
|
+
throw new Error("refusing to repair the launchd plist: the instance's harper-config.yaml records no http.port, " +
|
|
140
|
+
"so the HTTP listener is disabled. Repair preserves the instance's coordinates and will not " +
|
|
141
|
+
"substitute a default port, which would enable a listener the instance did not have.");
|
|
142
|
+
}
|
|
143
|
+
if (typeof raw === "number") {
|
|
144
|
+
if (Number.isInteger(raw) && raw >= 1 && raw <= 65535)
|
|
145
|
+
return String(raw);
|
|
146
|
+
throw new Error(`refusing to repair the launchd plist: http.port is ${raw}, which is not a usable port`);
|
|
147
|
+
}
|
|
148
|
+
const str = String(raw).trim();
|
|
149
|
+
if (harperPortValue(str) === null) {
|
|
150
|
+
throw new Error(`refusing to repair the launchd plist: http.port is "${str}", which is not a parseable bind value`);
|
|
151
|
+
}
|
|
152
|
+
return str;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* A secure-listener port, reduced to its number. Returns undefined for a
|
|
156
|
+
* disabled value (so the key is omitted and a disabled listener STAYS
|
|
157
|
+
* disabled), and throws for an unparseable one (so a caller refuses rather than
|
|
158
|
+
* writing a value it cannot read).
|
|
159
|
+
*/
|
|
160
|
+
export function preserveSecurePort(raw) {
|
|
161
|
+
if (raw === undefined || raw === null || (typeof raw === "string" && raw.trim() === ""))
|
|
162
|
+
return undefined;
|
|
163
|
+
const port = harperPortValue(raw);
|
|
164
|
+
if (port === null) {
|
|
165
|
+
throw new Error(`refusing to repair the launchd plist: secure port "${String(raw)}" is not a usable port`);
|
|
166
|
+
}
|
|
167
|
+
return port;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* The host half of a preserved `host:port` bind value, or null when the value
|
|
171
|
+
* is bare (no host) or unparseable. Mirrors `ops-api-bind.ts`'s `parseBindHost`
|
|
172
|
+
* so both readers agree on what "the host half" is.
|
|
173
|
+
*/
|
|
174
|
+
export function bindHostOf(value) {
|
|
175
|
+
const s = value.trim();
|
|
176
|
+
if (s.startsWith("[")) {
|
|
177
|
+
const close = s.indexOf("]");
|
|
178
|
+
return close === -1 ? null : s.slice(1, close);
|
|
179
|
+
}
|
|
180
|
+
const lastColon = s.lastIndexOf(":");
|
|
181
|
+
if (lastColon <= 0)
|
|
182
|
+
return null;
|
|
183
|
+
const tail = s.slice(lastColon + 1);
|
|
184
|
+
return /^\d+$/.test(tail) ? s.slice(0, lastColon) : null;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* The host to qualify an ENABLED secure listener with, given the (preserved)
|
|
188
|
+
* plaintext bind host. A bare secure value binds ALL interfaces exactly like a
|
|
189
|
+
* bare plaintext port does — Harper feeds `http.securePort` through the same
|
|
190
|
+
* `listenOnPorts` path — so when the plaintext listener IS narrowed, a bare
|
|
191
|
+
* secure value is qualified with the same host, or TLS would stay wide while
|
|
192
|
+
* plaintext is narrowed. A plaintext host that cannot guarantee IPv4 loopback
|
|
193
|
+
* (e.g. a legacy `::1`) is not mirrored; the secure listener falls back to the
|
|
194
|
+
* loopback default instead of producing a bind Flair's self-calls cannot reach.
|
|
195
|
+
*/
|
|
196
|
+
export function secureBindHostFor(plaintextHost) {
|
|
197
|
+
return plaintextHost !== null && guaranteesIpv4Loopback(plaintextHost) ? plaintextHost : DEFAULT_HTTP_BIND_HOST;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* The secure-listener config value a repair should write, or undefined when the
|
|
201
|
+
* listener is disabled. Repair PRESERVES the instance's coordinates, and this
|
|
202
|
+
* rule is "both or neither" with respect to the plaintext bind:
|
|
203
|
+
*
|
|
204
|
+
* - the secure value already names a host -> preserve that value verbatim
|
|
205
|
+
* (repair never re-homes a listener the instance recorded with a host);
|
|
206
|
+
* - the secure value is BARE and the plaintext value is ALSO bare -> preserve
|
|
207
|
+
* it bare. Repair moves NEITHER listener; narrowing TLS here while leaving a
|
|
208
|
+
* bare (all-interfaces) plaintext port in place would move one coordinate
|
|
209
|
+
* and silently drop LAN TLS clients after a `doctor --fix` that promised to
|
|
210
|
+
* change nothing;
|
|
211
|
+
* - the secure value is BARE and the plaintext value is qualified -> qualify
|
|
212
|
+
* the secure host the same way as the plaintext bind (a bare secure port
|
|
213
|
+
* binds all interfaces exactly like a bare plaintext port, so leaving it
|
|
214
|
+
* bare would narrow plaintext and leave TLS wide).
|
|
215
|
+
*/
|
|
216
|
+
export function qualifySecureBindValue(secureRaw, plaintextValue) {
|
|
217
|
+
const port = preserveSecurePort(secureRaw);
|
|
218
|
+
if (port === undefined)
|
|
219
|
+
return undefined;
|
|
220
|
+
const recorded = String(secureRaw).trim();
|
|
221
|
+
if (bindHostOf(recorded) !== null)
|
|
222
|
+
return recorded;
|
|
223
|
+
const plaintextHost = bindHostOf(plaintextValue);
|
|
224
|
+
if (plaintextHost === null)
|
|
225
|
+
return recorded;
|
|
226
|
+
return httpBind(secureBindHostFor(plaintextHost), port).bindValue;
|
|
227
|
+
}
|